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

Implementing addReaction function #427

Merged
merged 25 commits into from Mar 30, 2017

Conversation

gamelaster
Copy link
Contributor

@gamelaster gamelaster commented Mar 26, 2017

ToDo:

  • Make it working
  • Add remove
  • Adding info to readme
  • Adding support for parsing reactions in listen or threadlist
  • Check which all emojis are supported

@mangotec or @ravkr
Could you test if it works? For me I getting error.
image

@zoeyim
Copy link

zoeyim commented Mar 26, 2017

I am getting the same error

@gamelaster
Copy link
Contributor Author

gamelaster commented Mar 26, 2017

@mangotec I did changes, try it now, actually I got a error from utils.js, but it's not related to my code. Test it now. If it not will work, probably encodeURIComponent will be required
image

@zoeyim
Copy link

zoeyim commented Mar 26, 2017

Yeah getting the same error as you

@zoeyim
Copy link

zoeyim commented Mar 26, 2017

OK so I got it to work by changing doc_id to what was in the request, it is not the same as the thread id, but i am not sure what it is

Edit: it seems doc_id is always 1491398900900362 after trying multiple accounts talking to different people and in groups/pms

@zoeyim
Copy link

zoeyim commented Mar 26, 2017

And it seems you cannot use emojis other than the default ones :(

@gamelaster
Copy link
Contributor Author

gamelaster commented Mar 26, 2017

@mangotec
By using your doc_id it's works (on my country it still not visisble, but in thread_info I got reactions)! Now wondering wthat doc_id could be, maybe static definition of app or something...
image

@ravkr
Copy link
Contributor

ravkr commented Mar 26, 2017

I can't test it right now, but is there any way to remove reactions?

@gamelaster
Copy link
Contributor Author

@ravkr I don't know, probably yes, but don't know how request looks

@zoeyim
Copy link

zoeyim commented Mar 26, 2017

To remove a reaction you just need to replace "ADD_REACTION" with "REMOVE_REACTION"

Edit: Although the request has the emoji that you want to remove, you can actually just use any string and it will still successfully remove the reaction.

@gamelaster
Copy link
Contributor Author

@mangotec Thank you! I will implement that!

@Schmavery
Copy link
Owner

Great to see this collaboration! Let me know when it's ready to merge :)

@ravkr
Copy link
Contributor

ravkr commented Mar 28, 2017

@gamelaster you could use git rebase instead of merging from master, it just looks cleaner :D

@gamelaster
Copy link
Contributor Author

@ravkr yeah,I like rebase more than merging, but I don't know why, it's merged instead of rebase/fast-forward :D

@ravkr
Copy link
Contributor

ravkr commented Mar 28, 2017

you can try this next time 😄

git remote add upstream git@github.com:Schmavery/facebook-chat-api
git fetch upstream
git rebase upstream/master
git push -f origin master

@gamelaster
Copy link
Contributor Author

Okay, I got the reactions update! Let's go finish it 😊

@ravkr ahh yeah, I used merge instead rebase (when I use merge, it will in some conditions rebase only the code)

@gamelaster
Copy link
Contributor Author

gamelaster commented Mar 29, 2017

It's ready!
@Schmavery please review the changes! 😊
BTW: There is a new delta "event", ClientPayload, it's encrypted JSON, already just contains reactions and something like PIN, #433

var utils = require("../utils");
var log = require("npmlog");

var clientMutationId = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me nervous. Can you store this in the ctx object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,45 @@
"use strict";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to include this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right! Fixed

@gamelaster
Copy link
Contributor Author

Oyy, I again merged instead of rebase! 😆 All reviews has been solved!


var variables = {
data: {
client_mutation_id: ctx.clientMutationId++,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will give NaN if ctx.clientMutationId is not initialized to a number.
You can fix that here: https://github.com/Schmavery/facebook-chat-api/blob/master/index.js#L58

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, sorry I must have missed that.

utils.js Outdated
@@ -375,7 +375,8 @@ function formatDeltaMessage(m){
messageID: md.messageId,
attachments: (m.delta.attachments || []).map(v => _formatAttachment(v)),
timestamp: md.timestamp,
isGroup: !!md.threadKey.threadFbId
isGroup: !!md.threadKey.threadFbId,
reactions: originalMessage.reactions ? originalMessage.reactions : []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I going crazy or is originalMessage not defined here? Can you make sure to test this change? deltaMessage can have slightly different behaviour than the other one (unless you're not getting delta messages?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, when I thinking about this, the reactions in deltaMessage is not required because all reactions sync going through ClientPayload

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand. Can you explain more? Does delta message not have info about reactions?
Mhh... because it sends the delta message right away, so no one could have reacted to it yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, Delta message doesn't have info about reactions. But, the information about reaction is still in /pull/ request, but marked as ClientPayload. So first you get DeltaMessage and then ClientPayload contains Message Reaction

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think that can work.

if (clientPayload && clientPayload.deltas) {
for (var i in clientPayload.deltas) {
var delta = clientPayload.deltas[i];
if (delta.deltaMessageReaction) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just one more thing. Can you explicitly create and return a new object? This will help if facebook changes the format of the object they send us, I believe.
Why do you delete delta.threadKey and delta.action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think threadKey and action is useless.
And I did that new object.

DOCS.md Outdated
__Arguments__

* `reaction`: A string contains `emoji`, `emoji shortcut`, `emoji in unicode` or left `empty string` for delete reaction (look down for list of supported emojis)
* `messageID`: A string or number representing a thread. It happens to be someone's userId in the case of a one to one conversation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A string or number representing a thread
so... it is threadID or messageID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! It's a messageID !

src/listen.js Outdated
var delta = clientPayload.deltas[i];
if (delta.deltaMessageReaction) {
delta.deltaMessageReaction.type = "message_reaction";
delta.threadId = delta.threadKey.threadFbId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i used this code:

var fs = require("fs");
var login = require("facebook-chat-api");

login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
	if(err) return console.error(err);

	api.setOptions({listenEvents: true, logLevel: "silly"});

	var stopListening = api.listen((err, event) => {
		if(err) return console.error(err);

		if (event.type === "message") {
			console.log(JSON.stringify(event));
			//api.addReaction("👍", event.threadID, event.messageID);
			api.setMessageReaction("\uD83D\uDE0D", event.messageID, (err) => {
				console.error(err);
			})
		}
	});
});

and I got this error:

verb parseAndCheckLogin for (;;); {"t":"msg","seq":12,"u":100015305950325,"ms":[{"ofd_ts":1490827124159,"delta":{"payload":[123,34,100,101,108,116,97,115,34,58,91,123,34,100,101,108,116,97,77,101,115,115,97,103,101,82,101,97,99,116,105,111,110,34,58,123,34,116,104,114,101,97,100,75,101,121,34,58,123,34,111,116,104,101,114,85,115,101,114,70,98,73,100,34,58,49,48,48,48,48,49,51,51,49,51,56,53,53,55,48,125,44,34,109,101,115,115,97,103,101,73,100,34,58,34,109,105,100,46,36,99,65,65,65,65,66,102,56,100,69,74,100,104,84,88,51,100,89,70,98,72,68,105,87,101,117,88,101,80,34,44,34,97,99,116,105,111,110,34,58,48,44,34,117,115,101,114,73,100,34,58,49,48,48,48,49,53,51,48,53,57,53,48,51,50,53,44,34,114,101,97,99,116,105,111,110,34,58,34,92,117,48,48,102,48,92,117,48,48,57,102,92,117,48,48,57,56,92,117,48,48,56,100,34,44,34,115,101,110,100,101,114,73,100,34,58,49,48,48,48,48,49,51,51,49,51,56,53,53,55,48,44,34,111,102,102,108,105,110,101,84,104,114,101,97,100,105,110,103,73,100,34,58,34,54,50,53,50,57,56,50,49,54,50,51,54,54,55,53,54,55,53,49,34,125,125,93,125],"class":"ClientPayload"},"type":"delta","iseq":283814,"queue":100015305950325}]}
info listen Got answer in 282
ERR! listen TypeError: Cannot read property 'threadFbId' of undefined
ERR! listen     at parsePackets (/var/www/node/react/node_modules/facebook-chat-api/src/listen.js:163:55)
ERR! listen     at Array.forEach (native)
ERR! listen     at /var/www/node/react/node_modules/facebook-chat-api/src/listen.js:107:12
ERR! listen     at tryCatcher (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/util.js:26:23)
ERR! listen     at Promise._settlePromiseFromHandler (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:510:31)
ERR! listen     at Promise._settlePromiseAt (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:584:18)
ERR! listen     at Promise._settlePromises (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/promise.js:700:14)
ERR! listen     at Async._drainQueue (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:123:16)
ERR! listen     at Async._drainQueues (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:133:10)
ERR! listen     at Immediate.Async.drainQueues [as _onImmediate] (/var/www/node/react/node_modules/facebook-chat-api/node_modules/bluebird/js/main/async.js:15:14)
ERR! listen     at processImmediate [as _immediateCallback] (timers.js:383:17)
ERR! listen  [TypeError: Cannot read property 'threadFbId' of undefined]
[TypeError: Cannot read property 'threadFbId' of undefined]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange, after decoding your clientPayload, I got this response:
image
It's different than mine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, In my old parsed files, It's contains threadFbId, but today new parsed I have otherUserFbId too ! Maybe hotfix or something from Facebook side?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow 100% but if it seems like a value is sometimes in one place and sometimes in another, please support both 😄
Sometimes people have different versions and it would be great to have the api work for everyone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

case ":thumbsdown:":
reaction = "\uD83D\uDC4E";
default:
return callback({error: "reaction is not valid emoji"});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to add breaks to cases, now it always calls callback with error.
and maybe "Reaction is not a valid emoji." instead of "reaction is not valid emoji"?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thanks @ravkr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks @ravkr !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you've added one break, but it needs a break after every reaction = ...;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea, sorry, when I arrive home I will fix that 🤣

@gamelaster
Copy link
Contributor Author

Should be okay right now I hope! 😆

@Schmavery
Copy link
Owner

Work for you @ravkr?

DOCS.md Outdated
* 😢 - Unicode: `\uD83D\uDE22`, Shortcut: `:cry:`
* 😠 - Unicode: `\uD83D\uDE20`, Shortcut: `:angry:`
* 👍 - Unicode: `\uD83D\uDC4D`, Shortcut: `:thumbsup:`
* 👎 - Unicode: `\uD83D\uDC4E`, Shortcut: `:thumbsdown:`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it would be better if reaction names shortcuts were unified with the names of the reactions for Facebook posts, i.e.:

  • :love:
  • :haha:
  • :wow:
  • :sad:
  • :angery:
  • :like:
  • :dislike:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivkos hmm, probably you are right, I just used an "unified" emoji shortcuts. Let's left decide to @Schmavery

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the unified ones, we could potentially support both. @ivkos do you have a source for these "canonical" facebook reaction names?

Copy link
Contributor

@ivkos ivkos Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Schmavery They are not really canonical. It's what they're called in the Facebook UI and how most users know them. Reactions in Messenger don't have visible names in the UI, but most of my friends call them basically with the names of the corresponding reactions in Facebook.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but facebook's :love: is heart, not eyes with hearts... :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, I added support for Facebook shortcuts 😊

@ravkr
Copy link
Contributor

ravkr commented Mar 30, 2017

everything seems to work fine 😄

my test code:

var fs = require("fs");
var login = require("facebook-chat-api");

var x = 0;
var reactions = [
    "\uD83D\uDE0D",
    "\uD83D\uDE06",
    "\uD83D\uDE2E",
    "\uD83D\uDE22",
    "\uD83D\uDE20",
    "\uD83D\uDC4D",
    "\uD83D\uDC4E",
    ":heart_eyes:",
    ":laughing:",
    ":open_mouth:",
    ":cry:",
    ":angry:",
    ":thumbsup:",
    ":thumbsdown:"
]

login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
    if(err) return console.error(err);

    api.setOptions({listenEvents: true, logLevel: "silly"});

    var stopListening = api.listen((err, event) => {
        if(err) return console.error(err);

        if (event.type === "message" && event.body.toLowerCase().indexOf("rav") === 0) {
            console.log(JSON.stringify(event));
            api.setMessageReaction(reactions[x], event.messageID, (err) => {
                console.error(err);
            })
            console.log(`reaction test #${x}`);
            x = (x+1)%reactions.length;
        }
    });
});

zrzut ekranu z 2017-03-30 20 12 19

reaction = "\uD83D\uDE22";
break;
case ":angry:":
case ":angery:":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was probably typo in suggestion... :/ on facebook it is still angry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it from list which provided me @ivkos, but as I remember, it is angery, but not sure!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but he must have made mistake :(
zrzut ekranu z 2017-03-30 20 46 32

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, it's angry (checked myself too)! Fixed

@Schmavery Schmavery merged commit 5676a33 into Schmavery:master Mar 30, 2017
@Schmavery
Copy link
Owner

Schmavery commented Mar 30, 2017

Thanks a lot for this @gamelaster! I appreciate you taking all my little bits of feedback into account and finishing this so quickly!
Thanks also to @ravkr for the great test work :)
I love our little facebook-chat-api community! 😄

bsansouci pushed a commit that referenced this pull request Apr 1, 2017
* Implementing setMessageReaction function

* Adding documentation for setMessageReaction

* Adding Facebook emoji shortcuts to setMessageReaction
how2945ard pushed a commit to how2945ard/facebook-chat-api that referenced this pull request May 30, 2017
* Implementing setMessageReaction function

* Adding documentation for setMessageReaction

* Adding Facebook emoji shortcuts to setMessageReaction
how2945ard pushed a commit to how2945ard/facebook-chat-api that referenced this pull request May 30, 2017
* Implementing setMessageReaction function

* Adding documentation for setMessageReaction

* Adding Facebook emoji shortcuts to setMessageReaction
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants