This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CoolCoderCarl/develop
Develop
- Loading branch information
Showing
5 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,9 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# List of user ids | ||
user_list.txt | ||
|
||
# Session journals | ||
*.session* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import asyncio | ||
import logging | ||
import os | ||
import random | ||
from typing import List | ||
|
||
from telethon import TelegramClient, events | ||
|
||
API_ID = os.environ["API_ID"] | ||
API_HASH = os.environ["API_HASH"] | ||
SESSION = os.environ["SESSION"] | ||
|
||
CLIENT = TelegramClient(SESSION, API_ID, API_HASH) | ||
|
||
IGNORED_FILE = "user_list.txt" | ||
|
||
|
||
# Logging | ||
logging.basicConfig( | ||
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO | ||
) | ||
logging.basicConfig( | ||
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.ERROR | ||
) | ||
|
||
|
||
def load_user_ids_from_file() -> List[int]: | ||
""" | ||
Load user ids from file | ||
Try to load from file, if exception caught, send message about err | ||
Also convert to int to compare with id from Telegram | ||
:return: | ||
""" | ||
try: | ||
with open(IGNORED_FILE, "r") as users_ids_file: | ||
user_ids = [int(u_ids) for u_ids in users_ids_file.read().split()] | ||
logging.info("Uploaded from the file done successfully.") | ||
return user_ids | ||
except FileNotFoundError as file_not_found_err: | ||
logging.error(file_not_found_err) | ||
|
||
|
||
USERS_ID = load_user_ids_from_file() | ||
|
||
|
||
async def show_selected_users(): | ||
async for dialog in CLIENT.iter_dialogs(): | ||
if dialog.id in USERS_ID: | ||
logging.info(f"Selected username: {dialog.name}; ID: {dialog.id}") | ||
|
||
|
||
@CLIENT.on(events.NewMessage) | ||
async def handle_new_message(event): | ||
await show_selected_users() | ||
|
||
user_data = await event.client.get_entity(event.from_id) | ||
logging.info(f"Raw sender data: {user_data}") | ||
try: | ||
if user_data.id in USERS_ID: | ||
logging.info( | ||
f"User with name {user_data.first_name} - " | ||
f"with ID: {user_data.id} - " | ||
f"send message: {event.message.message}" | ||
) | ||
logging.info("Waiting for answer...") | ||
await asyncio.sleep(random.randrange(3, 15)) | ||
async with CLIENT.action(user_data.id, "typing"): | ||
await asyncio.sleep(random.randrange(2, 5)) | ||
await CLIENT.send_message( | ||
user_data.id, | ||
f""" | ||
Hello, {user_data.first_name}. \n | ||
This is an auto-generated answer just for you. \n | ||
You have been pseudorandomly selected to test a new bot. \n | ||
**Congratulations, it's absolutely free !** \n | ||
Soon I will come to you, but it's not certain. \n | ||
GL HF | ||
""", | ||
) | ||
logging.info("Answer was sent.") | ||
except ValueError as val_err: | ||
logging.error(f"Sender is {user_data.first_name}") | ||
logging.error(val_err) | ||
except TypeError as type_err: | ||
logging.error("That maybe sticker was sent, not text.") | ||
logging.error(f"Sender is {user_data.first_name}") | ||
logging.error(type_err) | ||
|
||
|
||
if __name__ == "__main__": | ||
CLIENT.start() | ||
CLIENT.run_until_disconnected() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
black | ||
isort | ||
telethon~=1.25.4 |