Skip to content

Instantly share code, notes, and snippets.

@geekgonecrazy
Last active April 14, 2021 21:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekgonecrazy/d6a0f68cbf58a4c57ae11520b15de053 to your computer and use it in GitHub Desktop.
Save geekgonecrazy/d6a0f68cbf58a4c57ae11520b15de053 to your computer and use it in GitHub Desktop.
Import Emoji into Rocket.Chat

Usage

To use this drop these two files in beside a folder called emoji filled with the emoji you want to upload.

Run npm install to grab the needed dependencies.

Modify the host, user, and pass in the file.

Then run: node import-emoji.js

const { driver } = require('@rocket.chat/sdk');
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
// customize the following with your server and user account information
const HOST = 'yourserver';
const USER = 'youruser';
const PASS = 'yourpassword';
const SSL = true; // server uses https ?
const emojiDir = './emoji';
var myuserid;
const run = async () => {
try {
const conn = await driver.connect( { host: HOST, useSsl: SSL})
myuserid = await driver.login({username: USER, password: PASS});
} catch (err) {
console.error('Failed to connect', err);
process.exit(1);
}
console.log(`Connected! UserId is ${myuserid}`);
const files = fs.readdirSync(emojiDir);
for (const file of files) {
const pathToFile = path.join(emojiDir, file);
console.log(`Preparing to upload: ${file}`);
try {
const stat = fs.statSync(pathToFile)
// Finally its a file... lets do something with it
if (stat.isFile()) {
const mimeType = mime.lookup(pathToFile);
var fileSplit = file.split('.');
const emojiData = {
name: fileSplit[0],
aliases: '',
newFile: true,
extension: fileSplit[1]
};
await driver.callMethod('insertOrUpdateEmoji', emojiData);
const fileData = fs.readFileSync(pathToFile);
console.log(`Uploading: ${file} mimeType: ${mimeType}`);
await driver.asyncCall('uploadEmojiCustom', [fileData, mimeType, emojiData]);
console.log(`Finished Uploading: ${file}`);
delete fileData;
}
} catch (err) {
console.error(err);
}
await sleep(1000);
}
}
function sleep(ms) {
console.log(`Sleeping for ${ms}ms`);
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
run()
{
"name": "rc-import-emoji",
"version": "1.0.0",
"description": "",
"main": "import-emoji.js",
"dependencies": {
"@rocket.chat/sdk": "^0.2.9-2",
"mime-types": "^2.1.27"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "github.com/geekgonecrazy",
}
@geekgonecrazy
Copy link
Author

I need to update this and use the rest api instead of realtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment