Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. from aiogram import Bot, types
  2. from aiogram.bot import api
  3. from aiogram.contrib.middlewares.logging import LoggingMiddleware
  4. from aiogram.dispatcher import Dispatcher
  5. from aiogram.dispatcher.webhook import SendMessage
  6. from aiogram.utils.executor import start_webhook
  7. import logging
  8.  
  9. API_TOKEN = "Токен"
  10.  
  11. # webhook settings
  12. WEBHOOK_HOST = 'IP'
  13. WEBHOOK_PATH = ''
  14. WEBHOOK_URL = f"https://{WEBHOOK_HOST}{WEBHOOK_PATH}"
  15.  
  16. WEBHOOK_SSL_CERT = '/etc/nginx/ssl/nginx.crt' # Path to the ssl certificate
  17. WEBHOOK_SSL_PRIV = '/etc/nginx/ssl/nginx.key' # Path to the ssl private key
  18.  
  19. # webserver settings
  20. WEBAPP_HOST = 'IP' # or ip
  21. WEBAPP_PORT = 8686
  22.  
  23.  
  24. logging.basicConfig(level=logging.INFO)
  25.  
  26. bot = Bot(token=API_TOKEN)
  27. dp = Dispatcher(bot)
  28. dp.middleware.setup(LoggingMiddleware())
  29.  
  30.  
  31. @dp.message_handler()
  32. async def echo(message: types.Message):
  33. # Regular request
  34. # await bot.send_message(message.chat.id, message.text)
  35.  
  36. # or reply INTO webhook
  37. return SendMessage(message.chat.id, message.text)
  38.  
  39.  
  40. async def on_startup(dp):
  41. await bot.set_webhook(WEBHOOK_URL, certificate=open(WEBHOOK_SSL_CERT, 'rb'))
  42. # insert code here to run it after start
  43.  
  44.  
  45. async def on_shutdown(dp):
  46. logging.warning('Shutting down..')
  47.  
  48. # insert code here to run it before shutdown
  49.  
  50. # Remove webhook (not acceptable in some cases)
  51. await bot.delete_webhook()
  52.  
  53. # Close DB connection (if used)
  54. await dp.storage.close()
  55. await dp.storage.wait_closed()
  56.  
  57. logging.warning('Bye!')
  58.  
  59.  
  60. if __name__ == '__main__':
  61. start_webhook(
  62. dispatcher=dp,
  63. webhook_path=WEBHOOK_PATH,
  64. on_startup=on_startup,
  65. on_shutdown=on_shutdown,
  66. skip_updates=True,
  67. host=WEBAPP_HOST,
  68. port=WEBAPP_PORT,
  69. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement