-
Notifications
You must be signed in to change notification settings - Fork 3
/
slack.js
130 lines (92 loc) · 2.95 KB
/
slack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack bot built with Botkit.
This bot demonstrates many of the core features of Botkit:
* Connect to Slack using the real time API
* Receive messages based on "spoken" patterns
* Send a message with attachments
* Send a message via direct message (instead of in a public channel)
# RUN THE BOT:
Get a Bot token from Slack:
-> http://my.slack.com/services/new/bot
Run your bot from the command line:
token=<MY TOKEN> node demo_bot.js
# USE THE BOT:
Find your bot inside Slack to send it a direct message.
Say: "Hello"
The bot will reply "Hello!"
Say: "Attach"
The bot will send a message with a multi-field attachment.
Send: "dm me"
The bot will reply with a direct message.
Make sure to invite your bot into other channels using /invite @<my bot>!
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var Botkit = require('botkit');
if (!process.env.SLACK_BOT_TOKEN) {
console.log('Error: Specify token in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
debug: false
});
controller.spawn({
token: process.env.SLACK_BOT_TOKEN
}).startRTM(function(err) {
if (err) {
throw new Error(err);
}
});
require('../index')({
botmetricsBotId: process.env.BOTMETRICS_BOT_ID,
botmetricsApiKey: process.env.BOTMETRICS_API_KEY,
controller: controller
});
controller.hears(['hello','hi'],['direct_message','direct_mention','mention'],function(bot,message) {
bot.reply(message,"Hello.");
});
controller.hears(['attach'],['direct_message','direct_mention'],function(bot,message) {
var attachments = [];
var attachment = {
title: 'This is an attachment',
color: '#FFCC99',
fields: [],
};
attachment.fields.push({
label: 'Field',
value: 'A longish value',
short: false,
});
attachment.fields.push({
label: 'Field',
value: 'Value',
short: true,
});
attachment.fields.push({
label: 'Field',
value: 'Value',
short: true,
});
attachments.push(attachment);
bot.reply(message,{
text: 'See below...',
attachments: attachments,
},function(err,resp) {
console.log(err,resp);
});
});
controller.hears(['dm me'],['direct_message','direct_mention'],function(bot,message) {
bot.startConversation(message,function(err,convo) {
convo.say('Heard ya');
});
bot.startPrivateConversation(message,function(err,dm) {
dm.say('Private reply!');
});
});