-
Notifications
You must be signed in to change notification settings - Fork 16
/
bot.js
89 lines (78 loc) · 3.79 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
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
var env = require('node-env-file')
env(__dirname + '/.env')
if (!process.env.ROCKETCHAT_URL || !process.env.ROCKETCHAT_USER || !process.env.ROCKETCHAT_PASS) {
usageTip()
}
var Botkit = require('botkit-rocketchat-connector')
var debug = require('debug')('botkit:main')
// the environment variables from RocketChat is passed in bot_options
// because the module it's external, so haven't access to .env file
var botOptions = {
debug: true,
studio_token: process.env.studio_token,
studio_command_uri: process.env.studio_command_uri,
studio_stats_uri: process.env.studio_command_uri,
rocketchat_host: process.env.ROCKETCHAT_URL,
rocketchat_bot_user: process.env.ROCKETCHAT_USER,
rocketchat_bot_pass: process.env.ROCKETCHAT_PASSWORD,
rocketchat_ssl: process.env.ROCKETCHAT_USE_SSL,
rocketchat_bot_rooms: process.env.ROCKETCHAT_ROOM,
rocketchat_bot_mention_rooms: process.env.MENTION_ROOMS,
rocketchat_bot_direct_messages: process.env.RESPOND_TO_DM,
rocketchat_bot_live_chat: process.env.RESPOND_TO_LIVECHAT,
rocketchat_bot_edited: process.env.RESPOND_TO_EDITED
}
// create the Botkit controller with the configurations of the RocketChatBot
var controller = Botkit({}, botOptions)
// imports local conversations to use bot without the botkit api
require(__dirname + '/components/local_conversations.js')(controller)
controller.startBot()
controller.startTicking()
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
if (process.env.studio_token) {
// TODO: configure the EVENTS here
controller.on(['direct_message', 'live_chat', 'channel', 'mention', 'message'], function (bot, message) {
controller.studio.runTrigger(bot, message.text, message.user, message.channel, message).then(function (convo) {
if (!convo) {
// no trigger was matched
// If you want your botbot to respond to every message,
// define a 'fallback' script in Botkit Studio
// and uncomment the line below.
// controller.studio.run(bot, 'fallback', message.user, message.channel);
} else {
// set variables here that are needed for EVERY script
// use controller.studio.before('script') to set variables specific to a script
convo.setVar('current_time', new Date())
}
}).catch(function (err) {
bot.reply(message, 'I experienced an error with a request to Botkit Studio: ' + err)
debug('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 usageTip () {
console.log('~~~~~~~~~~')
console.log('Botkit Studio Starter Kit')
console.log('You problably forgot to update your environment variables')
console.log('Get a Botkit Studio token here: https://studio.botkit.ai/')
console.log('~~~~~~~~~~')
}