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;
});
};