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

Telegram Bot通过WebHook接收/start时返回my_chat_member而非message问题

Telegram Bot接收/start命令的WebHook异常问题

我开发了一个Telegram Bot,用户发送/start命令后,服务器通过WebHook接收请求。

正常请求格式(99%场景)

绝大多数情况下,Telegram发送的请求包含message对象,明确携带/start命令文本:

{
   "update_id":99999999,
   "message":{
      "message_id":9999,
      "from":{
         "id":999999999,
         "is_bot":false,
         "first_name":"first_name",
         "last_name":"last_name",
         "language_code":"code"
      },
      "chat":{
         "id":99999999,
         "first_name":"first_name",
         "last_name":"last_name",
         "type":"private"
      },
      "date":1665383118,
      "text":"/start",
      "entities":[
         {
            "offset":0,
            "length":6,
            "type":"bot_command"
         }
      ]
   }
}

异常请求格式(新用户首次使用场景)

部分从未使用过Bot的新用户发送/start时,Telegram发送的请求仅包含my_chat_member对象,没有携带/start命令文本,且该问题在iPhone、Android、PC及Web端均有出现:

{
   "update_id":999999999,
   "my_chat_member":{
      "chat":{
         "id":999999999,
         "first_name":"first_name",
         "type":"private"
      },
      "from":{
         "id":999999999,
         "is_bot":false,
         "first_name":"first_name",
         "language_code":"code"
      },
      "date":1665381194,
      "old_chat_member":{
         "user":{
            "id":8888888888,
            "is_bot":true,
            "first_name":"bot_name",
            "username":"bot_name"
         },
         "status":"member"
      },
      "new_chat_member":{
         "user":{
            "id":8888888888,
            "is_bot":true,
            "first_name":"bot_name",
            "username":"bot_name"
         },
         "status":"kicked",
         "until_date":0
      }
   }
}

原因分析

这是Telegram的内置机制:当新用户首次与Bot互动(点击"Start"按钮)时,Telegram会先触发my_chat_member类型的更新,用于同步Bot在用户聊天中的成员状态。部分场景下,message类型的/start请求会延迟发送,甚至不发送,导致服务器仅收到my_chat_member更新。

解决方法

  1. 扩展WebHook的更新类型处理:除了监听message类型的更新,必须同时处理my_chat_member类型的更新。
  2. 将my_chat_member更新视为/start触发信号:当收到my_chat_member更新时,若检测到Bot的状态从非活跃(如kicked)变为活跃(member),即可判定为用户触发了/start,向用户发送欢迎消息或后续引导内容。
  3. 避免重复响应:可通过记录用户ID和事件时间戳,避免同时收到my_chat_member和message时重复发送相同内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 03:50:35