mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Create webhook_example_2.py
This commit is contained in:
parent
3f8034ad90
commit
7f588082b9
1 changed files with 45 additions and 0 deletions
45
examples/webhook_example_2.py
Normal file
45
examples/webhook_example_2.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from aiogram import Bot, types
|
||||||
|
from aiogram.dispatcher import Dispatcher
|
||||||
|
from aiogram.utils.executor import start_webhook
|
||||||
|
|
||||||
|
API_TOKEN = 'BOT TOKEN HERE'
|
||||||
|
|
||||||
|
# webhook settings
|
||||||
|
WEBHOOK_HOST = 'https://your.domain'
|
||||||
|
WEBHOOK_PATH = '/path/to/api'
|
||||||
|
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
|
||||||
|
|
||||||
|
# webserver settings
|
||||||
|
WEBAPP_HOST = 'localhost' # or ip
|
||||||
|
WEBAPP_PORT = 3001
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
bot = Bot(token=API_TOKEN, loop=loop)
|
||||||
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler()
|
||||||
|
async def echo(message: types.Message):
|
||||||
|
await bot.send_message(message.chat.id, message.text)
|
||||||
|
|
||||||
|
|
||||||
|
async def on_startup(dp):
|
||||||
|
await bot.set_webhook(WEBHOOK_URL)
|
||||||
|
# insert code here to run it after start
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
async def on_shutdown(dp):
|
||||||
|
# insert code here to run it before shutdown
|
||||||
|
#
|
||||||
|
await bot.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
start_webhook(dispatcher=dp, webhook_path=WEBHOOK_PATH, on_startup=on_startup, on_shutdown=on_shutdown,
|
||||||
|
skip_updates=True, host=WEBAPP_HOST, port=WEBAPP_PORT)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue