Skip to content

Instantly share code, notes, and snippets.

@danielravina
Last active November 29, 2022 16:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielravina/364d4d59d78d5b960426d702fa47df87 to your computer and use it in GitHub Desktop.
Save danielravina/364d4d59d78d5b960426d702fa47df87 to your computer and use it in GitHub Desktop.
A friendly Slack Bot that responds and reacts to everyone with emojis 🗣
const rp = require('request-promise')
const Botkit = require('botkit');
const emojiDict = require("emoji-dictionary");
const shuffle = require('shuffle-array')
const getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const controller = Botkit.slackbot();
controller.spawn({
token: process.env.SLACK_BOT_TOKEN,
}).startRTM()
// Direct messaging and mentions
controller.hears('.*',['direct_message','direct_mention','mention'], (bot,message) => {
let text = message.text
text = text.replace(/[^\w\s]/gi, '')
rp(`https://emoji.getdango.com/api/emoji?q=${text}`).then(response => {
const emojiList = JSON.parse(response).results
const picks = shuffle([emojiList[0], emojiList[1]])
const replyMessage = picks.map(emoji => emoji.text).join('');
bot.replyWithTyping(message, replyMessage);
})
});
// Reactions in channels
controller.on('ambient', (bot, message) => {
let text = message.text
text = text.replace(/[^\w\s]/gi, '')
rp(`https://emoji.getdango.com/api/emoji?q=${text}`).then(response => {
const results = JSON.parse(response).results
if (results[0].score > 0.03) {
const = emojiName1 = emojiDict.getName(results[0].text)
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: emojiName1,
});
}
if (results[0].score - results[1].score < 0.03 && results[1].score > 0.01) {
const emojiName2 = emojiDict.getName(results[1].text)
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: emojiName2,
});
}
})
});
@danielravina
Copy link
Author

danielravina commented Nov 10, 2017

Plug in your SLACK_BOT_TOKEN, run node emoji-bot.js.
After inviting the bot to a channel, with the power of AI, it will respond to @mentions and add reactions to messages.

You can also talk to it privately if that's your thing!

@juanguren
Copy link

Hey @danielravina ! This is great! A long time has passed since this but do you know if it's possible for the bot to comment on a message based on a specific reaction emoji?

@juanguren
Copy link

Nevermind, I think this reaction.get method API is perfect :D

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