From 3b2ead532297fd2c2b9c678d51023301b2ec85b7 Mon Sep 17 00:00:00 2001 From: latan Date: Thu, 10 Aug 2023 15:54:10 +0300 Subject: [PATCH] Fix error, switch default storage from Redis to Memory, and add logging to multibot example --- examples/echo_bot_webhook.py | 2 +- examples/echo_bot_webhook_ssl.py | 4 +++- examples/multibot.py | 11 ++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/echo_bot_webhook.py b/examples/echo_bot_webhook.py index e5bfd114..1fa56c3d 100644 --- a/examples/echo_bot_webhook.py +++ b/examples/echo_bot_webhook.py @@ -66,7 +66,7 @@ async def echo_handler(message: types.Message) -> None: async def on_startup(bot: Bot) -> None: # If you have a self-signed SSL certificate, then you will need to send a public # certificate to Telegram - await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}") + await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}", secret_token=WEBHOOK_SECRET) def main() -> None: diff --git a/examples/echo_bot_webhook_ssl.py b/examples/echo_bot_webhook_ssl.py index 9fd5b457..ad41bc4d 100644 --- a/examples/echo_bot_webhook_ssl.py +++ b/examples/echo_bot_webhook_ssl.py @@ -3,6 +3,7 @@ This example shows how to use webhook with SSL certificate. """ import logging import ssl +import sys from os import getenv from aiohttp import web @@ -75,6 +76,7 @@ async def on_startup(bot: Bot) -> None: await bot.set_webhook( f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}", certificate=FSInputFile(WEBHOOK_SSL_CERT), + secret_token=WEBHOOK_SECRET, ) @@ -116,5 +118,5 @@ def main() -> None: if __name__ == "__main__": - logging.basicConfig(level=logging.INFO) + logging.basicConfig(level=logging.INFO, stream=sys.stdout) main() diff --git a/examples/multibot.py b/examples/multibot.py index 5e6f03f9..82fac4a1 100644 --- a/examples/multibot.py +++ b/examples/multibot.py @@ -1,15 +1,16 @@ +import logging +import sys from os import getenv from typing import Any, Dict, Union from aiohttp import web -from finite_state_machine import form_router from aiogram import Bot, Dispatcher, F, Router from aiogram.client.session.aiohttp import AiohttpSession from aiogram.enums import ParseMode from aiogram.exceptions import TelegramUnauthorizedError from aiogram.filters import Command, CommandObject -from aiogram.fsm.storage.redis import DefaultKeyBuilder, RedisStorage +from aiogram.fsm.storage.memory import MemoryStorage from aiogram.types import Message from aiogram.utils.token import TokenValidationError, validate_token from aiogram.webhook.aiohttp_server import ( @@ -17,6 +18,7 @@ from aiogram.webhook.aiohttp_server import ( TokenBasedRequestHandler, setup_application, ) +from finite_state_machine import form_router main_router = Router() @@ -57,10 +59,13 @@ async def on_startup(dispatcher: Dispatcher, bot: Bot): def main(): + logging.basicConfig(level=logging.INFO, stream=sys.stdout) session = AiohttpSession() bot_settings = {"session": session, "parse_mode": ParseMode.HTML} bot = Bot(token=MAIN_BOT_TOKEN, **bot_settings) - storage = RedisStorage.from_url(REDIS_DSN, key_builder=DefaultKeyBuilder(with_bot_id=True)) + storage = MemoryStorage() + # In order to use RedisStorage you need to use Key Builder with bot ID: + # storage = RedisStorage.from_url(REDIS_DSN, key_builder=DefaultKeyBuilder(with_bot_id=True)) main_dispatcher = Dispatcher(storage=storage) main_dispatcher.include_router(main_router)