Go语言 telegram-bot-API/v5 SetWebhook函数未定义

ezykj2lf  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(94)

在官方文档中,有以下使用webhook构建tg bot的示例代码:

  1. func main() {
  2. bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. bot.Debug = true
  7. log.Printf("Authorized on account %s", bot.Self.UserName)
  8. wh, _ := tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
  9. _, err = bot.SetWebhook(wh)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. info, err := bot.GetWebhookInfo()
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. if info.LastErrorDate != 0 {
  18. log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
  19. }
  20. updates := bot.ListenForWebhook("/" + bot.Token)
  21. go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
  22. for update := range updates {
  23. log.Printf("%+v\n", update)
  24. }
  25. }

我正在遵循这个例子,但是遇到了使用bot.SetWebhook(wh)的问题,它显示

  1. bot.SetWebhhook() undefined (type *tgbotapi.BotAPI has no field or method SetWebhook)

这个包是版本问题还是环境设置问题?还是别的什么
我不知道该怎么办才来找你帮忙
Thanks in advance

bttbmeg0

bttbmeg01#

你提供的例子是旧的,不再有SetWebhhook方法。首先安装软件包(如果没有安装,请安装最新版本):

  1. go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5

下面是一个包含webhooks的工作示例:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  6. )
  7. func main() {
  8. bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. bot.Debug = true
  13. log.Printf("Authorized on account %s", bot.Self.UserName)
  14. wh, _ := tgbotapi.NewWebhookWithCert("https://www.example.com:8443/"+bot.Token, "cert.pem")
  15. _, err = bot.Request(wh)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. info, err := bot.GetWebhookInfo()
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. if info.LastErrorDate != 0 {
  24. log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
  25. }
  26. updates := bot.ListenForWebhook("/" + bot.Token)
  27. go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
  28. for update := range updates {
  29. log.Printf("%+v\n", update)
  30. }
  31. }
展开查看全部

相关问题