mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge d297dafd46 into 613cfb8df0
This commit is contained in:
commit
aea803197e
1 changed files with 32 additions and 13 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
import ssl
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
||||
|
|
@ -7,16 +8,21 @@ from aiogram.dispatcher.webhook import SendMessage
|
|||
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}"
|
||||
API_TOKEN = 'Your bot token'
|
||||
|
||||
# webserver settings
|
||||
WEBAPP_HOST = 'localhost' # or ip
|
||||
WEBAPP_PORT = 3001
|
||||
WEBAPP_HOST = 'localhost' # or 0.0.0.0
|
||||
WEBAPP_PORT = 88
|
||||
|
||||
# SSL settings
|
||||
WEBHOOK_SSL_CERT = 'path_to_cert.pem'
|
||||
WEBHOOK_SSL_PRIV = 'path_to_private.key'
|
||||
|
||||
# webhook settings
|
||||
WEBHOOK_HOST = 'XX.XX.XX.XX' # IP your host
|
||||
WEBHOOK_PORT = 88 # Ports currently supported for Webhooks: 443, 80, 88, 8443
|
||||
WEBHOOK_PATH = '/bot'
|
||||
WEBHOOK_URL = f'{WEBHOOK_HOST}:{WEBHOOK_PORT}{WEBHOOK_PATH}'
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
|
@ -35,18 +41,25 @@ async def echo(message: types.Message):
|
|||
|
||||
|
||||
async def on_startup(dp):
|
||||
await bot.set_webhook(WEBHOOK_URL)
|
||||
logging.warning('Webhook startup..')
|
||||
|
||||
# Check webhook
|
||||
webhook = await bot.get_webhook_info()
|
||||
|
||||
# If webhook URL doesnt match current - remove webhook
|
||||
if webhook.url != WEBHOOK_URL:
|
||||
await bot.delete_webhook()
|
||||
|
||||
# Set webhook
|
||||
await bot.set_webhook(WEBHOOK_URL, certificate=open(WEBHOOK_SSL_CERT, 'rb').read())
|
||||
# insert code here to run it after start
|
||||
|
||||
|
||||
async def on_shutdown(dp):
|
||||
logging.warning('Shutting down..')
|
||||
|
||||
await bot.close()
|
||||
# insert code here to run it before shutdown
|
||||
|
||||
# Remove webhook (not acceptable in some cases)
|
||||
await bot.delete_webhook()
|
||||
|
||||
# Close DB connection (if used)
|
||||
await dp.storage.close()
|
||||
await dp.storage.wait_closed()
|
||||
|
|
@ -55,6 +68,11 @@ async def on_shutdown(dp):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# Generate SSL context.
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
|
||||
context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)
|
||||
|
||||
start_webhook(
|
||||
dispatcher=dp,
|
||||
webhook_path=WEBHOOK_PATH,
|
||||
|
|
@ -63,4 +81,5 @@ if __name__ == '__main__':
|
|||
skip_updates=True,
|
||||
host=WEBAPP_HOST,
|
||||
port=WEBAPP_PORT,
|
||||
ssl_context=context
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue