misc: code consistency and bot instance creation

This commit is contained in:
sdmway 2024-05-07 20:14:38 -06:00
parent a7d3e9a720
commit df1ab06762
11 changed files with 32 additions and 20 deletions

View file

@ -7,7 +7,8 @@ from os import getenv
from aiohttp import web
from aiogram import Bot, Dispatcher, Router, types
from aiogram import Bot, Dispatcher, Router
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message
@ -29,7 +30,7 @@ WEBHOOK_PATH = "/webhook"
WEBHOOK_SECRET = "my-secret"
# Base URL for webhook will be used to generate webhook URL for Telegram,
# in this example it is used public DNS with HTTPS support
BASE_WEBHOOK_URL = "https://aiogram.dev/"
BASE_WEBHOOK_URL = "https://aiogram.dev"
# All handlers should be attached to the Router (or Dispatcher)
router = Router()
@ -49,7 +50,7 @@ async def command_start_handler(message: Message) -> None:
@router.message()
async def echo_handler(message: types.Message) -> None:
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
@ -79,7 +80,7 @@ def main() -> None:
dp.startup.register(on_startup)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# Create aiohttp.web.Application instance
app = web.Application()

View file

@ -8,7 +8,8 @@ from os import getenv
from aiohttp import web
from aiogram import Bot, Dispatcher, Router, types
from aiogram import Bot, Dispatcher, Router
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import FSInputFile, Message
@ -54,7 +55,7 @@ async def command_start_handler(message: Message) -> None:
@router.message()
async def echo_handler(message: types.Message) -> None:
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
@ -90,7 +91,7 @@ def main() -> None:
dp.startup.register(on_startup)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# Create aiohttp.web.Application instance
app = web.Application()

View file

@ -1,8 +1,11 @@
import asyncio
import html
import logging
from os import getenv
from aiogram import Bot, Dispatcher, types
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import (
Command,
CommandObject,
@ -11,7 +14,7 @@ from aiogram.filters import (
)
from aiogram.types import ErrorEvent
TOKEN = "42:TOKEN"
TOKEN = getenv("BOT_TOKEN")
dp = Dispatcher()
@ -100,7 +103,7 @@ async def handle_set_name(message: types.Message, command: CommandObject) -> Non
async def main() -> None:
# Initialize Bot instance with a default parse mode which will be passed to all API calls
bot = Bot(TOKEN, parse_mode="HTML")
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)

View file

@ -5,6 +5,7 @@ from os import getenv
from typing import Any, Dict
from aiogram import Bot, Dispatcher, F, Router, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import Command, CommandStart
from aiogram.fsm.context import FSMContext
@ -124,7 +125,7 @@ async def show_summary(message: Message, data: Dict[str, Any], positive: bool =
async def main():
bot = Bot(token=TOKEN, parse_mode=ParseMode.HTML)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()
dp.include_router(form_router)

View file

@ -6,6 +6,7 @@ from handlers.echo import echo_router
from handlers.start import start_router
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
# Bot token can be obtained via https://t.me/BotFather
@ -22,7 +23,7 @@ async def main() -> None:
)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)

View file

@ -1,11 +1,11 @@
from aiogram import Router
from aiogram.filters import Command
from aiogram.filters import CommandStart
from aiogram.types import Message
start_router = Router()
@start_router.message(Command("start"))
@start_router.message(CommandStart())
async def command_start_handler(message: Message) -> None:
"""
This handler receives messages with `/start` command

View file

@ -290,7 +290,7 @@ def create_dispatcher():
async def main():
dispatcher = create_dispatcher()
bot = Bot(TOKEN)
bot = Bot(token=TOKEN)
await dispatcher.start_polling(bot)

View file

@ -16,6 +16,8 @@ from aiogram.types import (
ReplyKeyboardRemove,
)
TOKEN = getenv("BOT_TOKEN")
BUTTON_CANCEL = KeyboardButton(text="❌ Cancel")
BUTTON_BACK = KeyboardButton(text="🔙 Back")
@ -199,5 +201,5 @@ if __name__ == "__main__":
# Recommended to use CLI instead of this snippet.
# `aiogram run polling scene_example:create_dispatcher --token BOT_TOKEN --log-level info`
dp = create_dispatcher()
bot = Bot(token=getenv("TELEGRAM_TOKEN"))
dp.run_polling()
bot = Bot(token=TOKEN)
dp.run_polling(bot)

View file

@ -4,6 +4,7 @@ import sys
from os import getenv
from aiogram import Bot, Dispatcher, Router
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import LEAVE_TRANSITION, ChatMemberUpdatedFilter, CommandStart
from aiogram.types import (
@ -79,7 +80,7 @@ async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> Non
async def main() -> None:
# Initialize Bot instance with a default parse mode which will be passed to all API calls
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()

View file

@ -1,5 +1,5 @@
from aiogram import Bot, F, Router
from aiogram.filters import Command
from aiogram.filters import Command, CommandStart
from aiogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
@ -11,7 +11,7 @@ from aiogram.types import (
my_router = Router()
@my_router.message(Command("start"))
@my_router.message(CommandStart())
async def command_start(message: Message, bot: Bot, base_url: str):
await bot.set_chat_menu_button(
chat_id=message.chat.id,

View file

@ -8,6 +8,8 @@ from handlers import my_router
from routes import check_data_handler, demo_handler, send_message_handler
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums.parse_mode import ParseMode
from aiogram.types import MenuButtonWebApp, WebAppInfo
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
@ -24,7 +26,7 @@ async def on_startup(bot: Bot, base_url: str):
def main():
bot = Bot(token=TOKEN, parse_mode="HTML")
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dispatcher = Dispatcher()
dispatcher["base_url"] = APP_BASE_URL
dispatcher.startup.register(on_startup)