H
H
HPositron2022-03-26 18:21:48
API
HPositron, 2022-03-26 18:21:48

Why can't I send photos using tgbotapi in Golang?

When trying to submit a photo using the official code example from github tgbotapi :

data, _ := ioutil.ReadFile("image.png")
b := tgbotapi.FileBytes{Name: "image.png", Bytes: data}

msg := tgbotapi.NewPhoto(chatID, b)
msg.Caption = "Test"

_, err := bot.Send(msg)

if err != nil {
  log.Println(err)
}


Occurs panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x10 pc=0xbc72ad]

goroutine 1 [running]:
github.com/go-telegram-bot-api/telegram-bot-api/v5.(*BotAPI).UploadFiles(0x0, {0xc150ae, 0x9}, 0xc000134ba0, {0xc000024660, 0x1, 0x1})
        C:/Users/****/go/pkg/mod/github.com/go-telegram-bot-api/telegram-bot-api/[email protected].5.1/bot.go:221 +0x30d
github.com/go-telegram-bot-api/telegram-bot-api/v5.(*BotAPI).Request(0xa7a651, {0xc5c398, 0xc00013a000})
        C:/Users/****/go/pkg/mod/github.com/go-telegram-bot-api/telegram-bot-api/[email protected].5.1/bot.go:326 +0xf6
github.com/go-telegram-bot-api/telegram-bot-api/v5.(*BotAPI).Send(_, {_, _})
        C:/Users/****/go/pkg/mod/github.com/go-telegram-bot-api/telegram-bot-api/[email protected].5.1/bot.go:342 +0x58
main.SendPhoto({0xc1a04c, 0xa88905}, 0x24e41057, 0xc000030000)
        D:/Work/tgphoto.go:53 +0x186
main.main()
        D:/Work/tgphoto.go:13 +0x2f
exit status 2


What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2022-03-26
@HPositron

What line does the panic point to?
You obviously have not initialized any of the used objects.
I bet that you did not initialize the bot, show how you create it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question