Skip to content
This repository was archived by the owner on Jul 9, 2022. It is now read-only.

Unexpected token < in JSON at position 0 #895

Closed
PedjPedj opened this issue Dec 10, 2021 · 11 comments
Closed

Unexpected token < in JSON at position 0 #895

PedjPedj opened this issue Dec 10, 2021 · 11 comments

Comments

@PedjPedj
Copy link

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);
}
@w3webtool
Copy link

access listenmqtt fix this line to debuffer

@PedjPedj
Copy link
Author

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.

@d00ML0rDz
Copy link

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

@afish
Copy link

afish commented Dec 12, 2021

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>

@BadAimWeeb
Copy link
Contributor

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
Copy link

afish commented Dec 12, 2021

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.

@w3webtool
Copy link

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 

@w3webtool
Copy link

line 117

BadAimWeeb added a commit to BadAimWeeb/facebook-chat-api that referenced this issue Dec 12, 2021
@BadAimWeeb
Copy link
Contributor

BadAimWeeb commented Dec 13, 2021

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.

@PedjPedj
Copy link
Author

PedjPedj commented Dec 13, 2021

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.

@PedjPedj
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants