Schmavery/facebook-chat-api

Unexpected token < in JSON at position 0

PedjPedj opened this issue · 11 comments

Hi, I keep having this error. Please help.
It seems to be having problem parsing the message. I'm not really sure.
I'm a JS noob.

undefined:1
<%�
^

SyntaxError: Unexpected token  in JSON at position 0

2021-12-10_10-39

Code:

const fs = require('fs');
const login = require('facebook-chat-api');

let credentials = {
    appState: JSON.parse(fs.readFileSync(__dirname + `/data/fbCookies.json`, `utf8`)),
};

try {
    login(credentials, (err, api) => {
        if(err) return console.error("error in Login API", err);

        api.listenMqtt((err, message) => {
            console.log(message);

        });

    });

} catch (err) {
    console.error(err);
}

access listenmqtt fix this line to debuffer

access listenmqtt fix this line to debuffer

Hi, Appreciate the reply but I'm not sure which line in listenmqtt you're referring and how I'll "debuffer" it. I'm sorry, I'm just new to this. I'm still learning.

Seeing the same issue. Started occurring a couple of days ago.

afish commented

In my case the line to parse was
<Buffer 19 3c 25 d0 01 19 05 00 25 ca 01 19 05 00 25 a8 01 19 05 00 00>

In my case the line to parse was <Buffer 19 3c 25 d0 01 19 05 00 25 ca 01 19 05 00 25 a8 01 19 05 00 00>

i've never seen that format before, could you tell me what topic did you get that from?

afish commented

I do this:

api.setOptions({listenEvents: true});
	api.setOptions({selfListen: true})

	var stopListening = api.listenMqtt((err, event) => {
		if(err) signalError(err);
		messageReceived(event, api);
	});

It looks like the sequence is:
Contact X wants to text me.
I get initial typ message (that X is typing).
Seconds later I get another typ message (X is still typing).
I get the actual message from X.
I get this fancy buffer which breaks the application.

Seems like I get this message every time someone texts me (or when I text someone as I'm self listening). Can't tell whether there is a pattern in that but I'm getting these messages for couple days now.

mqttClient.on('message', function(topic, message, packet) {

let jsonMessage = Buffer.from(message).toString();
try {
  jsonMessage = JSON.parse(jsonMessage);
}
catch {
  jsonMessage = {};
}

try change it in listenMqtt.js 

line 117

If anyone is having this problem, please install my test branch (by using the command below) and edit your code so that it will log the error (preferability to file to ensure that you don't miss the error) and publish the error dump here. Thank you.

npm i https://github.com/BadAimWeeb/facebook-chat-api.git#test-mqttformat

Note: This test branch is not based on fca-unofficial, if you're running c3cbot/Mirai or something based on my own fork, DO NOT APPLY THIS TEST BRANCH. Some API from my fork IS NOT COMPATIBLE with the base facebook-chat-api.

mqttClient.on('message', function(topic, message, packet) {


let jsonMessage = Buffer.from(message).toString();
try {
  jsonMessage = JSON.parse(jsonMessage);
}
catch {
  jsonMessage = {};
}

try change it in listenMqtt.js

line 117

Thanks @w3webtool, It seems that it's working now. Tho I didn't tested it much yet.

I guess I should close this issue now.. Thanks everyone who helped!