Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
df89b14
Merge pull request #6 from CoolCoderCarl/develop
CoolCoderCarl Oct 23, 2022
ecfd40b
Update requirements.txt & .gitignore; add sqlitedb.py with sqlalchemy
CoolCoderCarl Mar 30, 2023
ce145a0
Add sqlitedb.py & update bot.py
CoolCoderCarl Apr 11, 2023
551f2c7
Fixed; add models.py
CoolCoderCarl Apr 13, 2023
2032fba
Merge branch 'develop' into feature/FUNC_6
CoolCoderCarl Apr 13, 2023
0c8c8b0
Small fixes
CoolCoderCarl Apr 13, 2023
2fbaa28
Small fixes
CoolCoderCarl Apr 13, 2023
95fb94b
Update congrats func
CoolCoderCarl Apr 15, 2023
402b759
Fix Containerfile
CoolCoderCarl Apr 15, 2023
66f2643
Update docker-compose.yaml
CoolCoderCarl Apr 15, 2023
c1b6870
Update docker-compose.yaml & Containerfile
CoolCoderCarl Apr 15, 2023
3d0a04c
Update docker-compose.yaml & Containerfile & requirements.txt
CoolCoderCarl Apr 16, 2023
9d8b267
Update .gitignore
CoolCoderCarl Apr 16, 2023
54706e3
Add bucket.py & dynaconfig.py; update bot.py
CoolCoderCarl Apr 16, 2023
268c8c5
Update docker-compose.yaml
CoolCoderCarl Apr 16, 2023
88fc886
Send picture from bucket
CoolCoderCarl Apr 16, 2023
bdf4b12
Fix Containerfile
CoolCoderCarl Apr 16, 2023
b98a340
Fix bot.py
CoolCoderCarl Apr 16, 2023
f2e942c
Fix requirements.txt
CoolCoderCarl Apr 16, 2023
f95ee09
Add prune func to clear container
CoolCoderCarl Apr 16, 2023
22be4e0
Update .gitignore & docker-compose.yaml
CoolCoderCarl Apr 18, 2023
4f5db8d
Update .gitignore
CoolCoderCarl Apr 18, 2023
0d6c21b
Update docker-compose.yaml
CoolCoderCarl Apr 18, 2023
319c529
Move to src; update & fixes
CoolCoderCarl Apr 18, 2023
3bb6199
Small fixes
CoolCoderCarl Apr 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,19 @@ dmypy.json
# List of user ids & answers
*_list.txt
*_response.txt
*_file.txt
*_patterns.txt

# Session journals
*.session*
*.session*

# SQLite
*.db

# Bucket objects
birthdays/
*.jpg
*.png

# Dynaconf
settings.toml
3 changes: 2 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM python:3.9-alpine as builder

WORKDIR /opt

COPY ["bot.py", "/opt/"]
ADD ["/src/", "/opt/"]

COPY requirements.txt requirements.txt

RUN pip3 install --no-cache-dir -r requirements.txt
Expand Down
184 changes: 0 additions & 184 deletions bot.py

This file was deleted.

15 changes: 10 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ services:
container_name: herold_bot
image: h0d0user/herold_bot:latest
restart: always
network_mode: host
networks:
- herold_net
environment:
- API_ID=YOURAPIID
- API_HASH=YOURAPIHASH
- SESSION=YOURSESSION
volumes:
- "/root/herold_bot/circulation_ids_list.txt:/opt/circulation_ids_list.txt"
- "/root/herold_bot/circulation_response.txt:/opt/circulation_response.txt"
- "/root/herold_bot/new_year_patterns.txt:/opt/new_year_patterns.txt"
- "/root/herold_bot/herold_database.db:/opt/herold_database.db"
- "/root/herold_bot/congrats_list.txt:/opt/congrats_list.txt"
- "/root/herold_bot/JustTalk.session:/opt/JustTalk.session"
- "/root/herold_bot/settings.toml:/opt/settings.toml"

volumes:
herold_bot:
herold_volume:

networks:
herold_net:
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
black
isort
telethon~=1.25.4
boto3~=1.26.114
dynaconf~=3.1.12
telethon~=1.25.4
SQLAlchemy~=2.0.7
botocore~=1.29.114
86 changes: 86 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import asyncio
import logging
import os
import random
import time
from datetime import datetime
from pathlib import Path
from typing import List

from telethon import TelegramClient

import bucket
import db

# Environment variables
API_ID = os.environ["API_ID"]
API_HASH = os.environ["API_HASH"]
SESSION = os.environ["SESSION"]

client = TelegramClient(SESSION, API_ID, API_HASH)

# 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_congrats_from_file(file: Path) -> List[str]:
"""
Load congrats from the file
Try to load from file, if exception caught, send message about err
:return:
"""
try:
with open(Path(file), "r", encoding="utf-8") as file:
result = file.readlines()
logging.info(f"Uploaded response from the {file.name} done successfully.")
return result
except FileNotFoundError as file_not_found_err:
logging.error(f"Err while load file - {file_not_found_err}")
return None


CONGRATULATIONS = load_congrats_from_file(Path("congrats_list.txt"))


async def send_congratulations():
"""
Check if someone has a birthday on this date then send congratulation
Wait for one day to check date
:return:
"""
current_date = datetime.today().strftime("%m.%d")
if db.get_tg_id(current_date) is None:
logging.info(f"Today - {current_date} - are no one to congratulate")
time.sleep(86400)
else:
user_data = await client.get_entity(db.get_tg_id(current_date))
logging.info(
f"Today - {current_date} - going to congratulate {user_data.first_name} - {user_data.username}"
)
await client.send_message(
db.get_tg_id(current_date), random.choice(CONGRATULATIONS)
)
async with client.action(user_data.id, "typing"):
await asyncio.sleep(random.randrange(2, 5))
await client.send_file(
db.get_tg_id(current_date),
bucket.download_file_from_bucket("birthdays", Path("/home/")),
)
time.sleep(400)
bucket.prune_directory(Path("/home/"))
time.sleep(86000)


async def main():
while True:
await send_congratulations()


if __name__ == "__main__":
with client:
client.loop.run_until_complete(main())
Loading