Created
August 19, 2016 15:31
-
-
Save clooth/8e46d9bbebf4ac671919a43aba66c69a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import sensate | |
import Sensate, { Event, Message } from 'sensate' | |
// Import wanted gateways | |
import SlackGateway from 'sensate-gateway-slack' | |
// Logging | |
const log = () => console.log.apply(console, arguments) | |
// Create a new sensate instance that connects to slack | |
const bot = new Sensate(new SlackGateway({ | |
// The slack token used in the connection | |
token: 'dummy' | |
}), { debug: true }) | |
// When the bot is connected to the gateway | |
bot.on(Event.CONNECTED, () => { | |
log('Connection initiated.') | |
}) | |
// When the bot disconects from the gateway | |
bot.on(Event.DISCONNECTED, () => { | |
log('Connection terminated.') | |
}) | |
// Bind message listener | |
bot.on(Event.MESSAGE_RECEIVED, (message) => { | |
const user = message.user | |
const type = message.type | |
// Handle different message types differently | |
switch (type) { | |
// When a user joins a channel | |
case Message.CHANNEL_JOIN: { | |
const channel = message.channel | |
log(`${user.name} joined ${channel.name}`) | |
// Speak on the channel | |
bot.reply(message, `Welcome, ${user.name}!`) | |
break | |
} | |
// When a user parts a channel | |
case Message.CHANNEL_PART: { | |
const channel = message.channel | |
log(`${user.name} parted ${channel.name}`) | |
// Speak on the channel | |
bot.reply(message, `Why did ${user.name} have to go?`) | |
break | |
} | |
// When a user sends a channel message | |
case Message.CHANNEL_MESSAGE: { | |
const channel = message.channel | |
log(`${user.name} on ${channel.name}: ${message.text}`) | |
// Send a private message to the message sender | |
bot.replyPrivate(message, `What did you mean by "${message.text}"?`) | |
break | |
} | |
// When a user sends a direct message | |
case Message.DIRECT_MESSAGE: { | |
log(`${user.name} in private: ${message.text}`) | |
// With direct messages, `bot.reply` replies in private | |
bot.reply(message, `${message.text}`) | |
break | |
} | |
default: | |
log(`Unhandled message type ${type}`) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment