probability

This commit is contained in:
Administrator 2022-12-17 16:53:03 +03:00
parent 3e210b19a6
commit 7c0c0f1b9d
6 changed files with 107 additions and 4 deletions

View File

@ -7,6 +7,8 @@ services:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
MONGO_HOST: "mongo.develop.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:

View File

@ -7,6 +7,8 @@ services:
image: mathwave/sprint-repo:pizda-bot image: mathwave/sprint-repo:pizda-bot
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:

69
main.py
View File

@ -1,8 +1,12 @@
import os import os
from random import randrange
import telebot import telebot
from cachetools import TTLCache
from telebot.types import Message from telebot.types import Message
import settings
from mongo import mongo
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN")) bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@ -14,21 +18,78 @@ answers_net_rus = {"нет", "нeт"}
answers_net_eng = {"net", "nеt"} answers_net_eng = {"net", "nеt"}
cache = TTLCache(settings.CACHE_SIZE, settings.CACHE_TTL)
def get_chat_info(chat_id: int) -> dict:
cached_info = cache.get(chat_id)
if cached_info is not None:
return cached_info
mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id})
if mongo_info is not None:
cache[chat_id] = mongo_info
return mongo_info
chat_info = {"chat_id": chat_id, "state": "default", "probability": 100}
mongo.chats_collection.insert_one(chat_info)
cache[chat_id] = chat_info
return chat_info
def set_values(chat_id: int, **values):
cached_info = cache.get(chat_id)
if cached_info is None:
mongo_info = mongo.chats_collection.find_one({"chat_id": chat_id})
if mongo_info is None:
chat_info = {"chat_id": chat_id, "state": "default", "probability": 100}
chat_info.update(values)
mongo.chats_collection.insert_one(chat_info)
cache[chat_id] = chat_info
else:
mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values})
mongo_info = dict(mongo_info)
mongo_info.update(values)
cache[chat_id] = mongo_info
else:
cached_info.update(values)
mongo.chats_collection.update_one({"chat_id": chat_id}, {"$set": values})
@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() @bot.message_handler()
def do_action(message: Message): def do_action(message: Message):
info = get_chat_info(message.chat.id)
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() convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
if len(convert_text) > 0: if len(convert_text) > 0:
convert_text = convert_text[-1] convert_text = convert_text[-1]
else: else:
return return
ans = None
if convert_text in answers_rus: if convert_text in answers_rus:
bot.reply_to(message, "Пизда!") ans = "Пизда!"
if convert_text in answers_eng: if convert_text in answers_eng:
bot.reply_to(message, "Pizda!") ans = "Pizda!"
if convert_text in answers_net_rus: if convert_text in answers_net_rus:
bot.reply_to(message, "Пидора ответ!") ans = "Пидора ответ!"
if convert_text in answers_net_eng: if convert_text in answers_net_eng:
bot.reply_to(message, "Pidora otvet!") ans = "Pidora otvet!"
if ans is not None and randrange(1, 101) <= info["probability"]:
bot.reply_to(message, ans)
bot.polling() bot.polling()

25
mongo.py Normal file
View File

@ -0,0 +1,25 @@
from functools import cached_property
import pymongo
import settings
class Mongo:
def __init__(self):
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
self.client = pymongo.MongoClient(url)
self.database = self.client.get_database("pizda-bot")
self.chats_collection.create_index([
("chat_id", 1)
])
def __getitem__(self, item):
return self.database.get_collection(item)
@cached_property
def chats_collection(self):
return self["chats"]
mongo = Mongo()

View File

@ -1,6 +1,9 @@
cachetools==5.2.0
certifi==2022.12.7 certifi==2022.12.7
charset-normalizer==2.1.1 charset-normalizer==2.1.1
dnspython==2.2.1
idna==3.4 idna==3.4
pymongo==4.3.3
pyTelegramBotAPI==4.1.1 pyTelegramBotAPI==4.1.1
requests==2.28.1 requests==2.28.1
urllib3==1.26.13 urllib3==1.26.13

10
settings.py Normal file
View File

@ -0,0 +1,10 @@
import os
import sys
import zoneinfo
MONGO_USER = os.getenv("MONGO_USER", "mongo")
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000))
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))