welcomecenterbot/api/webhook.py

87 lines
2.8 KiB
Python
Raw Normal View History

2023-04-17 21:42:25 +00:00
from sanic import Sanic
from sanic.response import text
2023-04-24 06:14:35 +00:00
from tgbot.config import WEBHOOK, FEEDBACK_CHAT_ID, BUTTON_VOUCH
2023-04-23 16:54:58 +00:00
from tgbot.handlers.handle_feedback import handle_feedback, handle_answer
from tgbot.handlers.handle_members_change import handle_join, handle_left
from tgbot.handlers.handle_join_request import handle_join_request
from tgbot.handlers.handle_default import handle_default
from tgbot.handlers.command_my import handle_command_my
from tgbot.handlers.command_graph import handle_command_graph
from tgbot.handlers.callback_vouch import handle_button
from tgbot.handlers.callback_unlink import handle_unlink
2023-04-24 06:14:35 +00:00
from tgbot.api import register_webhook, send_message
2023-04-23 16:54:58 +00:00
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
@app.route('/', methods=["GET"])
async def register(req):
2023-04-23 16:54:58 +00:00
res = 'skipped'
2023-04-17 21:42:25 +00:00
if not app.config.REGISTERED:
r = register_webhook(WEBHOOK)
2023-04-23 16:54:58 +00:00
print(f'\n\t\t\tWEBHOOK REGISTERED:\n{r}')
2023-04-17 21:42:25 +00:00
app.config.REGISTERED = True
2023-04-23 16:54:58 +00:00
print(r)
res = 'ok'
return text(res)
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-04-24 06:14:35 +00:00
if 'text' in msg:
if msg['chat']['id'] == msg['from']['id']:
if msg['text'] == '/my':
handle_command_my(msg)
else:
handle_feedback(msg)
elif str(msg['chat']['id']) == FEEDBACK_CHAT_ID:
if 'reply_to_message' in msg:
handle_answer(msg)
elif msg['text'] == '/graph':
await handle_command_graph(msg)
2023-04-23 16:54:58 +00:00
else:
2023-04-24 06:14:35 +00:00
handle_default(msg)
2023-04-23 16:54:58 +00:00
elif 'new_chat_member' in msg:
handle_join(msg)
elif 'left_chat_member' in msg:
handle_left(msg)
2023-04-22 01:17:50 +00:00
else:
2023-04-24 06:14:35 +00:00
print('message without text')
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']
if data.startswith(BUTTON_VOUCH):
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'])
# wtf
else:
print('UNHANDLED EVENT')
2023-04-17 21:42:25 +00:00
except Exception:
import traceback
2023-04-24 06:14:35 +00:00
r = send_message(FEEDBACK_CHAT_ID, f'<pre>{traceback.format_exc()}</pre>')
2023-04-17 21:42:25 +00:00
traceback.print_exc()
return text('ok')