简体   繁体   中英

Weird error when trying out wit.ai's messenger bot example code

Trying to learn wit.ai and create a messenger bot via their example code from their github. after messing around and adding my own take, I encountered this error:

UnhandledPromiseRejectionWarning: Error: (#100) Param message[text] must be a UTF-8 encoded string

I've tried using the UTF8 package ( https://www.npmjs.com/package/utf8 ) but I don't think it resolves this issue. I believe this has also been asked years before ( facebook messenger bot encoding error ) but the solution provided there seemed to have been present in the original wit.ai sample code as of current yet I'm still getting the error.

This is the function where the error is thrown:

const fbMessage = (id, text) => {      
  const body = JSON.stringify({
    recipient: { id },
    message: { text },
  });
  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;
  });
};

The "v15.0/me/messages" API expects a message to either contain attachment or text.

Text:

{
  "recipient": {"id": "some_valid_id"},
  "message": {
    "text": "some_string"
  }
}

Or attachment, which expects following FB templates , which are in JSON:

{
   "recipient": {"id": "some_valid_id"},
   "message":{
      "attachment":{
         "type":"template",
         "payload":{
            "template_type":"generic",
            "elements":[
               {
                  "title":"example_title",
                  "subtitle":"example_subtitle",
                  "image_url":"some_url",
                  "buttons":[
                     {
                        "type":"postback",
                        "title":"Option1",
                        "payload":"opt1"
                     },
                     {
                        "type":"postback",
                        "title":"Option2",
                        "payload":"opt2"
                     }
                  ]
               }
            ]
         }
      }
   }
}

Based on the above, it is either "text" for a simple string, or "attachment" for a rich message.

Returning back to your error. When using a value other than an encoded string for text, you will get the exact error:

UnhandledPromiseRejectionWarning: Error: (#100) Param message[text] must be a UTF-8

You will notice that this is what you are doing in your code, ie using json with text instead of attachment.

I hope this will help fix the issue for you, but I fixed mine using what I mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM