【问题标题】:facebook messenger bot encoding errorfacebook messenger bot编码错误
【发布时间】:2016-08-16 13:42:30
【问题描述】:

我使用 facebook messenger api 和 wit.ai 操作编写了示例回显消息机器人。

收到我来自 facebook 页面的消息,并且使用 wit api 定义的正确操作函数也被调用。然而 在返回响应时,我收到以下错误 -

哎呀!将响应转发到:错误:(#100)参数消息[文本]必须是UTF-8编码的字符串 在 fetch.then.then.json (/app/index.js:106:13) 在 process._tickCallback (internal/process/next_tick.js:103:7)

这是用于返回响应的函数 -

const fbMessage = (id, text) => {  
  const body = JSON.stringify({
    recipient: { id },
    message: { text },
  });
  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_ACCESS_TOKEN);
  return fetch('https://graph.facebook.com/v2.6/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json; charset=UTF-8'},
    body
  })
  .then(rsp => rsp.json())
  .then(json => {
    if (json.error && json.error.message) {
      throw new Error(json.error.message);`enter code here`
    }   
    return json;
  });
};

我已经从文档中的 messenger.js 文件中复制了这个函数,因为我只是在尝试 POC。 我在这个函数中检查了 text 和 id 的值,并使用 console.log 语句进行了验证,这些语句都正常运行。

有专家可以帮我解决这个错误吗?

注意 - 我尝试使用 text.toString("utf8"); 对文本进行编码但它将编码字符串返回为[object object],这就是 我从机器人得到的回应。所以它不起作用。

【问题讨论】:

    标签: encoding utf-8 facebook-messenger wit.ai facebook-chatbot


    【解决方案1】:

    node-wit获取最新代码,facebook id使用有变化,

    据Facebook:

    在 5 月 17 日星期二,通过 webhook 传递的用户和页面 ID 格式将 从 int 更改为 string 以更好地支持默认 json 编码器 在 js 中(修剪长整数)。请确保您的应用适用于 从 webhook 以及整数返回的字符串 id。

    您仍然遇到 api 问题,请尝试添加 if(event.message && !event.message.is_echo) 条件,如下面的代码所示。

     // Message handler
     app.post('/webhook', (req, res) => {
       const data = req.body;
        if (data.object === 'page') {
          data.entry.forEach(entry => {
            entry.messaging.forEach(event => {
             if (event.message && !event.message.is_echo) {
                const sender = event.sender.id;
               const sessionId = findOrCreateSession(sender);
               const {text, attachments} = event.message;
               if (attachments) {
                 fbMessage(sender, 'Sorry I can only process text messages for now.')
                 .catch(console.error);
               } else if (text) {
                 wit.runActions(
                   sessionId, // the user's current session
                   text, // the user's message
                   sessions[sessionId].context // the user's current session state
                 ).then((context) => {
                   console.log('Waiting for next user messages');
                   sessions[sessionId].context = context;
                 })
                 .catch((err) => {
                   console.error('Oops! Got an error from Wit: ', err.stack || err);
                 })
               }
             } else {
               console.log('received event', JSON.stringify(event));
             }
           });
         });
       }
       res.sendStatus(200);
     });
    

    参考:
    no matching user bug
    no matching user fix

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2017-11-10
      • 2016-11-09
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      相关资源
      最近更新 更多