You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Facebook Messenger Bot postback.reply异常:跨页面消息混淆

奇怪的Messenger Postback回复错误:发送了其他页面的确认消息

我在生产环境使用一款Rails gem开发了一款Messenger聊天机器人,核心功能是发送订阅邀请。当用户点击订阅按钮时,Webhook会触发payload为SUB_YES_PAYLOAD的postback,用来保存订阅者信息,同时回复用户自定义的确认消息(不同页面的消息各不相同)。

但最近遇到了一个非常诡异的问题:有时候postback的回复会发送其他页面的确认消息,我已经排查了好几个小时,却始终找不到问题所在。

Webhook核心代码

Bot.on :postback do |postback|
  # 通过Facebook发送的页面ID获取对应的CoreBot记录
  @core_bot = CoreBot.find_by_page_id(postback.recipient['id'])

  if postback.payload == 'SUB_YES_PAYLOAD'
    # 检查数据库中是否已存在该订阅者
    if BotUser.find_by_sender_id(postback.sender['id']).present? == false
      # 调用Facebook API获取用户信息
      url = "https://graph.facebook.com/v2.6/" + postback.sender['id'] + "?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=" + @core_bot.page_access_token
      resp = Net::HTTP.get_response(URI.parse(url))
      @user_data = JSON.parse(resp.body)

      @first_name = @user_data['first_name']
      @last_name = @user_data['last_name']
      @profile_pic = @user_data['profile_pic']
      @locale = @user_data['locale']
      @timezone = @user_data['timezone']
      @gender = @user_data['gender']

      # 创建并保存订阅者记录
      @bot_user = BotUser.new(
        core_bot_id: @core_bot.id,
        sender_id: postback.sender['id'],
        first_name: @first_name,
        last_name: @last_name,
        profile_pic: @profile_pic,
        locale: @locale,
        timezone: @timezone,
        gender: @gender
      )
      
      if @bot_user.save
        # 如果用户设置了自定义确认消息就发送,否则发送默认欢迎语
        if @core_bot.yes_subscribe_message.present? == true
          postback.reply({ text: @core_bot.yes_subscribe_message # 问题出在这里,有时会发送错误的消息 })
        else
          postback.reply({ text: "Welcome!" })
        end
      end
    end
  end
end

错误现象示例

错误的回复消息

看起来问题出在调用@core_bot.yes_subscribe_message的时候,@core_bot对象不是当前页面的记录,但诡异的是,订阅者已经被正确保存到了对应的@core_bot.id的记录里,逻辑上不该出现这种对象“串位”的情况。

我的应用部署在Heroku标准Web dyno,使用Heroku Postgres Hobby经典数据库。

补充CoreBot模型代码

# id :integer not null, primary key
# user_id :integer
# page_name :string
# page_id :integer
# page_access_token :string
# greeting_message :string
# yes_subscribe_button :string
# no_subscribe_button :string
# yes_subscribe_message :string
# no_subscribe_message :string
# created_at :datetime not null
# updated_at :datetime not null
# active :boolean default(TRUE)
# picture :string default("https://scontent.xx.fbcdn.net/v/t1.0-1/p480x480/20729408_135562287047146_4447605348389117589_n.png?oh=ba7b4a319a002db384168f50e1ccfec5&oe=5AAE506E")

class CoreBot < ApplicationRecord
  validates_uniqueness_of :page_id
  validates :page_id, :presence => true
  has_secure_token
  belongs_to :user
  has_many :letters, dependent: :destroy
  has_many :bot_users, dependent: :destroy
  has_many :weekly_analytics, dependent: :destroy
  has_many :segments, dependent: :destroy
  has_many :sequences, dependent: :destroy
  has_many :invitations, dependent: :destroy
  has_one :checkbox_iframe, dependent: :destroy
  has_one :button_iframe, dependent: :destroy
end

恳请各位大佬帮忙排查下这个问题,感谢支持!

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

火山引擎 最新活动