bot icon indicating copy to clipboard operation
bot copied to clipboard

Panic when editing a message

Open CodeExplorerX opened this issue 1 year ago • 2 comments

Panic occurs when editing a message. The code was used as an example from the repository page. go version: go version go1.22.4 windows/amd64 Code: image

Debug log: ` "edited_message":{"message_id":106,"from":{"id":23432432,"is_bot":false,"first_name":"\u1160","username":"example_user","language_code":"en"},"chat":{"id":24234232,"first_name":"\u1160","username":"example_user","type":"private"},"date":1720134098,"edit_date":1720134108,"text":"/edit","entities":[{"offset":0,"length":5,"type":"bot_command"}]}}]}' panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x48 pc=0xb23a40]

goroutine 18 [running]: main.handler({0xc2d018, 0xc000068240}, 0xc0000000c0, 0xc0000b2000) C:/Users/anonymous/Desktop/bot/main.go:33 +0x40 github.com/go-telegram/bot.(*Bot).ProcessUpdate.func1() C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/process_update.go:25 +0x6d github.com/go-telegram/bot.(*Bot).ProcessUpdate(0xc000287f78?, {0xc2d018?, 0xc000068240?}, 0x0?) C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/process_update.go:36 +0xc3 github.com/go-telegram/bot.(*Bot).waitUpdates(0xc0000000c0, {0xc2d018, 0xc000068240}, 0x0?) C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/wait_updates.go:17 +0x85 created by github.com/go-telegram/bot.(*Bot).Start in goroutine 1 C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/bot.go:116 +0xa5 exit status 2 `

CodeExplorerX avatar Jul 04 '24 23:07 CodeExplorerX

Additions: When you restart the bot after a panic, panic occurs again and the bot cannot work. To eliminate panic, changing the token helps.

Debug log: 2024/07/05 01:17:11 [TGBOT] [DEBUG] request url: https://api.telegram.org/botexample/getMe, payload: null 2024/07/05 01:17:11 [TGBOT] [DEBUG] response from 'https://api.telegram.org/botexample/getMe' with payload '{"ok":true,"result":{"id":7159102647,"is_bot":true,"first_name":"kfmdskfms","username":"smdflksdmf_bot","can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false,"can_connect_to_business":false}}' 2024/07/05 01:17:11 [TGBOT] [DEBUG] response from 'https://api.telegram.org/botexample/getUpdates' with payload '{"ok":true,"result":[{"update_id":78667351, "edited_message":{"message_id":106,"from":{"id":3423423,"is_bot":false,"first_name":"\u1160","username":"anonymous","language_code":"en"},"chat":{"id":3423423,"first_name":"\u1160","username":"anonymous","type":"private"},"date":1720134098,"edit_date":1720134108,"text":"/edit","entities":[{"offset":0,"length":5,"type":"bot_command"}]}}]}' panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x48 pc=0xd23a40]

goroutine 34 [running]: main.handler({0xe2d018, 0xc000068240}, 0xc0000000c0, 0xc0000fa000) C:/Users/anonymous/Desktop/bot/main.go:33 +0x40 github.com/go-telegram/bot.(*Bot).ProcessUpdate.func1() C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/process_update.go:25 +0x6d github.com/go-telegram/bot.(*Bot).ProcessUpdate(0xc0000b7f78?, {0xe2d018?, 0xc000068240?}, 0x0?) C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/process_update.go:36 +0xc3 github.com/go-telegram/bot.(*Bot).waitUpdates(0xc0000000c0, {0xe2d018, 0xc000068240}, 0x0?) C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/wait_updates.go:17 +0x85 created by github.com/go-telegram/bot.(*Bot).Start in goroutine 1 C:/Users/anonymous/go/pkg/mod/github.com/go-telegram/[email protected]/bot.go:116 +0xa5 exit status 2

CodeExplorerX avatar Jul 04 '24 23:07 CodeExplorerX

EditMessageText works for me https://github.com/go-telegram/bot/tree/main/examples/edit_message Can you provide your code?

negasus avatar Jul 18 '24 14:07 negasus

I also met this problem, here's the solution.

Check out the example code here. The difference is, line 35 checks whether update.Message is nil.

You can do that in your default handler too, I think that will work. My approach is adding a middleware that checks update.Message, and it worked.

Here's my middleware

func nilChecker(next bot.HandlerFunc) bot.HandlerFunc {
	return func(ctx context.Context, b *bot.Bot, update *models.Update) {
		if update.Message == nil {
			return
		}
		next(ctx, b, update)
	}
}

TurboHsu avatar Sep 03 '24 07:09 TurboHsu