You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram登录小部件回调未触发问题排查求助

问题描述
  • 创建Bot
  • 后端启动Bot并配置/telegram-callback端点
  • 通过@BotFather设置Bot的域名https://www.example.ru/telegram-callback,此时Telegram发起存活检测,Nginx日志显示请求成功:
    149.154.161.244 - - - - [18/Oct/2023:07:50:49 +0300] "GET /telegram-callback HTTP/1.1" 200 0 0 "-" "TelegramBot (like TwitterBot)
    
  • 在Telegram登录小部件生成页面创建登录组件,授权类型设为Callback,请求权限设为true
  • 点击“Login with Telegram”,输入手机号并确认,收到Telegram系统通知后点击确认

完成上述步骤后,Telegram未携带用户参数调用GET /telegram-callback,Nginx和Bot日志均无相关记录。Bot代码如下:

import (
    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
    "go.uber.org/zap"
    "log"
    "net/http"
)

func handleTelegramOAuthCallback(w http.ResponseWriter, r *http.Request) {
    zap.L().Debug("handleTelegramOAuthCallback", zap.String("url", r.URL.String()),
        zap.String("encode", r.URL.Query().Encode()))
}

func main() {
    http.HandleFunc("/telegram-callback", handleTelegramOAuthCallback)

    go func() {
        log.Fatal(http.ListenAndServe(":8080", nil))
    }()

    bot, err := tgbotapi.NewBotAPI("TOKEN")
    if err != nil {
        panic(err)
    }

    bot.Debug = true
    updateConfig := tgbotapi.NewUpdate(0)
    updateConfig.Timeout = 10
    updates := bot.GetUpdatesChan(updateConfig)
    for update := range updates {
        if update.Message == nil {
            continue
        }
        msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
        msg.ReplyToMessageID = update.Message.MessageID
        if _, err = bot.Send(msg); err != nil {
            panic(err)
        }
    }
}
排查与解决方案

1. 登录小部件回调URL匹配问题

登录小部件的data-auth-url参数必须和@BotFather中设置的域名完全一致,包括协议(https)、域名、路径。如果小部件里填的是http://或者路径拼写错误,Telegram不会发起回调。

2. Nginx反向代理配置验证

存活检测请求能通过不代表回调请求能正常转发,检查Nginx配置是否正确转发所有/telegram-callback请求到后端:8080端口,确保没有遗漏请求头或限制请求方法:

location /telegram-callback {
    proxy_pass http://localhost:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

同时查看Nginx错误日志(默认路径/var/log/nginx/error.log),排查是否有请求拦截或转发失败记录。

3. 后端日志级别调整

代码中使用zap.L().Debug()记录回调,但如果Zap未初始化或日志级别高于Debug,这些日志不会输出。临时改用标准库log打印日志,验证是否收到请求:

func handleTelegramOAuthCallback(w http.ResponseWriter, r *http.Request) {
    log.Printf("Received callback: %s, query params: %s", r.URL.String(), r.URL.Query().Encode())
    zap.L().Debug("handleTelegramOAuthCallback", zap.String("url", r.URL.String()),
        zap.String("encode", r.URL.Query().Encode()))
}

4. 请求方法与权限配置检查

  • Telegram登录小部件的Callback授权默认用GET请求,确保Nginx和后端没有限制GET方法访问/telegram-callback。
  • 先将小部件的“请求权限”设为false,测试基础回调是否正常,排除权限不匹配导致的回调跳过问题。

5. 网络与防火墙规则确认

检查服务器防火墙是否允许Telegram IP段访问:8080端口,同时确认服务器能正常访问Telegram API,避免网络链路问题导致回调通知无法送达。


内容的提问来源于stack exchange,提问作者Aleksey Budaev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 13:21:27