go-telegram / bot

Telegram Bot API Go framework
MIT License
502 stars 46 forks source link

Default handler processes updates out of order #64

Closed pakuula closed 3 months ago

pakuula commented 4 months ago

At the end of the issue there is a very basic echo bot with debug enabled

When the bot is off, type in the chat messages:

Then launch the bot. The log (simplified):

[TGBOT] [DEBUG] request url: https://api.telegram.org/botFOO:BAR/getMe, payload: null
[TGBOT] [DEBUG] response from 'https://api.telegram.org/botFOO:BAR/getMe' with payload '{"ok":true,"result":{"id":botbotbot,"is_bot":true,...,"can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false}}'
[TGBOT] [DEBUG] response from 'https://api.telegram.org/botFOO:BAR/getUpdates' with payload '{"ok":true,"result":[
{"update_id":180259658,"message":{"message_id":95,"from":{"...},"chat":{...},"date":1709399995,"text":"1"}},
{"update_id":180259659,"message":{"message_id":96,"from":{"...},"chat":{...},"date":1709399995,"text":"2"}},
{"update_id":180259660,"message":{"message_id":97,"from":{"...},"chat":{...},"date":1709399996,"text":"3"}},
{"update_id":180259661,"message":{"message_id":98,"from":{"...},"chat":{...},"date":1709399996,"text":"4"}},
{"update_id":180259662,"message":{"message_id":99,"from":{"...},"chat":{...},"date":1709399999,"text":"5"}},
{"update_id":180259663,"message":{"message_id":100,"from":{"...},"chat":{...},"date":1709400000,"text":"6"}},
{"update_id":180259664,"message":{"message_id":101,"from":{"...},"chat":{...},"date":1709400000,"text":"7"}},
{"update_id":180259665,"message":{"message_id":102,"from":{"...},"chat":{...},"date":1709400001,"text":"8"}}
]}'
INFO received message: text=8
INFO received message: text=4
INFO received message: text=5
INFO received message: text=7
INFO received message: text=1
INFO received message: text=6
INFO received message: text=2
INFO received message: text=3

TG server returns updates in order: messages in the array result are in the ascending order of update_id. But the handler receives the messages in a random order.

Are there options to ensure the order of the updates in the handler?

The program:

package main

import (
    "context"
    "log/slog"
    "os"
    "os/signal"

    "github.com/go-telegram/bot"
    "github.com/go-telegram/bot/models"
)

func main() {
    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    opts := []bot.Option{
        bot.WithDefaultHandler(handler),
        bot.WithDebug(),
    }

    token, ok := os.LookupEnv("TG_BOT")
    if !ok {
        panic("Set TG_BOT environment variable")
    }
    b, err := bot.New(token, opts...)
    if nil != err {
        panic(err)
    }

    b.Start(ctx)
}

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
    if update == nil {
        slog.Info("nil update")
        return
    }
    if update.Message == nil {
        slog.Info("nil message")
    }
    slog.Info("received message:",
        "text", update.Message.Text,
    )
    b.SendMessage(ctx, &bot.SendMessageParams{
        ChatID: update.Message.Chat.ID,
        Text:   update.Message.Text,
    })
}
negasus commented 4 months ago

Indeed, now messages may arrive out of order due to implementation features (launching processing workers in goroutines). I'll look into what can be done soon

negasus commented 3 months ago

Worker pool removed in v1.2.0