Merge pull request 'queues' (#16) from queues into master

Reviewed-on: #16
This commit is contained in:
emmatveev 2024-11-17 14:13:26 +03:00
commit 53e929fe5a
14 changed files with 322 additions and 153 deletions

View File

@ -3,15 +3,13 @@ version: "3.4"
services:
bot:
poll:
image: mathwave/sprint-repo:pizda-bot
command: bot
command: poll
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"
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
deploy:
mode: replicated
restart_policy:
@ -20,15 +18,30 @@ services:
parallelism: 1
order: start-first
updater:
worker:
image: mathwave/sprint-repo:pizda-bot
command: updater
command: worker
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"
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
mailbox:
image: mathwave/sprint-repo:pizda-bot
command: mailbox
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
STAGE: "development"
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
deploy:
mode: replicated
restart_policy:

View File

@ -3,15 +3,13 @@ version: "3.4"
services:
bot:
poll:
image: mathwave/sprint-repo:pizda-bot
command: bot
command: poll
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "production"
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
deploy:
mode: replicated
restart_policy:
@ -20,15 +18,30 @@ services:
parallelism: 1
order: start-first
updater:
worker:
image: mathwave/sprint-repo:pizda-bot
command: updater
command: worker
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "production"
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
deploy:
mode: replicated
restart_policy:
condition: any
update_config:
parallelism: 1
order: start-first
mailbox:
image: mathwave/sprint-repo:pizda-bot
command: mailbox
environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
STAGE: "production"
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
deploy:
mode: replicated
restart_policy:

View File

@ -42,4 +42,5 @@ jobs:
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
QUEUES_TOKEN_DEV: ${{ secrets.QUEUES_TOKEN_DEV }}
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot

View File

@ -42,4 +42,5 @@ jobs:
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
QUEUES_TOKEN_PROD: ${{ secrets.QUEUES_TOKEN_PROD }}
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot

3
daemons/base.py Normal file
View File

@ -0,0 +1,3 @@
class BaseDaemon:
def execute(self):
raise NotImplementedError

27
daemons/mailbox.py Normal file
View File

@ -0,0 +1,27 @@
import telebot
import os
from daemons import base
from utils import queues
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
def __init__(self):
super().__init__()
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@property
def queue_name(self):
return 'pizda_bot_mailbox'
def execute(self):
self.poll()
def process(self, payload):
body = {
'chat_id': payload['chat_id'],
'text': payload['message'],
}
if payload['action'] == 'reply':
body['reply_to_message_id'] = payload['reply_to']
self.bot.send_message(**body)

16
daemons/poll.py Normal file
View File

@ -0,0 +1,16 @@
import os
import json
import telebot
from daemons import base
from telebot import types
from utils import queues
class Daemon(base.BaseDaemon):
def execute(self):
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@bot.message_handler()
def do_action(message: types.Message):
queues.set_task('pizda_bot_worker', message.json, 1)
bot.polling()

148
daemons/worker.py Normal file
View File

@ -0,0 +1,148 @@
import datetime
import os
from random import randrange, choice
from daemons import base
from utils import queues
from telebot.types import Message
import json
from mongo import mongo
from sprint_platform import PlatformClient
from storage import set_values, get_chat_info
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
stage = os.getenv("STAGE", 'local')
client = PlatformClient(
security_token,
'Pizda Bot',
stage,
['constants', 'answers', 'replies'],
[],
need_poll=True,
)
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
def get_self_name():
return client.get_config('constants')['self_name']
def get_answers():
return client.get_config('answers')
def get_replies():
return client.get_config('replies')
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
@property
def queue_name(self):
return 'pizda_bot_worker'
def execute(self):
self.poll()
def reply(self, text: str, chat_id: int, message_id: int):
queues.set_task(
'pizda_bot_mailbox',
{
'action': 'reply',
'message': text,
'reply_to': message_id,
'chat_id': chat_id
},
1,
)
def send(self, text: str, chat_id: int):
queues.set_task(
'pizda_bot_mailbox',
{
'action': 'send',
'message': text,
'chat_id': chat_id
},
1,
)
def process_command(self, message: Message):
if message.text.startswith('/pointers'):
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id, "points": {"$exists": True}}).sort("points", -1))
if not rating:
self.send('Ебаллы пока никому не начислялись', message.chat.id)
return
text = "Рейтинг Ебальников:\n"
rating_arr = list(enumerate(rating))
total = 0
for _, value in rating_arr:
total += value['points']
for index, value in rating_arr:
text += f"{index + 1}. @{value['username']} - {value['points']}\n"
self.send(text, message.chat.id)
elif message.text.startswith('/point'):
if not message.reply_to_message:
self.reply('Чтобы начислить Ебаллы, нужно прописать команду ответом на чье-то сообщение', message.chat.id, message.message_id)
return
username = message.reply_to_message.from_user.username
if username == get_self_name():
self.reply('Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!', message.chat.id, message.message_id)
return
mongo.inc_points(username, message.chat.id)
self.reply('Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!', message.chat.id, message.reply_to_message.message_id)
elif message.text.startswith('/rating'):
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id}).sort("count", -1))
if not rating:
self.send('В этом чате я пока никому не парировал', message.chat.id)
return
text = "Вот кому я парировал:\n"
rating_arr = list(enumerate(rating))
total = 0
for _, value in rating_arr:
total += value['count']
for index, value in rating_arr:
text += f"{index + 1}. @{value['username']} - {value['count']} ({int(value['count'] / total * 100)}%)\n"
self.send(text, message.chat.id)
elif message.text.startswith('/setprobability'):
self.send('Отправь одно число - вероятность парирования', message.chat.id)
set_values(message.chat.id, state="set_probability")
def process(self, payload):
message: Message = Message.de_json(json.dumps(payload))
if message.text.startswith('/'):
self.process_command(message)
return
if message.reply_to_message:
if message.reply_to_message.from_user.username == get_self_name():
self.reply(choice(get_replies()), message.chat.id, message.message_id)
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):
self.reply(f'chat id: {message.chat.id}\nprobability: {info["probability"]}', message.chat.id, message.message_id)
return
if info['state'] == "set_probability":
try:
value = int(message.text)
if value < 0 or value > 100:
self.reply('Число не попадает в диапозон от 0 до 100!', message.chat.id, message.message_id)
else:
set_values(message.chat.id, probability=value, state="default")
self.reply('Ок! Установил', message.chat.id, message.message_id)
except ValueError:
self.reply('Это не число!', message.chat.id, message.message_id)
return
convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
if len(convert_text) > 0:
convert_text = convert_text[-1]
else:
return
ans = get_answers().get(convert_text)
if ans is not None and randrange(1, 101) <= info["probability"]:
self.reply(ans, message.chat.id, message.message_id)
mongo.inc(message.from_user.username, message.chat.id)

0
launch.json Normal file
View File

5
local_platform.json Normal file
View File

@ -0,0 +1,5 @@
{
"configs": {},
"experiments": {},
"platform_staff": {}
}

141
main.py
View File

@ -1,137 +1,18 @@
import datetime
import os
import sys
from random import randrange, choice
import telebot
from telebot.types import Message
from mongo import mongo
from sprint_platform import PlatformClient
from storage import set_values, get_chat_info
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
stage = os.getenv("STAGE", 'local')
client = PlatformClient(
security_token,
'Pizda Bot',
stage,
['constants', 'answers', 'replies'],
[],
need_poll=True,
)
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
def get_self_name():
return client.get_config('constants')['self_name']
def get_answers():
return client.get_config('answers')
def get_replies():
return client.get_config('replies')
@bot.message_handler(commands=['setprobability'])
def set_probability(message: Message):
bot.send_message(message.chat.id, "Отправь одно число - вероятность парирования")
set_values(message.chat.id, state="set_probability")
@bot.message_handler(commands=['rating'])
def show_rating(message: Message):
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id}).sort("count", -1))
if not rating:
bot.send_message(message.chat.id, "В этом чате я пока никому не парировал")
return
text = "Вот кому я парировал:\n"
rating_arr = list(enumerate(rating))
total = 0
for _, value in rating_arr:
total += value['count']
for index, value in rating_arr:
text += f"{index + 1}. @{value['username']} - {value['count']} ({int(value['count'] / total * 100)}%)\n"
bot.send_message(message.chat.id, text)
@bot.message_handler(commands=['point'])
def point(message: Message):
if not message.reply_to_message:
bot.reply_to(message, 'Чтобы начислить Ебаллы, нужно прописать команду ответом на чье-то сообщение')
return
username = message.reply_to_message.from_user.username
if username == get_self_name():
bot.reply_to(message, 'Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!')
return
mongo.inc_points(username, message.chat.id)
bot.reply_to(message.reply_to_message, 'Тебе начислили Ебалл!')
@bot.message_handler(commands=['pointers'])
def pointers(message: Message):
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id, "points": {"$exists": True}}).sort("points", -1))
if not rating:
bot.send_message(message.chat.id, "Ебаллы пока никому не начислялись")
return
text = "Рейтинг Ебальников:\n"
rating_arr = list(enumerate(rating))
total = 0
for _, value in rating_arr:
total += value['points']
for index, value in rating_arr:
text += f"{index + 1}. @{value['username']} - {value['points']}\n"
bot.send_message(message.chat.id, text)
@bot.message_handler()
def do_action(message: Message):
if message.reply_to_message:
if message.reply_to_message.from_user.username == get_self_name():
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"]}')
return
if info['state'] == "set_probability":
try:
value = int(message.text)
if value < 0 or value > 100:
bot.reply_to(message, "Число не попадает в диапозон от 0 до 100!")
else:
set_values(message.chat.id, probability=value, state="default")
bot.reply_to(message, "Ок! Установил")
except ValueError:
bot.reply_to(message, "Это не число!")
return
convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
if len(convert_text) > 0:
convert_text = convert_text[-1]
else:
return
ans = get_answers().get(convert_text)
if ans is not None and randrange(1, 101) <= info["probability"]:
bot.reply_to(message, ans)
mongo.inc(message.from_user.username, message.chat.id)
arg = sys.argv[-1]
if arg == 'bot':
bot.polling()
elif arg == 'updater':
from updater import update
update()
if arg == 'poll':
from daemons import poll
daemon = poll.Daemon()
elif arg == 'worker':
from daemons import worker
daemon = worker.Daemon()
elif arg == 'mailbox':
from daemons import mailbox
daemon = mailbox.Daemon()
else:
from api import app
app.run(host="0.0.0.0", port=1238)
daemon.execute()

View File

@ -1,7 +1,4 @@
from functools import cached_property
import pymongo
import settings
@ -21,11 +18,11 @@ class Mongo:
def __getitem__(self, item):
return self.database.get_collection(item)
@cached_property
@property
def chats_collection(self):
return self["chats"]
@cached_property
@property
def counter_collection(self):
return self["counter"]

0
utils/__init__.py Normal file
View File

64
utils/queues.py Normal file
View File

@ -0,0 +1,64 @@
import json
import os
import requests
import time
stage = os.getenv("STAGE", 'local')
if stage == 'development':
QUEUES_URL = 'https://queues.develop.sprinthub.ru'
elif stage == 'production':
QUEUES_URL = 'https://queues.sprinthub.ru'
else:
QUEUES_URL = None
token = os.getenv('QUEUES_TOKEN')
class QueuesException(Exception):
...
class TasksHandlerMixin:
def poll(self):
while True:
if QUEUES_URL is None:
data = {'payload': json.loads(input('Input message: '))}
else:
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name, 'X-Queues-Token': token})
if response.status_code == 404:
time.sleep(0.2)
continue
if response.status_code == 403:
raise NotImplemented('QUEUE_TOKEN is incorrect')
data = response.json()
try:
self.process(data['payload'])
except Exception as exc:
print(f'Error processing message id={data["id"]}, payload={data["payload"]}, exc={exc}')
continue
if QUEUES_URL is None:
continue
try:
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': data['id']}, headers={'X-Queues-Token': token})
if resp.status_code != 202:
raise QueuesException
except:
print(f'Failed to finish task id={data["id"]}')
@property
def queue_name(self):
raise NotImplemented
def process(self, payload):
raise NotImplemented
def set_task(queue_name: str, payload: dict, seconds_to_execute: int, delay: int|None = None):
resp = requests.post(f'{QUEUES_URL}/api/v1/put', headers={'queue': queue_name, 'X-Queues-Token': token}, json={
'payload': payload,
'seconds_to_execute': seconds_to_execute,
'delay': delay,
})
if resp.status_code != 202:
raise QueuesException