Encoding facebook messenger机器人编码错误

Encoding facebook messenger机器人编码错误,encoding,utf-8,facebook-messenger,wit.ai,facebook-chatbot,Encoding,Utf 8,Facebook Messenger,Wit.ai,Facebook Chatbot,我已经使用facebook messenger api和wit.ai操作编写了echo消息bot示例 从facebook页面收到我的消息,并调用使用wit api定义的正确操作函数。然而 在返回响应时,我得到以下错误信息: 哎呀!将响应转发到时出错:错误:(#100)Param message[text]必须是UTF-8编码的字符串 在fetch.then.then.json(/app/index.js:106:13) 在进程中。_tick回调(内部/process/next_tick.js:1

我已经使用facebook messenger api和wit.ai操作编写了echo消息bot示例

从facebook页面收到我的消息,并调用使用wit api定义的正确操作函数。然而 在返回响应时,我得到以下错误信息:

哎呀!将响应转发到时出错:错误:(#100)Param message[text]必须是UTF-8编码的字符串 在fetch.then.then.json(/app/index.js:106:13) 在进程中。_tick回调(内部/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],这就是 我从机器人那里得到的回应。所以它不起作用。

从获取最新代码,facebook id的使用发生了变化,

根据Facebook:

5月17日星期二,通过webhooks交付的用户和页面ID的格式将 将int改为字符串以更好地支持默认json编码器 在js中(修剪长整数)。请确保你的应用程序与 从webhooks以及ints返回的字符串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);
 });
参考:

从获取最新代码,facebook id使用发生变化,

根据Facebook:

5月17日星期二,通过webhooks交付的用户和页面ID的格式将 将int改为字符串以更好地支持默认json编码器 在js中(修剪长整数)。请确保你的应用程序与 从webhooks以及ints返回的字符串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);
 });
参考: