From e088a27b5633b5aa81c22399b95e5403cad20ec4 Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sun, 31 May 2020 20:50:18 +0300 Subject: [PATCH 1/2] Refact and add ssl context Current version didnt work from box. My commit include: refact settings: - add ssl settings - add webhook_port to webhook_url - refact webserversettings refact on_startup: - add logging info on startup - add check for bad webhook url - add ssl sert to set_webhook __main__: - add generate ssl context - add ssl context in start_webhook --- examples/webhook_example.py | 44 ++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/webhook_example.py b/examples/webhook_example.py index 6efa8767..464d301f 100644 --- a/examples/webhook_example.py +++ b/examples/webhook_example.py @@ -1,4 +1,5 @@ import logging +import ssl from aiogram import Bot, types from aiogram.contrib.middlewares.logging import LoggingMiddleware @@ -7,16 +8,20 @@ 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 # Ports currently supported for Webhooks: 443, 80, 88, 8443 + +# 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_PATH = '/bot' +WEBHOOK_URL = f'{WEBHOOK_HOST}:{WEBAPP_PORT}{WEBHOOK_PATH}' logging.basicConfig(level=logging.INFO) @@ -35,18 +40,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 +67,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 +80,5 @@ if __name__ == '__main__': skip_updates=True, host=WEBAPP_HOST, port=WEBAPP_PORT, + ssl_context=context ) From d297dafd46c81bf7195cd85aeda1e125a42e9110 Mon Sep 17 00:00:00 2001 From: Stanislav Semenov Date: Sun, 31 May 2020 22:23:40 +0300 Subject: [PATCH 2/2] Update webhook_example.py --- examples/webhook_example.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/webhook_example.py b/examples/webhook_example.py index 464d301f..d2e22dd4 100644 --- a/examples/webhook_example.py +++ b/examples/webhook_example.py @@ -12,7 +12,7 @@ API_TOKEN = 'Your bot token' # webserver settings WEBAPP_HOST = 'localhost' # or 0.0.0.0 -WEBAPP_PORT = 88 # Ports currently supported for Webhooks: 443, 80, 88, 8443 +WEBAPP_PORT = 88 # SSL settings WEBHOOK_SSL_CERT = 'path_to_cert.pem' @@ -20,8 +20,9 @@ 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}:{WEBAPP_PORT}{WEBHOOK_PATH}' +WEBHOOK_URL = f'{WEBHOOK_HOST}:{WEBHOOK_PORT}{WEBHOOK_PATH}' logging.basicConfig(level=logging.INFO)