-
Notifications
You must be signed in to change notification settings - Fork 598
Implementing addReaction function #427
Changes from 22 commits
c6957bf
a34c12b
325f16f
6161ed8
99655f7
23c26e6
1ec7ad1
915993f
4a8842a
2211491
bdb9284
9c313ab
8d459e5
7dd583d
d476290
6248686
b74b5e3
7f458e1
d8365fc
158e2ed
a2b54ee
c933951
c55efec
4b2dc82
a2eb5a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
* [`api.sendMessage`](#sendMessage) | ||
* [`api.sendTypingIndicator`](#sendTypingIndicator) | ||
* [`api.setOptions`](#setOptions) | ||
* [`api.setMessageReaction`](#setMessageReaction) | ||
* [`api.setTitle`](#setTitle) | ||
|
||
--------------------------------------- | ||
|
@@ -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"`. | ||
|
@@ -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:` | ||
* 😆 - Unicode: `\uD83D\uDE06`, Shortcut: `:laughing:` | ||
* 😮 - Unicode: `\uD83D\uDE2E`, Shortcut: `:open_mouth:` | ||
* 😢 - Unicode: `\uD83D\uDE22`, Shortcut: `:cry:` | ||
* 😠 - Unicode: `\uD83D\uDE20`, Shortcut: `:angry:` | ||
* 👍 - Unicode: `\uD83D\uDC4D`, Shortcut: `:thumbsup:` | ||
* 👎 - Unicode: `\uD83D\uDC4E`, Shortcut: `:thumbsdown:` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but facebook's :love: is heart, not eyes with hearts... :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okey, I added support for Facebook shortcuts 😊 |
||
|
||
|
||
--------------------------------------- | ||
|
||
<a name="setTitle"></a> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
globalCallback(null, { | ||
type: "message_reaction", | ||
threadId: 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': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"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:": | ||
reaction = "\uD83D\uDE0D"; | ||
break; | ||
case ":laughing:": | ||
reaction = "\uD83D\uDE06"; | ||
break; | ||
case ":open_mouth:": | ||
reaction = "\uD83D\uDE2E"; | ||
break; | ||
case ":cry:": | ||
reaction = "\uD83D\uDE22"; | ||
break; | ||
case ":angry:": | ||
reaction = "\uD83D\uDE20"; | ||
break; | ||
case ":thumbsup:": | ||
reaction = "\uD83D\uDC4D"; | ||
break; | ||
case ":thumbsdown:": | ||
reaction = "\uD83D\uDC4E"; | ||
break; | ||
default: | ||
return callback({error: "Reaction is not a valid emoji."}); | ||
break; | ||
} | ||
|
||
var variables = { | ||
data: { | ||
client_mutation_id: ctx.clientMutationId++, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok, sorry I must have missed that. |
||
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); | ||
}); | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 : [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add this to the docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I searched, the message format is not documented in DOCS There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added 😊 |
||
}; | ||
|
||
if(m.type === "pages_messaging") obj.pageID = m.realtime_viewer_fbid.toString(); | ||
|
@@ -718,6 +719,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") | ||
|
@@ -756,5 +764,6 @@ module.exports = { | |
generatePresence: generatePresence, | ||
generateAccessiblityCookie: generateAccessiblityCookie, | ||
formatDate: formatDate, | ||
decodeClientPayload: decodeClientPayload, | ||
getAppState: getAppState, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check if any other emojis are supported? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but @mangotec tried it and don't found any other
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool 😄 (too bad, would have been fun)