-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.py
86 lines (68 loc) · 2.96 KB
/
webhook.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
from aiogram import Dispatcher, types
from aiohttp import web
from asyncio import get_event_loop
def webhook_pooling(
dp: Dispatcher = None,
port: int | str = None,
link: str = None,
loop = None,
admin_list: list | int | str = None,
startup_message: str = 'Бот был запущен! ☠️ ❱ 👾 ❱ 🤖',
shutdown_message: str = 'Бот был выключен. 🤖 ❱ 👾 ❱ ☠️'
):
# Create a bot instance with the provided token
if loop is None:
loop = get_event_loop()
bot = dp.bot
token = bot._token
# Create an aiohttp web application
app = web.Application()
# Construct the webhook path using the provided link and token
webhook_path = f'{link}/{token}'
print(webhook_path)
# Add a POST route to handle incoming webhooks
app.router.add_post(f'/{token}', lambda request: handle_webhook(request, token, dp))
# Register the on_startup and on_shutdown handlers
app.on_startup.append(lambda _: on_startup(dp, startup_message, admin_list, webhook_path))
app.on_shutdown.append(lambda _: on_shutdown(dp, shutdown_message, admin_list))
# Run the web application
web.run_app(
app,
host='0.0.0.0',
port=port,
loop=loop
)
async def handle_webhook(request, token, dp):
# Extract the token from the URL
url = str(request.url)
index = url.rfind('/')
token_ = url[index + 1:]
# Verify if the extracted token matches the provided token
if token_ == token:
# Process the incoming update using the Dispatcher
update = types.Update(**await request.json())
await dp.process_update(update)
# Return a success response
return web.Response()
else:
# Return a forbidden response if the tokens do not match
return web.Response(status=403)
async def start_shutdown(bot, text: str = None, admin_list: tuple | set | list | str | int = None):
# Check if the text and admin_list parameters are provided
if text is not None and admin_list is not None:
# Check the type of admin_list and send a message accordingly
if isinstance(admin_list, (tuple, set, list)):
for admin_id in admin_list:
await bot.send_message(chat_id=admin_id, text=text)
elif isinstance(admin_list, (str, int)):
await bot.send_message(chat_id=admin_list, text=text)
async def on_startup(dp, startup_message, admin_list, webhook_path):
# Set the webhook path for the bot
await dp.bot.set_webhook(webhook_path)
# Send the startup message to the specified admin_list
pass
# await start_shutdown(dp.bot, startup_message, admin_list)
async def on_shutdown(dp, shutdown_message, admin_list):
# Send the shutdown message to the specified admin_list
pass
# await start_shutdown(dp.bot, shutdown_message, admin_list)