This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbot.js
123 lines (94 loc) · 4.36 KB
/
bot.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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
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
* Reply to messages
* Use the conversation system to ask questions
* Use the built in storage system to store and retrieve information
for a user.
# RUN THE BOT:
Get a Bot token from Slack:
-> http://my.slack.com/services/new/bot
Get a Botkit Studio token from Botkit.ai:
-> https://studio.botkit.ai/
Run your bot from the command line:
token=<MY SLACK TOKEN> studio_token=<MY BOTKIT STUDIO TOKEN> node bot.js
# USE THE BOT:
Find your bot inside Slack to send it a direct message.
Say: "Hello"
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.token) {
console.log('Error: Specify a Slack bot token in environment.');
usage_tip();
process.exit(1);
}
//
// if (!process.env.studio_token) {
// console.log('Error: Specify a Botkit Studio token in environment.');
// usage_tip();
// process.exit(1);
// }
// Create the Botkit controller, which controls all instances of the bot.
var controller = Botkit.slackbot({
debug: false,
retry: 10,
studio_token: process.env.studio_token
});
// Dashbot is a turnkey analytics platform for bots.
// Sign up for a free key here: https://www.dashbot.io/ to see your bot analytics in real time.
if (process.env.DASHBOT_API_KEY) {
var dashbot = require('dashbot')(process.env.DASHBOT_API_KEY).slack;
controller.middleware.receive.use(dashbot.receive);
controller.middleware.send.use(dashbot.send);
controller.log.info('Thanks for using Dashbot. Visit https://www.dashbot.io/ to see your bot analytics in real time.');
} else {
controller.log.info('No DASHBOT_API_KEY specified. For free turnkey analytics for your bot, go to https://www.dashbot.io/ to get your key.');
}
// Spawn a single instance of the bot to connect to your Slack team
// You can extend this bot later to connect to multiple teams.
// Refer to the Botkit docs on Github
var bot = controller.spawn({
token: process.env.token,
}).startRTM();
var normalizedPath = require("path").join(__dirname, "skills");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./skills/" + file)(controller);
});
// This captures and evaluates any message sent to the bot as a DM
// or sent to the bot in the form "@bot message" and passes it to
// Botkit Studio to evaluate for trigger words and patterns.
// If a trigger is matched, the conversation will automatically fire!
// You can tie into the execution of the script using the functions
// controller.studio.before, controller.studio.after and controller.studio.validate
if (process.env.studio_token) {
controller.on('direct_message,direct_mention,mention', function(bot, message) {
controller.studio.runTrigger(bot, message.text, message.user, message.channel).catch(function(err) {
bot.reply(message, 'I experienced an error with a request to Botkit Studio: ' + err);
});
});
} else {
console.log('~~~~~~~~~~');
console.log('NOTE: Botkit Studio functionality has not been enabled');
console.log('To enable, pass in a studio_token parameter with a token from https://studio.botkit.ai/');
}
function usage_tip() {
console.log('~~~~~~~~~~');
console.log('Botkit Studio Starter Kit');
console.log('Execute your bot application like this:');
console.log('token=<MY SLACK TOKEN> studio_token=<MY BOTKIT STUDIO TOKEN> node bot.js');
console.log('Get a Slack token here: https://my.slack.com/apps/new/A0F7YS25R-bots')
console.log('Get a Botkit Studio token here: https://studio.botkit.ai/')
console.log('~~~~~~~~~~');
}