英文:
Telegram Bot Webhook - Connection Refused (Golang)
问题
我已经配置了一个Telegram机器人,根据以下配置使用Webhooks:
操作系统:Ubuntu
服务器:Apache2
虚拟主机:
- 成功链接到域名
- 监听端口8843
- 端口443、8843都已通过ufw allow打开
Webhook:
- 设置为"domain.app:8843/" + bot.Token
- HandleFunc设置为"/" + bot.Token
- 使用certFile和keyFile成功使用ListenAndServeTLS
然而,我的机器人仍然无法工作,我已经花了整整18个小时来尝试修复它。现在已经是凌晨3点了,我已经快要崩溃了。
当我启动我的二进制文件时,它会将以下内容打印到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
如果有人有任何想法,请帮帮我!
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"}}
如果我向机器人发送启动命令,pending_update_count实际上也会增加,所以这是一个非常奇怪的问题。
我的代码:
func main() {
// 创建一个新的机器人
bot, err := tgbotapi.NewBotAPI("PLACEHOLDER")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// 设置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)
}
// 启动Webhook服务器
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
}
// 检查更新是否为回调查询(按钮按下)
if update.CallbackQuery != nil {
callbackQuery := update.CallbackQuery
data := callbackQuery.Data
// 创建一个新的消息
msg := tgbotapi.NewMessage(callbackQuery.Message.Chat.ID, "")
// 根据按下的按钮设置文本和键盘
switch data {
case "get_information":
msg.Text = "这是一些关于支持的格式的信息:"
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
case "start_file_conversion":
msg.Text = "太棒了!点击链接下载文件:https://example.com/ks283dj"
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
}
// 发送消息
bot.Send(msg)
}
})
log.Printf("即将监听0.0.0.0:8443:8443")
errServe := http.ListenAndServeTLS("0.0.0.0:8443", "fullchain.pem", "privkey.pem", nil)
log.Fatal(errServe)
}
希望这可以帮助你解决问题!
英文:
I have configured a Telegram bot to use Webhooks according to the following configuration:
OS: Ubuntu
Server: Apache2
Virtual Host:
-
Sucessfully linked to domain
-
Listening on port 8843
-
Ports 443, 8843 all open with ufw allow
Webhook:
-
Set to "domain.app:8843/" bot.Token
-
HandleFunc set to "/" + bot.Token
-
certFile and keyFile are used succesfully with ListenAndServeTLS
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)
}
答案1
得分: 1
Webhook设置在这里是错误的:
原来的代码是:
_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://domain.app:8443/" + bot.Token))
http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request)
应该使用以下代码:
_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://domain.app:8443/bot" + bot.Token))
http.HandleFunc("/bot"+bot.Token, func(w http.ResponseWriter, r *http.Request)
如https://core.telegram.org/bots/api中所述:
所有对Telegram Bot API的请求必须通过HTTPS进行服务,并且需要以以下形式呈现:
https://api.telegram.org/bot/METHOD_NAME。
英文:
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<token>/METHOD_NAME.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论