Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from aiogram import Bot, types
- from aiogram.bot import api
- from aiogram.contrib.middlewares.logging import LoggingMiddleware
- from aiogram.dispatcher import Dispatcher
- from aiogram.dispatcher.webhook import SendMessage
- from aiogram.utils.executor import start_webhook
- import logging
- API_TOKEN = "Токен"
- # webhook settings
- WEBHOOK_HOST = 'IP'
- WEBHOOK_PATH = ''
- WEBHOOK_URL = f"https://{WEBHOOK_HOST}{WEBHOOK_PATH}"
- WEBHOOK_SSL_CERT = '/etc/nginx/ssl/nginx.crt' # Path to the ssl certificate
- WEBHOOK_SSL_PRIV = '/etc/nginx/ssl/nginx.key' # Path to the ssl private key
- # webserver settings
- WEBAPP_HOST = 'IP' # or ip
- WEBAPP_PORT = 8686
- logging.basicConfig(level=logging.INFO)
- bot = Bot(token=API_TOKEN)
- dp = Dispatcher(bot)
- dp.middleware.setup(LoggingMiddleware())
- @dp.message_handler()
- async def echo(message: types.Message):
- # Regular request
- # await bot.send_message(message.chat.id, message.text)
- # or reply INTO webhook
- return SendMessage(message.chat.id, message.text)
- async def on_startup(dp):
- await bot.set_webhook(WEBHOOK_URL, certificate=open(WEBHOOK_SSL_CERT, 'rb'))
- # insert code here to run it after start
- async def on_shutdown(dp):
- logging.warning('Shutting down..')
- # 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()
- logging.warning('Bye!')
- 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,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement