Fix error, switch default storage from Redis to Memory, and add logging to multibot example

This commit is contained in:
latan 2023-08-10 15:54:10 +03:00
parent 31baa12d90
commit 3b2ead5322
3 changed files with 12 additions and 5 deletions

View file

@ -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:

View file

@ -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()

View file

@ -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)