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() 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() else: from api import app app.run(host="0.0.0.0", port=1238)