platform
This commit is contained in:
parent
6fda21ed9e
commit
ef296a599c
119
main.py
119
main.py
@ -3,129 +3,40 @@ import sys
|
|||||||
from random import randrange, choice
|
from random import randrange, choice
|
||||||
|
|
||||||
import telebot
|
import telebot
|
||||||
from cachetools import TTLCache
|
|
||||||
from requests import get
|
|
||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
from mongo import mongo
|
from mongo import mongo
|
||||||
|
from platform import PlatformClient
|
||||||
from storage import set_values, get_chat_info
|
from storage import set_values, get_chat_info
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_ANSWERS = {
|
|
||||||
"a": "Хуй на!",
|
|
||||||
"da": "Pizda!",
|
|
||||||
"а": "Хуй на!",
|
|
||||||
"200": "Отсоси на месте!",
|
|
||||||
"300": "Отсоси у тракториста!",
|
|
||||||
"dа": "Pizda!",
|
|
||||||
"gde": "V pizde!",
|
|
||||||
"net": "Pidora otvet!",
|
|
||||||
"дa": "Пизда!",
|
|
||||||
"чe": "Хуй через плечо!",
|
|
||||||
"чo": "Хуй через плечо!",
|
|
||||||
"gdе": "V pizde!",
|
|
||||||
"heт": "Пидора ответ!",
|
|
||||||
"nеt": "Pidora otvet!",
|
|
||||||
"да": "Пизда!",
|
|
||||||
"че": "Хуй через плечо!",
|
|
||||||
"чо": "Хуй через плечо!",
|
|
||||||
"чё": "Хуй через плечо!",
|
|
||||||
"hет": "Пидора ответ!",
|
|
||||||
"гдe": "В пизде!",
|
|
||||||
"нeт": "Пидора ответ!",
|
|
||||||
"ага": "В жопе нога!",
|
|
||||||
"где": "В пизде!",
|
|
||||||
"как": "Жопой об косяк!",
|
|
||||||
"кто": "Конь в пальто!",
|
|
||||||
"нет": "Пидора ответ!",
|
|
||||||
"ога": "В жопе нога!",
|
|
||||||
"опа": "Срослась пизда и жопа!",
|
|
||||||
"угу": "Иди в пизду!",
|
|
||||||
"слыш": "За углом поссышь!",
|
|
||||||
"ладно": "Прохладно!",
|
|
||||||
"слышь": "За углом поссышь!",
|
|
||||||
"двести": "Отсоси на месте!",
|
|
||||||
"триста": "Отсоси у тракториста!",
|
|
||||||
"здрасте": "Пизду покрасьте!",
|
|
||||||
"здрасьте": "Пизду покрасьте!"
|
|
||||||
}
|
|
||||||
|
|
||||||
DEFAULT_REPLIES = [
|
|
||||||
"Пошел нахуй!",
|
|
||||||
"Я твою маму ебал!",
|
|
||||||
"Ты пидор!",
|
|
||||||
]
|
|
||||||
|
|
||||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||||
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
||||||
stage = os.getenv("STAGE")
|
stage = os.getenv("STAGE", 'local')
|
||||||
|
|
||||||
|
|
||||||
|
platform = PlatformClient(
|
||||||
|
security_token,
|
||||||
|
'Pizda Bot',
|
||||||
|
stage,
|
||||||
|
['constants', 'answers', 'replies'],
|
||||||
|
[],
|
||||||
|
need_poll=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
|
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
|
||||||
|
|
||||||
|
|
||||||
CACHE_CONFIG = TTLCache(1, 60)
|
|
||||||
SELF_NAME = None
|
|
||||||
|
|
||||||
|
|
||||||
def get_self_name():
|
def get_self_name():
|
||||||
global SELF_NAME
|
return platform.get_config('constants')['self_name']
|
||||||
if SELF_NAME:
|
|
||||||
return SELF_NAME
|
|
||||||
answers_response = get(
|
|
||||||
'https://platform.sprinthub.ru/configs/get',
|
|
||||||
headers={'X-Security-Token': security_token},
|
|
||||||
params={
|
|
||||||
'project': "Pizda Bot",
|
|
||||||
'stage': stage,
|
|
||||||
'name': "constants"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if answers_response.status_code != 200:
|
|
||||||
print("ERROR")
|
|
||||||
return 'answer_da_bot'
|
|
||||||
SELF_NAME = answers_response.json()['self_name']
|
|
||||||
return SELF_NAME
|
|
||||||
|
|
||||||
|
|
||||||
def get_answers():
|
def get_answers():
|
||||||
answers = CACHE_CONFIG.get('answers')
|
return platform.get_config('answers')
|
||||||
if answers is None:
|
|
||||||
answers_response = get(
|
|
||||||
'https://platform.sprinthub.ru/configs/get',
|
|
||||||
headers={'X-Security-Token': security_token},
|
|
||||||
params={
|
|
||||||
'project': "Pizda Bot",
|
|
||||||
'stage': stage,
|
|
||||||
'name': "answers"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if answers_response.status_code != 200:
|
|
||||||
print("ERROR")
|
|
||||||
return DEFAULT_ANSWERS
|
|
||||||
answers = answers_response.json()
|
|
||||||
CACHE_CONFIG['answers'] = answers
|
|
||||||
return answers
|
|
||||||
|
|
||||||
|
|
||||||
def get_replies():
|
def get_replies():
|
||||||
answers = CACHE_CONFIG.get('replies')
|
return platform.get_config('replies')
|
||||||
if answers is None:
|
|
||||||
answers_response = get(
|
|
||||||
'https://platform.sprinthub.ru/configs/get',
|
|
||||||
headers={'X-Security-Token': security_token},
|
|
||||||
params={
|
|
||||||
'project': "Pizda Bot",
|
|
||||||
'stage': stage,
|
|
||||||
'name': "replies"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if answers_response.status_code != 200:
|
|
||||||
print("ERROR")
|
|
||||||
return DEFAULT_REPLIES
|
|
||||||
answers = answers_response.json()
|
|
||||||
CACHE_CONFIG['answers'] = answers
|
|
||||||
return answers
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['setprobability'])
|
@bot.message_handler(commands=['setprobability'])
|
||||||
|
105
platform.py
Normal file
105
platform.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
import json
|
||||||
|
import typing
|
||||||
|
import urllib.parse
|
||||||
|
from threading import Thread
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
from requests import get
|
||||||
|
|
||||||
|
|
||||||
|
class PlatformClient:
|
||||||
|
def __init__(self, platform_security_token: str, app_name: str, stage: str, configs: typing.List[str], experiments: typing.List[str], need_poll: bool = True):
|
||||||
|
self.platform_security_token = platform_security_token
|
||||||
|
self.app_name = app_name
|
||||||
|
self.stage = stage
|
||||||
|
self.configs = configs
|
||||||
|
self.experiments = experiments
|
||||||
|
self.endpoint = 'https://platform.sprinthub.ru/'
|
||||||
|
self.configs_url = urllib.parse.urljoin(self.endpoint, 'configs/get')
|
||||||
|
self.experiments_url = urllib.parse.urljoin(self.endpoint, 'experiments/get')
|
||||||
|
self.staff_url = urllib.parse.urljoin(self.endpoint, 'is_staff')
|
||||||
|
self.config_storage = {}
|
||||||
|
self.experiment_storage = {}
|
||||||
|
self.poll_data()
|
||||||
|
if need_poll:
|
||||||
|
self.poll_data_in_thread()
|
||||||
|
|
||||||
|
def poll_data_in_thread(self):
|
||||||
|
def inner():
|
||||||
|
while True:
|
||||||
|
sleep(30)
|
||||||
|
self.fetch_configs()
|
||||||
|
self.fetch_experiments()
|
||||||
|
|
||||||
|
Thread(target=inner).start()
|
||||||
|
|
||||||
|
def poll_data(self):
|
||||||
|
self.fetch_configs(with_exception=True)
|
||||||
|
self.fetch_experiments(with_exception=True)
|
||||||
|
|
||||||
|
def request_with_retries(self, url, params, with_exception=False, retries_count=3):
|
||||||
|
exception_to_throw = None
|
||||||
|
for _ in range(retries_count):
|
||||||
|
try:
|
||||||
|
response = get(
|
||||||
|
url,
|
||||||
|
headers={'X-Security-Token': self.platform_security_token},
|
||||||
|
params=params
|
||||||
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
print(f'Failed to request {url}, status_code={response.status_code}')
|
||||||
|
exception_to_throw = Exception('Not 200 status')
|
||||||
|
except Exception as exc:
|
||||||
|
print(exc)
|
||||||
|
exception_to_throw = exc
|
||||||
|
sleep(1)
|
||||||
|
print(f'Failed fetching with retries: {url}, {params}')
|
||||||
|
if with_exception:
|
||||||
|
raise exception_to_throw
|
||||||
|
|
||||||
|
def fetch_configs(self, with_exception=False):
|
||||||
|
if self.stage == 'local':
|
||||||
|
local_platform = json.loads(open('local_platform.json', 'r').read())
|
||||||
|
self.config_storage = local_platform['configs']
|
||||||
|
return
|
||||||
|
for config in self.configs:
|
||||||
|
response_data = self.request_with_retries(self.configs_url, {
|
||||||
|
'project': self.app_name,
|
||||||
|
'stage': self.stage,
|
||||||
|
'name': config
|
||||||
|
}, with_exception)
|
||||||
|
self.config_storage[config] = response_data
|
||||||
|
|
||||||
|
def fetch_experiments(self, with_exception=False):
|
||||||
|
if self.stage == 'local':
|
||||||
|
local_platform = json.loads(open('local_platform.json', 'r').read())
|
||||||
|
self.experiment_storage = local_platform['experiments']
|
||||||
|
return
|
||||||
|
for experiment in self.experiments:
|
||||||
|
response_data = self.request_with_retries(self.experiments_url, {
|
||||||
|
'project': self.app_name,
|
||||||
|
'stage': self.stage,
|
||||||
|
'name': experiment
|
||||||
|
}, with_exception)
|
||||||
|
self.experiment_storage[experiment] = response_data
|
||||||
|
|
||||||
|
def is_staff(self, **kwargs):
|
||||||
|
if self.stage == 'local':
|
||||||
|
local_platform = json.loads(open('local_platform.json', 'r').read())
|
||||||
|
local_staff = local_platform['staff']
|
||||||
|
for element in local_staff:
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
if element[key] == value:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
response_data = self.request_with_retries(self.staff_url, kwargs)
|
||||||
|
if response_data is None:
|
||||||
|
return False
|
||||||
|
return response_data['is_staff']
|
||||||
|
|
||||||
|
def get_config(self, name):
|
||||||
|
return self.config_storage[name]
|
||||||
|
|
||||||
|
def get_experiment(self, name):
|
||||||
|
return self.experiment_storage[name]
|
Loading…
Reference in New Issue
Block a user