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

Utility function for getting the image URL for a Messenger-style emoji #477

Merged
merged 4 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [`api.forwardAttachment`](#forwardAttachment)
* [`api.getAppState`](#getAppState)
* [`api.getCurrentUserID`](#getCurrentUserID)
* [`api.getEmojiUrl`](#getEmojiUrl)
* [`api.getFriendsList`](#getFriendsList)
* [`api.getThreadHistory`](#getThreadHistory)
* [`api.getThreadInfo`](#getThreadInfo)
Expand Down Expand Up @@ -440,6 +441,41 @@ Returns the currently logged-in user's Facebook user ID.

---------------------------------------

<a name="getEmojiUrl"></a>
### api.getEmojiUrl(c, size[, pixelRatio])

Returns the URL to a Facebook Messenger-style emoji image asset.

__note__: This function will return a URL regardless of whether the image at the URL actually exists.
This can happen if, for example, Messenger does not have an image asset for the requested emoji.

__Arguments__

* `c` - The emoji character
* `size` - The width and height of the emoji image; supported sizes are 32, 64, and 128
* `pixelRatio` - The pixel ratio of the emoji image; supported ratios are '1.0' and '1.5' (default is '1.0')

__Example__

```js
const fs = require("fs");
const login = require("facebook-chat-api");

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

// Prints https://static.xx.fbcdn.net/images/emoji.php/v8/z9c/1.0/128/1f40d.png
console.log('Snake emoji, 128px (128x128 with pixel ratio of 1.0');
console.log(api.getEmojiUrl('\ud83d\udc0d', 128));

// Prints https://static.xx.fbcdn.net/images/emoji.php/v8/ze1/1.5/128/1f40d.png
console.log('Snake emoji, 192px (128x128 with pixel ratio of 1.5');
console.log(api.getEmojiUrl('\ud83d\udc0d', 128, '1.5'));
});
```

---------------------------------------

<a name="getFriendsList"></a>
### api.getFriendsList(callback)

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function buildAPI(globalOptions, html, jar) {
'deleteThread',
'forwardAttachment',
'getCurrentUserID',
'getEmojiUrl',
'getFriendsList',
'getThreadHistory',
'getThreadInfo',
Expand Down
24 changes: 24 additions & 0 deletions src/getEmojiUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

const util = require("util");

module.exports = function() {
return function getEmojiUrl(c, size, pixelRatio) {
/*
Resolves Facebook Messenger emoji image asset URL for an emoji character.
Supported sizes are 32, 64, and 128.
Supported pixel ratios are '1.0' and '1.5' (possibly more; haven't tested)
*/
const baseUrl = 'https://static.xx.fbcdn.net/images/emoji.php/v8/z%s/%s';
pixelRatio = pixelRatio || '1.0';

let ending = util.format('%s/%s/%s.png', pixelRatio, size, c.codePointAt(0).toString(16));
Copy link
Owner

@Schmavery Schmavery Jun 6, 2017

Choose a reason for hiding this comment

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

Does util not need to be 'require'd for this to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason it worked when I tested it, but I've added the proper require

let base = 317426846;
for (let i = 0; i < ending.length; i++) {
base = (base << 5) - base + ending.charCodeAt(i);
}

let hashed = (base & 255).toString(16);
return util.format(baseUrl, hashed, ending);
};
};