Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge 813c7bf into 5b7c73f
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCoderCarl authored Oct 20, 2022
2 parents 5b7c73f + 813c7bf commit 28debbc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ dmypy.json

# Pyre type checker
.pyre/

# List of user ids
user_list.txt

# Session journals
*.session*
4 changes: 2 additions & 2 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ FROM python:3.9-alpine as builder
COPY ["bot.py", "/opt/"]
COPY requirements.txt requirements.txt

RUN pip3 install --no-cache-dir -r requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

FROM builder

CMD python3 /opt/bot.py
CMD ["python3", "/opt/bot.py"]
92 changes: 92 additions & 0 deletions bot.py
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()
11 changes: 9 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ services:
image: h0d0user/herold_bot:latest
restart: always
network_mode: host
# environment:
# - API_TOKEN=YOURTOKEN
environment:
- API_ID=YOURAPIID
- API_HASH=YOURAPIHASH
- SESSION=YOURSESSION
volumes:
- /opt/:/root/herold_bot/user_list.txt
- /opt/:/root/herold_bot/JustTalk.session
volumes:
herold_bot:
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
black
isort
telethon~=1.25.4

0 comments on commit 28debbc

Please sign in to comment.