- Initialize a node project with
npm init
- Install dependencies with
npm install --save
- Explain what WebSockets are and why you would use them
- Make requests to the Slack API using
request
module
Bot users have many of the same qualities as their human counterparts: they have profile photos, names, and bios, they exist in the team directory, they can be direct messaged or mentioned, they can post messages and upload files, and they can be invited to and kicked out of channels and private groups.
The biggest difference between bot users and regular users is that instead of interacting with a team via one of Slack's mobile or desktop apps, bot users are controlled programmatically via a bot user token that accesses one or more of Slack's APIs.
- Anything
- Deploy code
- http://www.slackbotlist.com/
- Create a new bot here https://ga-students.slack.com/apps/new/A0F7YS25R-bots
- Click "Add bot integration"
- Copy API Token
- We’ll need this later
WebSockets provide a persistent two-way connection between the client and server. Instead of polling for data, or requesting it explicitly ($.ajax), the server can send events over the websocket connection.
This is ideal for realtime applications like:
- Chat
- Collaborative editing (Google Docs)
- Multiplayer online games
$ mkdir my-slackbot
$ cd my-slackbot
$ npm init
$ npm install --save request
$ touch app.js
// app.js
var request = require("request");
var url = "https://slack.com/api/rtm.start?token=" + process.env.token;
request( url, function( err, response, body ){
var websocketUrl = JSON.parse( body ).url;
console.log(websocketUrl)
});
$ token=your-token-here node app.js
$ npm install --save ws
// app.js
var request = require("request");
var WebSocket = require("ws");
var url = "https://slack.com/api/rtm.start?token=" + process.env.token;
request( url, function( err, response, body ){
var websocketUrl = JSON.parse( body ).url;
var ws = new WebSocket(websocketUrl);
});
// app.js
var request = require("request");
var WebSocket = require("ws");
var url = "https://slack.com/api/rtm.start?token=" + process.env.token;
request( url, function( err, response, body ){
var websocketUrl = JSON.parse( body ).url;
var ws = new WebSocket(websocketUrl);
ws.on("message", function(message){
console.log(message);
})
});
- Send it a DM
- Mention it in a channel it’s in
- Type anything
How do you know when the
message
is a direct message?
// app.js
var request = require("request");
var WebSocket = require("ws");
var url = "https://slack.com/api/rtm.start?token=" + process.env.token;
request( url, function( err, response, body ){
var websocketUrl = JSON.parse( body ).url;
var ws = new WebSocket(websocketUrl);
ws.on("message", function(message){
console.log(message);
if(msg.type == "message" && msg.channel.substr(0,1) == "D"){
reply(msg.channel, msg.text.split(" ").reverse().join(" "))
}
})
});
function reply(channel, message){
var url = "https://slack.com/api/chat.postMessage?token=" + process.env.token + "&text=" + encodeURIComponent(message) + "&channel=" + channel;
request(url, function(err,res,body){
console.log(body)
})
}
GitHub created a bot service called Hubot - https://hubot.github.com/
- Follow the setup instructions here - https://github.com/slackhq/hubot-slack#creating-a-new-bot
- Create a new
.js
file in thescripts
folder - https://github.com/github/hubot/blob/master/docs/scripting.md#hearing-and-responding
Don’t fear the coffeescript! It’s very similar to JavaScript, with a ruby-like syntax.