SelfLearnedNoob
SelfLearnedNoob

Reputation: 33

Telegram Bot Webhook - Connection Refused (Golang)

I have configured a Telegram bot to use Webhooks according to the following configuration:

OS: Ubuntu

Server: Apache2

Virtual Host:

Webhook:

Still, however, my bot just won't work, spend literally the entire last 18 hours trying to fix it. It is 3 at night now, and I just am at my wits end.

When I start my binary it prints the following to stdout:

2023/01/07 01:50:15 Authorized on account ExampleAccount

2023/01/07 01:50:15 setWebhook resp: {"ok":true,"result":true,"description":"Webhook is already set"}

2023/01/07 01:50:15 getWebhookInfo resp: {"ok":true,"result":{"url":"https://domain.app:8443/MY_BOT_TOKEN","has_custom_certificate":false,"pending_update_count":0,"last_error_date":1673052633,"last_error_message":"Connection refused","max_connections":40,"ip_address":"x.x.x.x"}}

2023/01/07 01:50:15 Telegram callback failed: Connection refused

If somebody has any idea please help me!

2023/01/07 02:07:57 getWebhookInfo resp: {"ok":true,"result":{"url":"https://domain.app:8443/MY_BOT_TOKEN","has_custom_certificate":false,"pending_update_count":3,"last_error_date":1673052633,"last_error_message":"Connection refused","max_connections":40,"ip_address":"167.99.34.89"}}

If I issue start commands to the bot, the pending_update_count actually gets incremented as well, so this is a really strange issue.

My code:

func main() {
    // Create a new bot
    bot, err := tgbotapi.NewBotAPI("PLACEHOLDER")
    if err != nil {
        log.Panic(err)
    }
 
    bot.Debug = true
    log.Printf("Authorized on account %s", bot.Self.UserName)
 
    // Set the webhook
    _, err = bot.SetWebhook(tgbotapi.NewWebhook("https://domain.app:8443/" + bot.Token))
    if err != nil {
        log.Panic(err)
    }
 
    info, err := bot.GetWebhookInfo()
    if err != nil {
        log.Fatal(err)
    }
 
    if info.LastErrorDate != 0 {
        log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
    }
 
    // Start the webhook server
    http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
        update := tgbotapi.Update{}
        err := json.NewDecoder(r.Body).Decode(&update)
        if err != nil {
            log.Println(err)
            return
        }
 
        // Check if the update is a callback query (button press)
        if update.CallbackQuery != nil {
            callbackQuery := update.CallbackQuery
            data := callbackQuery.Data
 
            // Create a new message
            msg := tgbotapi.NewMessage(callbackQuery.Message.Chat.ID, "")
 
            // Set the text and keyboard based on the button pressed
            switch data {
            case "get_information":
                msg.Text = "Here is some information about which formats are supported:"
                msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
            case "start_file_conversion":
                msg.Text = "Great! Click the link to download your file: https://example.com/ks283dj"
                msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
            }
            // Send the message
            bot.Send(msg)
        }
    })
    log.Printf("About to listen on 0.0.0.0:8443:8443")
    errServe := http.ListenAndServeTLS("0.0.0.0:8443", "fullchain.pem", "privkey.pem", nil)
    log.Fatal(errServe)
}

Upvotes: 1

Views: 1502

Answers (1)

SelfLearnedNoob
SelfLearnedNoob

Reputation: 33

The Webhook setup is wrong here:

Instead of:

_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://domain.app:8443/" + bot.Token))

http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request)

Use:

_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://domain.app:8443/bot" + bot.Token))

http.HandleFunc("/bot"+bot.Token, func(w http.ResponseWriter, r *http.Request)

As described here: https://core.telegram.org/bots/api:

All queries to the Telegram Bot API must be served over HTTPS and need to be presented in this form: https://api.telegram.org/bot/METHOD_NAME.

Upvotes: 1

Related Questions