From 7c730cd83102591797c36114a6443fcdb65c2e2b Mon Sep 17 00:00:00 2001 From: emmatveev Date: Thu, 7 Mar 2024 19:58:02 +0300 Subject: [PATCH] updater --- .deploy/deploy-dev.yaml | 17 +++++++++++++++++ .deploy/deploy-prod.yaml | 17 +++++++++++++++++ main.py | 6 ++++++ updater.py | 21 +++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 updater.py diff --git a/.deploy/deploy-dev.yaml b/.deploy/deploy-dev.yaml index 95d8263..b8fd083 100644 --- a/.deploy/deploy-dev.yaml +++ b/.deploy/deploy-dev.yaml @@ -20,6 +20,23 @@ services: parallelism: 1 order: start-first + updater: + image: mathwave/sprint-repo:pizda-bot + command: updater + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV + MONGO_HOST: "mongo.develop.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_DEV + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "development" + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + pizda-bot-nginx: image: mathwave/sprint-repo:pizda-bot command: api diff --git a/.deploy/deploy-prod.yaml b/.deploy/deploy-prod.yaml index f6ee1f2..a0a49ee 100644 --- a/.deploy/deploy-prod.yaml +++ b/.deploy/deploy-prod.yaml @@ -20,6 +20,23 @@ services: parallelism: 1 order: start-first + updater: + image: mathwave/sprint-repo:pizda-bot + command: updater + environment: + TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD + MONGO_HOST: "mongo.sprinthub.ru" + MONGO_PASSWORD: $MONGO_PASSWORD_PROD + PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN + STAGE: "production" + deploy: + mode: replicated + restart_policy: + condition: any + update_config: + parallelism: 1 + order: start-first + pizda-bot-nginx: image: mathwave/sprint-repo:pizda-bot command: api diff --git a/main.py b/main.py index e1aedb1..fcd50b8 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import datetime import os import sys from random import randrange, choice @@ -68,6 +69,8 @@ def do_action(message: Message): bot.reply_to(message, choice(get_replies())) return info = get_chat_info(message.chat.id) + if client.get_config('updater')['enabled']: + set_values(message.chat.id, last_time_updated=datetime.datetime.now()) if message.text == '#debug' and client.is_staff(telegram_id=message.from_user.id): bot.send_message(message.chat.id, f'chat id: {message.chat.id}\n' f'probability: {info["probability"]}') @@ -97,6 +100,9 @@ def do_action(message: Message): arg = sys.argv[-1] if arg == 'bot': bot.polling() +elif arg == 'updater': + from updater import update + update() else: from api import app app.run(host="0.0.0.0", port=1238) diff --git a/updater.py b/updater.py new file mode 100644 index 0000000..cbcf540 --- /dev/null +++ b/updater.py @@ -0,0 +1,21 @@ +import datetime +import random +from time import sleep + +from mongo import mongo + + +def update(): + from main import bot, client + while True: + if client.get_config('updater')['enabled']: + fltr = {"last_time_updated": {"$lte": datetime.datetime.now() - datetime.timedelta(seconds=client.get_config('updater')['seconds_delay'])}} + if client.get_config('updater')['send_to_single']: + fltr["chat_id"] = {"$lt": 0} + for chat in mongo.chats_collection.find(fltr): + users = list(mongo.counter_collection.find({"chat_id": chat['chat_id'], 'username': {"$ne": None}})) + if not users: + continue + user = random.choice(users) + bot.send_message(chat['chat_id'], f"Как говорится, кто молчит - тот трансвестит, а трансвестит сегодня - @{user['username']}") + sleep(client.get_config('updater')['seconds_delay'])