ruz-bot/daemons/bot.py
2023-10-11 19:08:09 +03:00

47 lines
1.2 KiB
Python

import os
import telebot
from cachetools import TTLCache
from requests import get
from telebot.types import Message
import settings
from helpers.mongo import mongo
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
STAFF_CACHE = TTLCache(100, 60)
def is_staff(telegram_id):
print('there')
staff_data = STAFF_CACHE.get(telegram_id)
if staff_data is None:
staff_data = get(
'https://platform.sprinthub.ru/is_staff',
headers={'X-Security-Token': settings.PLATFORM_SECURITY_TOKEN},
params={
'telegram_id': telegram_id
}
)
if staff_data.status_code != 200:
return False
staff_data = staff_data.json()['is_staff']
STAFF_CACHE[telegram_id] = staff_data
return staff_data
@bot.message_handler(commands=['start'])
def on_start(message: Message):
mongo.users_collection.delete_many({"chat_id": message.chat.id})
do_action(message)
@bot.message_handler()
def do_action(message: Message):
if settings.STAGE == 'development' and not is_staff(message.chat.id):
return
from helpers.answer import Answer
Answer(message).process()