facebook messenger bot 编码错误

facebook messenger bot encoding error

我已经使用 Facebook Messenger api 和 wit.ai 操作编写了示例回显消息机器人。

收到我来自 facebook 页面的消息,并且还调用了使用机智 api 定义的正确操作函数。然而 在 return 处理响应时,我收到以下错误消息 -

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

这是用于 return 响应的函数 -

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。 我检查了此函数中文本和 id 的值,并使用 console.log 语句进行了验证,这些都正常。

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

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

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

根据 Facebook:

On Tue May 17 format of user and page ids delivered via webhooks will change from an int to a string to better support default json encoder in js (that trims long ints). Please make sure your app works with string ids returned from webhooks as well as with ints.

您仍然遇到 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