Download Custom Slack Emojis
This post will demonstrate how to use Slack’s emoji.list API method to extract and export custom emojis from Slack using the web client. This API is called by the web client to fetch all emojis when initializing a workspace, and can be called with a user’s xoxc
token.
tl;dr
Instructions for downloading the emojis can be found in the Gist at:
https://gist.github.com/nopfor/6a6abf04f52464eec71603b747ffc49a
Details
API Call
When Slack first loads a channel, it fetches custom emojis for the workspace using the emoji.list API. These values do not appear to be stored in a JavaScript variable (although at one point they were…), but it is possible to re-call the API.
The documentation shows that a “token” argument is required for authentication. During normal use, the web client sends the xoxc
token, which is saved in localStorage
. The following JavaScript will extract this token:
1JSON.parse(localStorage.localConfig_v2).teams[slackDebug.activeTeamId].token
The token will follow the format xoxc-11111111111-121212121212-121212121212-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
.
The API can be called using the multipart/form-data
Content-Type
. The following JavaScript will make the API request and dump the response to the console:
1// get auth token for API request
2var authToken = JSON.parse(localStorage.localConfig_v2).teams[slackDebug.activeTeamId].token;
3
4// setup request
5var formData = new FormData();
6formData.append('token', authToken);
7
8// make request
9(async () => {
10 const rawResponse = await fetch('/api/emoji.list', {
11 method: 'POST',
12 body: formData
13 });
14
15 const emojisApi = await rawResponse.json();
16
17 // dump to console
18 console.log(emojisApi.emoji);
19})();
Saving the Data
Due to the restricted (lack of) Access-Control-Allow-Origin
header on emoji.slack-edge.com, it is not possible to download the emojis using JavaScript straight from a workspace (located on app.slack.com). Attempting to do so will result in a Cross-Origin Resource Sharing (CORS) error.
Therefore, the download.js script of the Gist saves the results of the API call to a CSV. The CSV will have two columns: one for the image name and the other for the URL. Paste this script into the browser’s console, accept the popup warning, and save the file. Downloading the Emojis
Since custom emojis do not require authentication, it is possible to download them from outside the browser.
On Linux and Mac, the wget
utility can be leveraged to easily download these files. Ensure the terminal is open in the same directory as the emojis.csv
file, and run the following script (from download.sh):
1for emoji_line in $(cat emojis.csv); do
2 name=$(echo "$emoji_line" | cut -d, -f1)
3 url=$(echo "$emoji_line" | cut -d, -f2)
4
5 wget -O "$name" "$url"
6done
On Windows, PowerShell can be used to download all emojis in the CSV. Run the following commands from the same folder as the emojis.csv
file (from download.ps1, requires PowerShell 3):
1$emojis = Import-Csv -Path .\emojis.csv -Header 'Name','Url'
2
3foreach ($emoji in $emojis) {
4 Invoke-WebRequest -Uri $emoji.Url -OutFile $emoji.Name
5}
After running the scripts, all custom emojis from the Slack workspace should be present in the current directory/folder.