示例#1
0
def main():
    web_app = get_new_configured_app(dispatcher=dp, path=config.WEBHOOK_PATH)

    if config.WEBHOOK_USE:
        web_app.on_startup.append(on_startup)
        web_app.on_shutdown.append(on_shutdown)
        web_app = server.init_app(web_app)
        web.run_app(web_app, **config.WEBHOOK_SERVER)
    else:
        executor.on_startup(on_startup)
        executor.on_shutdown(on_shutdown)
        executor.start_polling()
示例#2
0
def get_webapp():
    def set_webhook(_):
        return bot.set_webhook(
            f"https://{os.getenv('BOT_DOMAIN')}{DEFAULT_WEB_PATH}")

    app = get_new_configured_app(dp)
    app.add_routes([web.get('/post',
                            post_teacher)])  # todo maybe posting signal here

    app.on_startup.append(set_webhook)
    # app.on_shutdown.append(on_shutdown)

    return app
示例#3
0
文件: web.py 项目: vstasn/aiobot
def build_app(dispatcher):
    application = get_new_configured_app(dispatcher=dispatcher,
                                         path=WEBHOOK_URL_PATH)
    # set settings
    application.settings = settings

    application.on_startup.append(init_pg)
    application.on_shutdown.append(
        functools.partial(on_shutdown, url=WEBHOOK_URL))
    application.on_startup.append(
        functools.partial(on_startup, url=WEBHOOK_URL))
    application.on_cleanup.append(close_pg)

    return application
示例#4
0
def main():
    if config.USE_WEBHOOK:
        app = get_new_configured_app(dp, config.WEBHOOK_PATH)

        app.on_startup.append(on_startup)
        if not config.HEROKU:
            app.on_shutdown.append(on_shutdown)

        context = None
        if config.CUSTOM_SSL_CERT:
            context = ssl.SSLContext()
            context.load_cert_chain(config.SSL_CERT, config.SSL_PRIV)

        run_app(app, port=config.WEBHOOK_LOCAL_PORT, ssl_context=context)

    else:
        async def proxy_login(*args, **kwargs):
            await api.login()

        try:
            start_polling(dp, loop=loop, skip_updates=True, on_startup=proxy_login)
        except KeyboardInterrupt:
            print('goodbye')
            dp.stop_polling()
                              certificate=open(WEBHOOK_SSL_CERT, 'rb'))


async def on_shutdown(app):
    """
    Выполняется при выключении бота.
    """
    await bot.delete_webhook()
    await conn.close()
    await dp.storage.close()
    await dp.storage.wait_closed()


if __name__ == '__main__':
    dp.middleware.setup(AntiFlood())
    dp.middleware.setup(CallbackAntiFlood())
    dp.middleware.setup(WordsFilter())

    app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_URL_PATH)

    app.on_startup.append(on_startup)
    app.on_shutdown.append(on_shutdown)

    context_ssl = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context_ssl.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)

    web.run_app(app,
                host=WEBAPP_HOST,
                port=WEBAPP_PORT,
                ssl_context=context_ssl)
示例#6
0
 async def configure_app(self, app = None):
     await self.bot.set_webhook(self.full_webhook_path)
     if app is not None:
         configure_app(dispatcher = self.dp, app = app, path = self.webhook_url)
     else:
         return get_new_configured_app(dispatcher=self.dp, path = self.webhook_url)
示例#7
0

@routes.get('/')
async def hello(request):
    print(request)
    return web.Response(text='<Omnidesk bot> by @spooti')


@routes.post('/omnidesk_message')
async def change_status(request):
    if 'urlencode' in request.content_type:
        data = urlencoded_to_dict(await request.text())
        await omnidesk_msg_handler(data)
    return web.Response()


async def on_startup(_):
    wh_url = 'https://omideskbot.herokuapp.com/tg'
    webhook = await bot.get_webhook_info()
    if webhook.url != wh_url:
        await bot.delete_webhook()
        await bot.set_webhook(f'https://omideskbot.herokuapp.com/tg')


app = get_new_configured_app(dp, '/tg')

app.add_routes(routes)

app.on_startup.append(on_startup)
web.run_app(app, port=os.environ['PORT'])
示例#8
0
                              certificate=open(WEBHOOK_SSL_CERT, 'rb'))

    # Запуск обновления и рассылки сообщений в отдельном потоке


# Выполняется по завершении
async def on_shutdown(application):
    del application
    # Удалить вебхук
    await bot.delete_webhook()

    # Закрыть хранилище в памяти
    await dp.storage.close()
    await dp.storage.wait_closed()


if __name__ == '__main__':

    # Создает объект класса веб-приложениe
    app = get_new_configured_app(dispatcher=dp, path='/' + TOKEN)

    # Задает эвенты на старте и окончании работы бота
    app.on_startup.append(on_startup)
    # Устанавливает SSL
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)

    # Стартует веб-приложение
    loop.call_later(0, repeat, post_update, loop)
    web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT, ssl_context=context)
示例#9
0
            f'Nothing found for {q.query}')
        inline_result = types.InlineQueryResultArticle(
            id='1',
            title='Nothing found',
            input_message_content=input_content,
            description=f'Nothing found for {q.query}')
        inline_results.append(inline_result)

    try:
        await bot.answer_inline_query(q.id, inline_results)
    except:
        pass


async def on_startup(*args, **kwargs):
    await bot.delete_webhook()
    await bot.set_webhook(config.WEBHOOK_URL)


async def on_shutdown(*args, **kwargs):
    await bot.delete_webhook()


if __name__ == '__main__':
    app = get_new_configured_app(dp, config.WEBHOOK_URL_PATH)

    app.on_startup.append(on_startup)
    app.on_shutdown.append(on_shutdown)

    run_app(app, port=config.WEBAPP_PORT)