0

Trying to connect Dialogflow to telegram bot to use it with webhook, to avoid using .json file credentials. A part of code from bot.py

from aiogram.dispatcher.webhook import SendMessage

API_HOST = os.getenv("API_HOST")
BOT_TOKEN = os.getenv("BOT_TOKEN")
APP_NAME = os.getenv("APP_NAME")

WEBHOOK_HOST = f"https://{APP_NAME}.herokuapp.com"
WEBHOOK_PATH = f"/webhook/{BOT_TOKEN}"
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"

WEBAPP_HOST = "0.0.0.0"
WEBAPP_PORT = int(os.environ.get("PORT", "5000"))

logging.basicConfig(level=logging.INFO)
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())

async def scheduler():
    """Scheduler for sending messages."""

    # aioschedule.every(10).seconds.do(prepare_message)
    # keep in mind that this is remote server time
    aioschedule.every().day.at("17:00").do(prepare_message)
    while True:
        await aioschedule.run_pending()
        await asyncio.sleep(1)


async def on_startup(dispatcher):
    """Startup function."""

    # add commands to MENU button
    await set_commands(bot)

    asyncio.create_task(scheduler())
    if not DEVELOP:
        await bot.set_webhook(url=WEBHOOK_URL)


async def on_shutdown(dispatcher):
    """Shutdown function."""
    await bot.delete_webhook()

@dp.message_handler(content_types=["text"])
async def reply_to_user(message: types.Message):
    # this code should reply to user with data from a webhook, but I do not khow how to retrieve it
    return SendMessage(message.chat.id, message.text)

if __name__ == "__main__":
    if DEVELOP:
        executor.start_polling(dispatcher=dp, skip_updates=True, on_startup=on_startup)
    else:
        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,
        )

Bot hosted on Heroku, it works, but but I do not khow how to retrieve payload from webhook url. Aiogram docs tells to use from aiogram.dispatcher.webhook import SendMessage and nothing more.

Vitalii Mytenko
  • 544
  • 5
  • 20

0 Answers0