-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (99 loc) · 4.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import ujson
from starlette.applications import Starlette
from starlette.responses import Response
from starlette.routing import Mount, Route, WebSocketRoute
from starlette.staticfiles import StaticFiles
from telebot.types import Update, Message, ReplyKeyboardRemove
from functions.ai_talk import AiTalk
from functions.ai_upscale import register_ai_upscale_handler
from functions.books import register_books_handler
from functions.chat_cmd import register_chat_handler
from functions.chess import chess_mp_endpoint, get_chess_games
from functions.photo_desc import register_photo_desc_handler
from functions.simple_cmds import init_simple_commands
from functions.voice_msg import register_voice_msg_handler
from functions.casino import register_casino_handler
from functions.weather import register_weather_handler
from functions.cities_game import register_cities_handler
from helpers import config, session_manager
from helpers.bot import bot
from helpers.db import BotDB
from helpers.gpts.gpts_apis import ChatGPT, GigaChat
from helpers.middleware import ChatManagement
from helpers.timer import timer
bot.setup_middleware(ChatManagement())
# from helpers.timer import timer
# @bot.message_handler(content_types=["user_shared"])
# def user_id_handler(msg: Message):
# bot.send_message(msg.chat.id, msg.user_shared.user_id)
#
#
# @bot.message_handler(content_types=["chat_shared"])
# def chat_id_handler(msg: Message):
# bot.send_message(msg.chat.id, msg.chat_shared.chat_id)
#
chatgpt = ChatGPT(config.openai_key)
gigachat = GigaChat(config.gigachat_secret)
init_simple_commands()
register_voice_msg_handler()
register_photo_desc_handler()
register_ai_upscale_handler()
register_books_handler()
register_chat_handler()
@bot.message_handler(['cancel'], state='*')
async def command_cancel(msg: Message):
await bot.send_message(msg.chat.id, "<b>Всё отменяю</b>", reply_markup=ReplyKeyboardRemove())
await BotDB.set_state(msg.chat.id, -1)
register_cities_handler()
register_weather_handler()
register_casino_handler()
ai_talk_inst = AiTalk(chatgpt, gigachat)
bot.register_message_handler(ai_talk_inst.start_ai_talk_listener)
# @bot.message_handler(content_types=content_type_media)
# async def chatting(msg: Message):
# if state == "wait_for_book_name":
# book_name_upload_state(msg)
# return
# elif state == "wait_for_book" or state == "wait_for_done":
# book_upload_state(msg)
# return
# elif state == "wait_for_pub":
# wait_user_upload_state(msg, state)
# return TODO
# @bot.callback_query_handler(None)
# def query(call: CallbackQuery):
# elif data.startswith("btn_upload"):
# book_upload_button(data, call)
# elif data.startswith("btn_ignore"):
# db.stats.ignore.append(int(data[data.rfind("_") + 1:]))
# bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id)
async def webhook_endpoint(request):
if request.headers.get('X-Telegram-Bot-Api-Secret-Token') != config.webhook_token:
return Response(status_code=403)
BotDB.loop.create_task(bot.process_new_updates([Update.de_json(ujson.loads(await request.body()))]))
return Response()
async def shutdown():
await bot.close_session()
if BotDB.pool is not None:
await BotDB.pool.clear()
await session_manager.close_all_sessions()
print("server stopped")
async def set_webhook():
await bot.set_webhook(url=config.web_url + 'tg_webhook', secret_token=config.webhook_token, max_connections=1000)
print("webhook set")
routes = [
Route('/tg_webhook', webhook_endpoint, methods=['POST']),
Route('/chess_games', get_chess_games, methods=['GET']),
WebSocketRoute('/cmp', chess_mp_endpoint), # chess multiplayer
Mount('/', StaticFiles(directory='website', html=True))
]
app = Starlette(routes=routes, on_shutdown=[shutdown])
# Запуск бота
if config.is_dev:
BotDB.loop.create_task(bot.delete_webhook())
BotDB.loop.create_task(bot.infinity_polling(skip_pending=True))
print("polling started")
else:
BotDB.loop.create_task(set_webhook())
print("server started")
BotDB.loop.create_task(timer())