go-telegram / bot

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

Example bot crashes with segmentation fault when added to a group #82

Closed MarioVilas closed 1 month ago

MarioVilas commented 1 month ago

I tried the example bot from the documentation that just echoes back whatever you write (echo/main.go).

Then I added the bot to a group with myself and tried to talk to it in the group. This is the error I get:

[signal SIGSEGV: segmentation violation code=0x2 addr=0x48 pc=0x102af14f0]

goroutine 34 [running]:
main.handler({0x102bffdf8, 0x1400002a200}, 0x1400014c000, 0x14000310000)
    /Users/marito/Work/cacabot/main.go:34 +0x40
github.com/go-telegram/bot.(*Bot).ProcessUpdate.func1()
    /Users/marito/go/pkg/mod/github.com/go-telegram/bot@v1.2.2/process_update.go:25 +0x6c
github.com/go-telegram/bot.(*Bot).ProcessUpdate(0x1400004e770?, {0x102bffdf8?, 0x1400002a200?}, 0x0?)
    /Users/marito/go/pkg/mod/github.com/go-telegram/bot@v1.2.2/process_update.go:36 +0xbc
github.com/go-telegram/bot.(*Bot).waitUpdates(0x1400014c000, {0x102bffdf8, 0x1400002a200}, 0x0?)
    /Users/marito/go/pkg/mod/github.com/go-telegram/bot@v1.2.2/wait_updates.go:17 +0x6c
created by github.com/go-telegram/bot.(*Bot).Start in goroutine 1
    /Users/marito/go/pkg/mod/github.com/go-telegram/bot@v1.2.2/bot.go:116 +0xa4

Note that I get the same error whenever I run the bot since then. Probably because it's still trying to download the message that causes it to crash.

MarioVilas commented 1 month ago

Line 34 contains this:

ChatID: update.Message.Chat.ID,

This is the entire function:

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
    b.SendMessage(ctx, &bot.SendMessageParams{
        ChatID: update.Message.Chat.ID,
        Text:   update.Message.Text,
    })
}
negasus commented 1 month ago

You can use handler

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
    if update.Message != nil {
        b.SendMessage(ctx, &bot.SendMessageParams{
            ChatID: update.Message.Chat.ID,
            Text:   update.Message.Text,
        })
    }
}

Simple examples don't cover all the ways a bot can be used