我已经根据以下配置配置了一个Telegram机器人来使用Webhook:
操作系统:Ubuntu
服务器:Apache2
虚拟主机:
- 成功链接到域
- 侦听端口8843
- 端口443、8843全部打开,允许ufw
网钩:
- Set to "domain.app:8843/" bot.Token
- HandleFunc设置为"/"+机器人令牌
- certFile和keyFile已成功用于ListenAndServeTLS
然而,我的机器人还是不工作,花了整整18个小时试图修复它。现在是晚上3点,我已经无计可施了。
当我启动我的二进制文件时,它将以下内容打印到stdout:
2023/01/07 01:50:15授权记帐示例记帐
2023年1月7日01:50:15设置网络连接,响应:{"确定":真,"结果":真,"描述":"已设置Webhook "}
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回拨电报失败:连接被拒绝
如果有人知道的话请帮帮我!
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"}}
如果我向bot发出start命令,pending_update_count实际上也会增加,所以这是一个非常奇怪的问题。
我的代码:
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条答案
按热度按时间tvokkenx1#
Webhook设置在此处错误:
取代:
use:
如下所述:https://core.telegram.org/bots/api:
对Telegram Bot API的所有查询都必须通过HTTPS提供,并且需要以以下形式显示:https://api.telegram.org/bot/方法名称。