Skip to content

Instantly share code, notes, and snippets.

@avicoder
Created February 11, 2024 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avicoder/81fda94e5652ddc8dc03f12734a7e46e to your computer and use it in GitHub Desktop.
Save avicoder/81fda94e5652ddc8dc03f12734a7e46e to your computer and use it in GitHub Desktop.
Mass Import Slack emojis to Larksuite.

Mass Importing Emojis from Slack to Larksuite

Prerequisite:

  1. Export all the emojies from Slack using this tool - https://emojibox.app/
  2. Convert all the exported emojies to the format accepted in Lark, i.e. 96x96, <100KB, png format, using this tool - https://redketchup.io/bulk-image-resizer
image
  1. Note the path where all the converted emojies are located.

This process involves two steps:

  • Step 1: Uploading all emojis from Slack.
  • Step 2: Providing the path where the emojis are uploaded for registration.

Part One

To initiate the first step, utilize the following script to prepare the payload required for the second part:

import requests, os
from requests_toolbelt import MultipartEncoder

# Initialize an empty list to store JSON payloads for each emoji
json_body = []

# Function to post each emoji file as an attachment
def post_file_attachment(url, file_name, content_type):
    # Create a multipart encoder for the file
    multipart = MultipartEncoder(
        fields={'enterprise': (file_name, open(file_name, 'rb'), content_type)}
    )

    # Set headers for the request
    headers = {
        "Req-Source": "admin-source",
        "Accept": "application/json, text/plain, */*",
        # Update the domain that you've set for your workspace
        "Origin": "",  # Example: "Origin": "https://tiktok.sg.larksuite.com"
        'Content-Type': multipart.content_type,
    }
    
    # Set cookies for the request
    # Replace 'SESSION_VALUE' with the actual session value obtained from developer tools
    cookies = {
        "session": "SESSION_VALUE",
    }

    # Make a POST request to upload the emoji file
    response = requests.post(url, data=multipart, headers=headers, cookies=cookies)
    
    # Extract key and name from the response JSON
    key = response.json()['data'][0]['key']
    name = response.json()['data'][0]['name'].split(".")[0]

    # Construct JSON payload for the emoji and append to json_body list
    json_body.append(
        {"emoji_summary":
            {"zh-CN": "",
             "en-US": name
             },
         "emoji_image_key":
             {"zh-CN": "",
              "en-US": key},
         }
    )

# URL for uploading emojis (replace DOMAIN_VALUE with your domain)
url = "https://{DOMAIN_VALUE}.sg.larksuite.com/sticker/enterprise_emoji/upload/emojis"

# Content type of the emoji files
content_type = "image/png"

# Directory path where the emoji files are located
directory_path = 'temp-tx/imgs'

# Iterate over each file in the directory
for filename in os.listdir(directory_path):
    file_path = os.path.join(directory_path, filename)
    if os.path.isfile(file_path):
        # Check if the file is a PNG image
        if filename.lower().endswith('.png'):
            print(f"Processing file: {file_path}")
            # Upload the file and add to the JSON payload
            post_file_attachment(url, file_path, content_type)

# Print the JSON payload for all emojis
print(json_body)

Make sure to replace SESSION_VALUE with the actual session value obtained from the developer tools, and DOMAIN_VALUE with your workspace domain. This script will upload all PNG files from the specified directory and construct a JSON payload with emoji information.

Part Two

To register the emojies, We need create and send a request using a tool of your choice, I will provide curl in the end.

Payload looks like this

SNIP ....
    "emojis": [
        <PASTE_JSON_PAYLOAD_CREATED_IN_FIRST_STEP>
    ]
SNIP .....

Put Request

PUT /sticker/enterprise_emoji/update HTTP/2
Host: <DOMAIN>.sg.larksuite.com
Cookie: session=<SESSION_TOKEN>;
Content-Length: 613
Credentials: same-origin
Content-Type: application/json;charset=UTF-8
Accept: application/json, text/plain, */*
Origin: https://<DOMAIN>.sg.larksuite.com
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Priority: u=1, i


{
    "category_cover_key": {
        "light_cover_key": "******"
    },
    "category_summary": {
        "en-US": "****"
    },
    "emojis": [
    <PASTE_JSON_PAYLOAD_CREATED_IN_FIRST_STEP>
    ],
    "category_permission": {
        "user_names": [
            {
                "id": "******",
                "name": "******",
                "avator":****
            }
        ]
    },
    "release_status": 1,
    "permission": {
        "userIds": [
            "*****"
        ]
    }
}

Curl Request -

curl -i -s -k -X $'PUT' \
    -H $'Host: <DOMAIN>.sg.larksuite.com' -H $'Content-Length: 613' -H $'Credentials: same-origin' -H $'Content-Type: application/json;charset=UTF-8' -H $'Accept: application/json, text/plain, */*' -H $'Origin: https://<DOMAIN_NAME>.sg.larksuite.com' -H $'Accept-Encoding: gzip, deflate, br' -H $'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' -H $'Priority: u=1, i' \
    -b $'session=<SESSION_TOKEN>' \
    --data-binary $'{\"category_cover_key\":{\"light_cover_key\":\"******\"},\"category_summary\":{\"en-US\":\"****\"},\"emojis\":[\x0d\x0a\x09        <PASTE_JSON_PAYLOAD_CREATED_IN_FIRST_STEP>\x0d\x0a],\"category_permission\":{\"user_names\":[{\"id\":\"******\",\"name\":\"******\",\"avator\":****}]},\"release_status\":1,\"permission\":{\"userIds\":[\"*****\"]}}' \
    $'https://<DOMAIN_NAME>.sg.larksuite.com/sticker/enterprise_emoji/update'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment