def main(): executor.start_webhook(dp, config.WEBHOOK_PATH, on_startup=on_startup, on_shutdown=on_shutdown, check_ip=True, port=config.PORT)
def bot_register(webhook: bool = False) -> None: try: import app.handlers # noqa: F401 if SERVERLESS and webhook: executor.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, ) else: executor.start_polling( dp, skip_updates=True, on_startup=on_startup, on_shutdown=on_shutdown, ) return except KeyboardInterrupt: pass except Exception as e: logging.exception(e)
def webhook(): from aiogram import executor from app.handlers import dp executor.start_webhook(dp, webhook_path=WEBHOOK_URL, on_startup=on_startup, on_shutdown=on_shutdown)
def run(): loop = asyncio.get_event_loop() try: loop.run_until_complete(db.init(Config)) if Config.DEPLOY: executor.start_webhook(dp, webhook_path=Config.WEBHOOK_PATH, host=Config.WEBAPP_HOST, port=Config.WEBAPP_PORT) else: executor.start_polling(dp, skip_updates=True) finally: # Graceful shutdown loop.run_until_complete(db.shutdown())
def run(): try: dp.loop.run_until_complete(db.init(Config)) dp.loop.create_task(track_submissions(15)) if Config.DEPLOY: executor.start_webhook(dp, webhook_path=Config.WEBHOOK_PATH, host=Config.WEBAPP_HOST, port=Config.WEBAPP_PORT, skip_updates=True) else: executor.start_polling(dp, skip_updates=True) finally: # Graceful shutdown dp.loop.run_until_complete( asyncio.gather(db.shutdown(), timus.shutdown()))
logging.info(f"Wait 20 until MYSQL Starts... initialising MYSQL DATABASE") await sleep(20) with open("mysql_data/init.sql", "r") as file: command = file.read() if command: logging.info("Loaded SQL command") sql.execute(command) logging.info("Table created") if __name__ == '__main__': if Reviews_state: from bot.handlers.reviews import dp from bot.handlers.setup_gauth import dp from bot.handlers.bot_info import dp from bot.handlers.buttons import dp from bot.handlers.decoding import dp from bot.handlers.encoding import dp from bot.handlers.admin_panel import dp from bot.handlers.main_handlers import dp from bot.handlers.errors import dp if Webhook_state: executor.start_webhook(dispatcher=dp, webhook_path="", host=WEBAPP_HOST, port=WEBAPP_PORT, on_startup=on_startup) else: executor.start_polling(dp, on_startup=on_startup_polling)
import_module("AuraX.utils.db_structure_migrator") async def start(_): log.debug("Starting before serving task for all modules...") loop.create_task(before_srv_task(loop)) if not get_bool_key("DEBUG_MODE"): log.debug("Waiting 2 seconds...") await asyncio.sleep(2) async def start_webhooks(_): url = os.getenv("WEBHOOK_URL") + f"/{TOKEN}" await bot.set_webhook(url) return await start(_) log.info("Starting loop..") log.info("Aiogram: Using polling method") if os.getenv("WEBHOOKS", False): port = os.getenv("WEBHOOKS_PORT", 8080) executor.start_webhook(dp, f"/{TOKEN}", on_startup=start_webhooks, port=port) else: executor.start_polling(dp, loop=loop, on_startup=start)
# 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!') def webhook(update): dispatcher.process_update(update) if __name__ == '__main__': if USE_WEBHOOK == '1': executor.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, ) else: executor.start_polling(dp, skip_updates=True)
if CONFIG.debug_mode: import_module("hitsuki.utils.sentry") async def before_srv_task(loop): for module in [m for m in LOADED_MODULES if hasattr(m, '__before_serving__')]: log.debug('Before serving: ' + module.__name__) loop.create_task(module.__before_serving__(loop)) async def start(_): log.debug("Starting before serving task for all modules...") loop.create_task(before_srv_task(loop)) if CONFIG.debug_mode: log.debug("Waiting 2 seconds...") await asyncio.sleep(2) async def start_webhooks(_): await bot.set_webhook(CONFIG.webhooks_url + f"/{CONFIG.token}") return await start(_) log.info("Starting loop..") log.info("Aiogram: Using polling method") if CONFIG.webhooks_enable: executor.start_webhook(dp, f'/{CONFIG.token}', on_startup=start_webhooks, port=CONFIG.webhooks_port) else: executor.start_polling(dp, loop=loop, on_startup=start)
webapp.router.add_post('/sendMessage', send_message_handler) self.runner = web.AppRunner(webapp) if __name__ == '__main__': aq = AdmissionQueue() if 'WEBHOOK_HOST' in os.environ: host = os.environ['WEBHOOK_HOST'] port = os.environ['WEBHOOK_PORT'] async def on_startup(dp): await aq.runner.setup() site = web.TCPSite(aq.runner, "127.0.0.1", 5006) await site.start() await aq.bot.set_webhook(host) async def on_shutdown(dp): await aq.bot.delete_webhook() executor.start_webhook(aq.dp, webhook_path='', on_startup=on_startup, on_shutdown=on_shutdown, host='localhost', port=port) else: executor.start_polling(aq.dp, skip_updates=True)
import aiogram from aiogram import executor from money_bot.initialization import dp, on_startup try: from money_bot import local_config as config except ImportError: from money_bot import example_config as config if __name__ == "__main__": executor.start_webhook(dp, on_startup=on_startup, webhook_path=config.WEBHOOK_PATH, host=config.SERVER_HOST, port=config.WEBHOOK_PORT)
await message.answer(res_msg) @dp.callback_query_handler() async def send_qr(callback_query: types.CallbackQuery): """ Creates and sends to the user the QR code of the url in callback query. """ img = qrcode.make(callback_query.data) bio = BytesIO() bio.name = 'qr.png' img.save(bio, 'PNG') bio.seek(0) img_file = types.InputFile(bio, filename='qr.png') await callback_query.answer() await bot.send_photo( callback_query.from_user.id, img_file, ResponseMsgs.QR_FOR_IMG.format(url=callback_query.data)) if __name__ == '__main__': executor.start_webhook(dp, '/', on_startup=on_startup, on_shutdown=on_shutdown, host='0.0.0.0', port=WebAppConfig.PORT)
address["address"]) else: suggestions = await data_source.get_suggestions(message.text) suggestions_flat = "\n".join([f"* {x}" for x in suggestions]) await message.answer("Sorry! Could not find the address\n" + ( "Did you mean:\n\n" + f"{suggestions_flat}" if suggestions else "")) except Exception as e: pass async def on_startup(dp): await bot.set_webhook(f"{WEBHOOK_URL}{WEBHOOK_PATH}") async def on_shutdown(dp): await bot.delete_webhook() if __name__ == '__main__': if WEBHOOK: executor.start_webhook(dp, WEBHOOK_PATH, on_startup=on_startup, on_shutdown=on_shutdown, skip_updates=True, host=WEBAPP_HOST, port=WEBAPP_PORT) else: executor.start_polling(dp, skip_updates=True)