welcomecenterbot/api/webhook.py

94 lines
3.3 KiB
Python
Raw Normal View History

2023-04-17 21:42:25 +00:00
from sanic import Sanic
from sanic.response import text
2023-09-11 15:52:35 +00:00
from bot.handlers.handle_feedback import handle_feedback, handle_answer
from bot.handlers.handle_members_change import handle_join, handle_left
from bot.handlers.handle_join_request import handle_join_request
from bot.handlers.handle_default import handle_default
from bot.handlers.command_my import handle_command_my
from bot.handlers.command_graph import handle_command_graph
from bot.handlers.command_ask import handle_command_ask
from bot.handlers.callback_vouch import handle_button
from bot.handlers.callback_unlink import handle_unlink
from bot.handlers.handle_startup import handle_startup
from bot.api import register_webhook, send_message
from bot.config import FEEDBACK_CHAT_ID
2023-09-11 17:02:29 +00:00
from bot.state import State
2023-04-17 21:42:25 +00:00
2023-04-23 17:04:13 +00:00
app = Sanic(name="welcomecenter")
2023-04-17 21:42:25 +00:00
app.config.REGISTERED = False
2023-09-11 17:02:29 +00:00
state = State()
2023-04-17 21:42:25 +00:00
2023-09-06 10:08:05 +00:00
@app.get('/')
2023-04-17 21:42:25 +00:00
async def register(req):
if not app.config.REGISTERED:
2023-09-06 10:08:05 +00:00
print(register_webhook())
2023-04-17 21:42:25 +00:00
app.config.REGISTERED = True
2023-04-28 12:24:14 +00:00
handle_startup()
2023-09-06 10:08:05 +00:00
return text('ok')
2023-04-23 16:54:58 +00:00
2023-04-17 21:42:25 +00:00
@app.post('/')
async def handle(req):
print(req)
try:
update = req.json
print(update)
2023-04-23 16:54:58 +00:00
# видимые сообщения
msg = update.get('message', update.get('edited_message'))
if msg:
2023-09-11 17:02:29 +00:00
msg['edit'] = 'edited_message' in update
if msg['chat']['id'] == msg['from']['id']:
# сообщения в личке с ботом
print('private chat message')
if msg['text'].startswith('/my'):
handle_command_my(msg)
elif msg['text'].startswith('/unlink'):
handle_unlink(msg)
2023-04-23 16:54:58 +00:00
else:
2023-09-11 17:02:29 +00:00
handle_feedback(msg, state)
elif str(msg['chat']['id']) == FEEDBACK_CHAT_ID:
# сообщения из группы обратной связи
print('feedback chat message')
if 'reply_to_message' in msg:
handle_answer(msg)
elif msg['text'] == '/graph':
await handle_command_graph(msg)
elif msg['text'].startswith('/ask'):
handle_command_ask(msg)
2023-04-22 01:17:50 +00:00
else:
2023-09-11 17:02:29 +00:00
# сообщения из всех остальных групп
cid = msg['chat']['id']
print(f'group {cid} chat message')
if 'text' in msg:
handle_default(msg)
elif 'new_chat_member' in msg:
handle_join(msg)
elif 'left_chat_member' in msg:
handle_left(msg)
2023-04-23 16:54:58 +00:00
# кнопки
2023-04-22 01:17:50 +00:00
elif 'callback_query' in update:
2023-04-23 16:54:58 +00:00
data = update['callback_query']['data']
2023-04-28 12:24:14 +00:00
if data.startswith('vouch'):
2023-04-23 16:54:58 +00:00
handle_button(update['callback_query'])
elif data.startswith('unlink'):
handle_unlink(update['callback_query'])
# заявки
2023-04-22 01:17:50 +00:00
elif 'chat_join_request' in update:
print('chat join request')
2023-04-23 16:54:58 +00:00
handle_join_request(update['chat_join_request'])
2023-04-17 21:42:25 +00:00
except Exception:
import traceback
2023-04-28 12:24:14 +00:00
r = send_message(FEEDBACK_CHAT_ID, f'<pre>\n{traceback.format_exc()}\n</pre>')
2023-04-17 21:42:25 +00:00
traceback.print_exc()
return text('ok')