telegram-bot-api icon indicating copy to clipboard operation
telegram-bot-api copied to clipboard

Incorrect example implementation of: tgbotapi.NewWebhookWithCert()

Open Etherdrake opened this issue 2 years ago • 4 comments

If I try to replicate the second example code:

package main

import (
	"log"
	"net/http"

	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Fatal(err)
	}

	bot.Debug = true

	log.Printf("Authorized on account %s", bot.Self.UserName)

	wh, _ := tgbotapi.NewWebhookWithCert("https://www.example.com:8443/"+bot.Token, "cert.pem")

	_, err = bot.Request(wh)
	if err != nil {
		log.Fatal(err)
	}

	info, err := bot.GetWebhookInfo()
	if err != nil {
		log.Fatal(err)
	}

	if info.LastErrorDate != 0 {
		log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
	}

	updates := bot.ListenForWebhook("/" + bot.Token)
	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)

	for update := range updates {
		log.Printf("%+v\n", update)
	}
}

It does not compile, instead returning the following compilation error:

./main.go:19:89: cannot use "cert.pem" (constant of type string) as type tgbotapi.RequestFileData in argument to tgbotapi.NewWebhookWithCert:
        string does not implement tgbotapi.RequestFileData (missing NeedsUpload method)

Etherdrake avatar Jan 08 '23 01:01 Etherdrake

Yeah!

Needs to be changed to tgbotapi.NewWebhookWithCert("https://www.example.com:8443/"+bot.Token, tgbotapi.FilePath("cert.pem"))

freaker2k7 avatar Feb 01 '23 13:02 freaker2k7

i use above example:

i am open the below url in chrome, its ok!

Post "https://api.telegram.org/botxxxxxxxxxx/getMe": dial tcp 118.180.172.204:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

huaiguoguo avatar Apr 28 '23 08:04 huaiguoguo

@freaker2k7 have you encountered the same problem?

huaiguoguo avatar Apr 30 '23 14:04 huaiguoguo

@huaiguoguo yes and what I recommended solved it for me.

freaker2k7 avatar Apr 30 '23 20:04 freaker2k7