-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
122 lines (88 loc) · 3.44 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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
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:
Create a new app via the Slack Developer site:
-> http://api.slack.com
Run your bot from the command line:
clientId=<MY SLACK TOKEN> clientSecret=<my client secret> PORT=<3000> node bot.js
# USE THE BOT:
Navigate to the built-in login page:
https://<myhost.com>/login
This will authenticate you with Slack.
If successful, your bot will come online and greet you.
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const Botkit = require('botkit');
const bot_options = {
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
clientSigningSecret: process.env.clientSigningSecret,
scopes: [
'bot',
'chat:write:bot',
'channels:history',
'channels:read',
'channels:write',
'commands',
'reactions:write',
'users:read',
],
};
// Use a mongo database if specified, otherwise store in a JSON file local to the app.
// Mongo is automatically configured when deploying to Heroku
if (process.env.MONGODB_URI) {
const mongoStorage = require('botkit-storage-mongo')({ mongoUri: process.env.MONGODB_URI, tables: [ 'links', 'feeds' ] });
bot_options.storage = mongoStorage;
} else {
bot_options.json_file_store = __dirname + '/.data/db/'; // store user data in a simple JSON format
}
// Create the Botkit controller, which controls all instances of the bot.
const controller = Botkit.slackbot(bot_options);
controller.startTicking();
// Set up an Express-powered webserver to expose oauth and webhook endpoints
const webserver = require(__dirname + '/components/express_webserver.js')(controller);
webserver.get('/', function(req, res){
res.render('index', {
domain: req.get('host'),
protocol: req.protocol,
layout: 'layouts/default'
});
});
webserver.get('/help', function(req, res){
res.render('help', {
domain: req.get('host'),
protocol: req.protocol,
layout: 'layouts/default'
});
});
webserver.get('/privacy', function(req, res){
res.render('privacy', {
domain: req.get('host'),
protocol: req.protocol,
layout: 'layouts/default'
});
});
// Set up a simple storage backend for keeping a record of customers
// who sign up for the app via the oauth
require(__dirname + '/components/user_registration.js')(controller);
// Send an onboarding message when a new team joins
require(__dirname + '/components/onboarding.js')(controller);
const normalizedPath = require("path").join(__dirname, "skills");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./skills/" + file)(controller);
});