Advertisement
TheAspirational

aiohookexample

Jul 23rd, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import asyncio
  2. import logging
  3. import ssl
  4.  
  5. from aiogram import Bot, types
  6. from aiogram.dispatcher import Dispatcher
  7. from aiogram.utils.executor import start_webhook
  8.  
  9. API_TOKEN = 'token'
  10.  
  11. # webhook settings
  12. WEBHOOK_HOST = '0.0.0.0' #если при создании сертификата был указан домен то он должен совпадать
  13.  
  14. WEBHOOK_PORT =  8443  # 443, 80, 88 or 8443 (port need to be 'open')
  15.  
  16. WEBHOOK_SSL_CERT = '/yours_server/cert/YOURPUBLIC.pem'  # Path to the ssl certificate
  17. WEBHOOK_SSL_PRIV = '/yours_server/cert/YOURPRIVATE.key'  # Path to the ssl private key
  18.  
  19. WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
  20. WEBHOOK_URL_PATH = "/{}/".format(API_TOKEN)
  21.  
  22. logging.basicConfig(level=logging.INFO)
  23.  
  24. bot = Bot(token=API_TOKEN)
  25. dp = Dispatcher(bot)
  26.  
  27. context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
  28. context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)
  29.  
  30. @dp.message_handler()
  31. async def echo(message: types.Message):
  32.     await bot.send_message(message.chat.id, message.text)
  33.        
  34. async def on_startup(dp):
  35.     await bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
  36.                 certificate=open(WEBHOOK_SSL_CERT, 'r'))
  37.  
  38. async def on_shutdown(dp):
  39.     pass
  40.  
  41. if __name__ == '__main__':
  42.     start_webhook(dispatcher=dp, webhook_path=WEBHOOK_URL_PATH,
  43.                   on_startup=on_startup, on_shutdown=on_shutdown,
  44.                   skip_updates=True, host=WEBHOOK_HOST, port=WEBHOOK_PORT,
  45.                   ssl_context=context,)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement