1

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

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

哎呀!将响应转发到:错误:(#100)Param message[text] must be an UTF-8 encoding string at fetch.then.then.json (/app/index.js:106:13) at process ._tickCallback (内部/进程/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] 返回,这就是我从 bot 得到的响应。所以它不起作用。

4

1 回答 1

0

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

据 Facebook 称:

5 月 17 日星期二,通过 webhook 传递的用户和页面 id 格式将从 int 更改为 string,以更好地支持 js 中的默认 json 编码器(修剪长整数)。请确保您的应用可以使用从 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);
 });

参考:
没有匹配的用户错误
没有匹配的用户修复

于 2016-08-28T13:52:37.887 回答