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

Commit

Permalink
Implementing setReaction function (#427)
Browse files Browse the repository at this point in the history
* Implementing setMessageReaction function

* Adding documentation for setMessageReaction

* Adding Facebook emoji shortcuts to setMessageReaction
  • Loading branch information
gamelaster authored and Schmavery committed Mar 30, 2017
1 parent 658fd75 commit 5676a33
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 2 deletions.
33 changes: 33 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* [`api.sendMessage`](#sendMessage)
* [`api.sendTypingIndicator`](#sendTypingIndicator)
* [`api.setOptions`](#setOptions)
* [`api.setMessageReaction`](#setMessageReaction)
* [`api.setTitle`](#setTitle)

---------------------------------------
Expand Down Expand Up @@ -651,6 +652,14 @@ Difference between `"read_receipt"` and `"read"`:
- `"read_receipt"` event triggers when other people read the user's messages.
- `"read"` event triggers when the user read other people's messages.
If `type` is `"message_reaction"`, then the object will have following fields (enabled `listenEvents` required):
- `"reaction"`: Contains reaction emoji
- `"userId"`: The reaction senders ID
- `"senderId"`: ID of author the message, where has been reaction added
- `"messageId"`: The ID of message
- `"threadId"`: ID of thread where has been message sent
- `"offlineThreadingId"`: The offline message ID
<a name="presence"></a>
If enabled through [setOptions](#setOptions), `message` could also be a presence object, (`type` will be `"presence"`), which is the online status of the user's friends. That object given to the callback will have the following fields:
- `type`: The string `"presence"`.
Expand Down Expand Up @@ -925,6 +934,30 @@ login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, ap
});
```
---------------------------------------
<a name="setMessageReaction"></a>
### api.setMessageReaction(reaction, messageID[, callback])
Sets reaction on message
__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 representing the message ID.
* `callback(err)` - A callback called when sending the reaction is done.
__Supported Emojis__
* 😍 - Unicode: `\uD83D\uDE0D`, Shortcut: `:heart_eyes:` or `:love:`
* 😆 - Unicode: `\uD83D\uDE06`, Shortcut: `:laughing:` or `:haha:`
* 😮 - Unicode: `\uD83D\uDE2E`, Shortcut: `:open_mouth:` or `:wow:`
* 😢 - Unicode: `\uD83D\uDE22`, Shortcut: `:cry:` or `:sad:`
* 😠 - Unicode: `\uD83D\uDE20`, Shortcut: `:angry:`
* 👍 - Unicode: `\uD83D\uDC4D`, Shortcut: `:thumbsup:` or `:like:`
* 👎 - Unicode: `\uD83D\uDC4E`, Shortcut: `:thumbsdown:` or `:dislike:`
---------------------------------------
<a name="setTitle"></a>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Result:
* [`api.sendMessage`](DOCS.md#sendMessage)
* [`api.sendTypingIndicator`](DOCS.md#sendTypingIndicator)
* [`api.setOptions`](DOCS.md#setOptions)
* [`api.setMessageReaction`](DOCS.md#setMessageReaction)
* [`api.setTitle`](DOCS.md#setTitle)

## Main Functionality
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function buildAPI(globalOptions, html, jar) {
clientID: clientID,
globalOptions: globalOptions,
loggedIn: true,
access_token: 'NONE'
access_token: 'NONE',
clientMutationId: 0
};

var api = {
Expand Down Expand Up @@ -100,6 +101,7 @@ function buildAPI(globalOptions, html, jar) {
'searchForThread',
'sendMessage',
'sendTypingIndicator',
'setMessageReaction',
'setTitle',
];

Expand Down
Empty file added src/forwardMessage.js
Empty file.
20 changes: 20 additions & 0 deletions src/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ module.exports = function(defaultFuncs, api, ctx) {
})(0)
break;
}

if (v.delta.class == "ClientPayload") {
var clientPayload = utils.decodeClientPayload(v.delta.payload);
if (clientPayload && clientPayload.deltas) {
for (var i in clientPayload.deltas) {
var delta = clientPayload.deltas[i];
if (delta.deltaMessageReaction) {
globalCallback(null, {
type: "message_reaction",
threadId: delta.deltaMessageReaction.threadKey.threadFbId ? delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey.otherUserFbId,
messageId: delta.deltaMessageReaction.messageId,
reaction: delta.deltaMessageReaction.reaction,
senderId: delta.deltaMessageReaction.senderId,
userId: delta.deltaMessageReaction.userId
});
}
}
return;
}
}

switch (v.delta.class) {
case 'ReadReceipt':
Expand Down
88 changes: 88 additions & 0 deletions src/setMessageReaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"use strict";

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

module.exports = function(defaultFuncs, api, ctx) {
return function setMessageReaction(reaction, messageID, callback) {
if(!callback) {
callback = function() {};
}

switch (reaction) {
case "\uD83D\uDE0D": //:heart_eyes:
case "\uD83D\uDE06": //:laughing:
case "\uD83D\uDE2E": //:open_mouth:
case "\uD83D\uDE22": //:cry:
case "\uD83D\uDE20": //:angry:
case "\uD83D\uDC4D": //:thumbsup:
case "\uD83D\uDC4E": //:thumbsdown:
case "":
//valid
break;
case ":heart_eyes:":
case ":love:":
reaction = "\uD83D\uDE0D";
break;
case ":laughing:":
case ":haha:":
reaction = "\uD83D\uDE06";
break;
case ":open_mouth:":
case ":wow:":
reaction = "\uD83D\uDE2E";
break;
case ":cry:":
case ":sad:":
reaction = "\uD83D\uDE22";
break;
case ":angry:":
reaction = "\uD83D\uDE20";
break;
case ":thumbsup:":
case ":like:":
reaction = "\uD83D\uDC4D";
break;
case ":thumbsdown:":
case ":dislike:":
reaction = "\uD83D\uDC4E";
break;
default:
return callback({error: "Reaction is not a valid emoji."});
break;
}

var variables = {
data: {
client_mutation_id: ctx.clientMutationId++,
actor_id: ctx.userID,
action: reaction == "" ? "REMOVE_REACTION" : "ADD_REACTION",
message_id: messageID,
reaction: reaction
}
};

var qs = {
doc_id: "1491398900900362",
variables: JSON.stringify(variables),
dpr: 1
};

defaultFuncs
.postFormData("https://www.messenger.com/webgraphql/mutation/", ctx.jar, {}, qs)
.then(utils.parseAndCheckLogin(ctx.jar, defaultFuncs))
.then(function(resData) {
if (!resData) {
throw {error: "addReaction returned empty object."};
}
if(resData.error) {
throw resData;
}
callback(null);
})
.catch(function(err) {
log.error("addReaction", err);
return callback(err);
});
};
};
11 changes: 10 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ function formatMessage(m) {
timestampAbsolute: originalMessage.timestamp_absolute,
timestampRelative: originalMessage.timestamp_relative,
timestampDatetime: originalMessage.timestamp_datetime,
tags: originalMessage.tags
tags: originalMessage.tags,
reactions: originalMessage.reactions ? originalMessage.reactions : []
};

if(m.type === "pages_messaging") obj.pageID = m.realtime_viewer_fbid.toString();
Expand Down Expand Up @@ -746,6 +747,13 @@ function formatPresence(presence, userID) {
};
}

function decodeClientPayload(payload) {
/*
Special function which Client using to "encode" clients JSON payload
*/
return JSON.parse(String.fromCharCode.apply(null, payload));
}

function getAppState(jar){
return jar
.getCookies("https://www.facebook.com")
Expand Down Expand Up @@ -785,5 +793,6 @@ module.exports = {
generatePresence: generatePresence,
generateAccessiblityCookie: generateAccessiblityCookie,
formatDate: formatDate,
decodeClientPayload: decodeClientPayload,
getAppState: getAppState,
};

0 comments on commit 5676a33

Please sign in to comment.