2

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;
  });
};
  • Where is `text` coming from? You realize that you aren't *using* the utf8 library, right? It is called but the results of that call are never saved. Needs e.g. `const utf8Text = utf8.encode(text);` – Codebling Jan 18 '22 at 06:12
  • Have you examined `text` before it gets sent? Is it possible that it's a `Buffer` or something? – Codebling Jan 18 '22 at 06:13
  • @Codebling, forgot to remove the utf8 usage, but when what you suggested is done: this error is thrown: UnhandledPromiseRejectionWarning: Error: (#100) Invalid keys "utf8Text" were found in param "message". Also, for your 2nd comment, I have logged text before it's being sent, and it's just the json of the message received from messenger: { text: 'delivery', intents: [ { id: '3066498216993712', name: 'delivery', confidence: 0.8579 } ], entities: {}, traits: {} } – thirdofthethirds Jan 18 '22 at 06:31
  • Have you tried with just `text: 'devliery'`? Because that text will not change in UTF8, there are no non-ASCII characters. And you are getting that error because you put `{ utf8Text }` instead of `{ text: utf8Text }`. Or you could rename the parameter 'originalText' and stick with 'text'..it was just an example. – Codebling Jan 18 '22 at 18:00

1 Answers1

1

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, i.e 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.

orabis
  • 2,749
  • 2
  • 13
  • 29