【问题标题】:Why don't quick replies I set on Wit.ai work on facebook messenger when deployed?为什么我在 Wit.ai 上设置的快速回复在部署时不能在 Facebook Messenger 上运行?
【发布时间】:2016-11-15 11:20:14
【问题描述】:

我正在使用 Wit.ai 在 facebook messenger 上创建一个聊天机器人。但是,如果我为某个用户输入设置快速回复,部署机器人并输入所述输入,我只会看到快速回复按钮应该遵循的文本。

【问题讨论】:

    标签: facebook facebook-graph-api facebook-messenger wit.ai


    【解决方案1】:

    我假设您使用的是 Node.js SDK 中的 messenger 示例。

    此代码不支持快速回复。首先,您可以使用我的代码在sendTextMessage 函数中实现所需的功能:

    const sendMessage = (id, message) => {
      const body = JSON.stringify({
        recipient: { id },
        message: message,
      });
      const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
      return fetch('https://graph.facebook.com/me/messages?' + qs, {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body,
      })
      .then(rsp => rsp.json())
      .then(json => {
        if (json.error && json.error.message) {
          throw new Error(json.error.message);
        }
        return json;
      });
    };
    
    const sendTextMessage = (id, text, quickreplies) => {
      if(!quickreplies) return sendMessage(id, {text});
    
      if(quickreplies.length > 10) {
        throw new Error("Quickreplies are limited to 10");
      }
    
      let body = {text, quick_replies: []};
      quickreplies.forEach(qr => {
        body.quick_replies.push({
          content_type: "text",
          title: qr,
          payload: 'PAYLOAD' //Not necessary used but mandatory
        });
      });
      return sendMessage(id, body);
    };
    

    其次,您必须考虑“发送”操作中的快速回复。这是一个非常简单的“发送操作”示例:

    const actions = {
      send({sessionId}, {text, quickreplies}) {
        const recipientId = sessions[sessionId].fbid;
        return sendTextMessage(recipientId, text, quickreplies);
      }
    }
    

    编辑:另外,请注意,信使快速回复限制为 20 个字符。

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多