Skip to content

Commit

Permalink
Use session for bot
Browse files Browse the repository at this point in the history
  • Loading branch information
borisevich-a-v committed Dec 25, 2024
1 parent 29a2d23 commit 62632a6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/aggregator/bot/create_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from typing_extensions import NamedTuple

from aggregator.bot.warden.warden import NotAllowed, Warden
from aggregator.config import ADMIN, AGGREGATOR_CHANNEL, TELEGRAM_API_HASH, TELEGRAM_API_ID, TELEGRAM_BOT_TOKEN
from aggregator.config import ADMIN, AGGREGATOR_CHANNEL, TELEGRAM_API_HASH, TELEGRAM_API_ID, TELEGRAM_BOT_TOKEN, \
BOT_SESSION
from aggregator.posts_storage import NoNewPosts, PostStorage
from aggregator.telegram_slow_client import TelegramSlowClient

Expand Down Expand Up @@ -45,9 +46,7 @@ def get_request_pattern(post_storage: PostStorage) -> re.Pattern:

def create_bot(post_storage: PostStorage, warden: Warden) -> TelegramClient:
logger.info("Creating bot")
bot = TelegramSlowClient(StringSession(), TELEGRAM_API_ID, TELEGRAM_API_HASH, min_request_interval=0.005).start(
bot_token=TELEGRAM_BOT_TOKEN
)
bot = TelegramSlowClient(StringSession(BOT_SESSION), TELEGRAM_API_ID, TELEGRAM_API_HASH, min_request_interval=0.005)

# TODO: should we parse album here? nea
@bot.on(events.NewMessage(chats=AGGREGATOR_CHANNEL))
Expand Down
2 changes: 2 additions & 0 deletions src/aggregator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def load_env_list(env_name: str) -> list[str]:
TELEGRAM_API_HASH = str(os.environ.get("TELEGRAM_API_HASH"))

CLIENT_SESSION = str(os.environ.get("CLIENT_SESSION"))
BOT_SESSION = str(os.environ.get("BOT_SESSION"))

DB_CONNECTION_STRING = str(os.environ.get("DB_CONNECTION_STRING"))

ADMIN = int(os.environ.get("ADMIN", 0))
Expand Down
2 changes: 1 addition & 1 deletion utilities/generate_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def generate_bot_session():
print("Generating bot session")
with TelegramClient(StringSession(), TELEGRAM_API_ID, TELEGRAM_API_HASH).start(bot_token=TELEGRAM_BOT_TOKEN) as bot:
print("Bot session:")
print(bot.sesson.save())
print(bot.session.save())


if len(sys.argv) != 2:
Expand Down

0 comments on commit 62632a6

Please sign in to comment.