🌙

getUpdates / Поллинг

getUpdates / Поллинг

Получает очередь апдейтов от сервера. Внутри Dispatcher.start_polling() вызывается автоматически.

updates = await bot.get_updates(offset=0, timeout=20, limit=100)

Параметры

ПараметрТипПо умолчаниюОписание
offsetint0update_id последнего обработанного апдейта + 1
timeoutint20Long-polling: ждать N секунд (max 25)
limitint100Максимум апдейтов за раз (max 100)

Возвращает: list[Update]

Запуск через Dispatcher (рекомендуется)

import asyncio
from pykodaribot import Bot, Dispatcher, filters

bot = Bot("bot_ВАШ_ТОКЕН")
dp = Dispatcher()

@dp.message(filters.command("start"))
async def start(msg):
    await msg.answer("Привет!")

asyncio.run(dp.start_polling(bot))

start_polling обрабатывает переподключение при ошибках и корректно закрывает сессию при остановке (Ctrl+C).

Ручной поллинг

import asyncio
from pykodaribot import Bot

async def main():
    bot = Bot("bot_ВАШ_ТОКЕН")
    offset = 0
    while True:
        updates = await bot.get_updates(offset=offset, timeout=20)
        for upd in updates:
            offset = upd.update_id + 1
            if upd.message:
                print(upd.message.text)
            elif upd.message_reaction:
                print(f"Реакция: {upd.message_reaction.emoji}")

asyncio.run(main())
1 просмотр