-
Notifications
You must be signed in to change notification settings - Fork 408
/
bot.js
181 lines (138 loc) · 6.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2018 Cisco and/or its affiliates.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// __ __ ___ ___
// |__) / \ | |__/ | |
// |__) \__/ | | \ | |
// This is the main file for the template bot.
// Load process.env values from .env file
require('dotenv').config();
if (!process.env.WEBEX_ACCESS_TOKEN) {
console.log( '\n-->Token missing: please provide a valid Webex user or bot access token in .env or via WEBEX_ACCESS_TOKEN environment variable');
process.exit(1);
}
// Read public URL from env,
// if not specified, try to infer it from public cloud platforms environments
var public_url = process.env.PUBLIC_URL;
if (!public_url) {
// Heroku hosting: available if dyno metadata are enabled, https://devcenter.heroku.com/articles/dyno-metadata
if (process.env.HEROKU_APP_NAME) {
public_url = 'https://' + process.env.HEROKU_APP_NAME + '.herokuapp.com';
}
// Glitch hosting
if (process.env.PROJECT_DOMAIN) {
public_url = 'https://' + process.env.PROJECT_DOMAIN + '.glitch.me';
}
}
var storage;
if (process.env.REDIS_URL) {
const redis = require('redis');
const { RedisDbStorage } = require('botbuilder-storage-redis');
// Initialize redis client
const redisClient = redis.createClient(process.env.REDIS_URL, { prefix: 'bot-storage:' });
storage = new RedisDbStorage(redisClient, 3600); // Keep redis data for 3600 seconds
}
if (process.env.MONGO_URI) {
const { MongoDbStorage } = require('botbuilder-storage-mongodb');
storage = new MongoDbStorage({ url: process.env.MONGO_URI })
}
// Create Webex Adapter
// const uuidv4 = require('uuid/v4');
const { v4: uuidv4 } = require('uuid');
const { WebexAdapter } = require('botbuilder-adapter-webex');
// If PUBLIC_URL not configured, supply a dummy public_address
// If using websockets, don't supply a secret or Botkit will try/fail to
// validate non-existent secret field for incoming events
const adapter = new WebexAdapter({
access_token: process.env.WEBEX_ACCESS_TOKEN,
public_address: public_url ? public_url : 'http://127.0.0.1',
secret: ( process.env.WEBSOCKET_EVENTS == 'True' ) ? null : uuidv4()
});
const { Botkit } = require('botkit');
const controller = new Botkit({
webhook_uri: '/api/messages',
adapter: adapter,
storage
});
// Create Botkit controller
if (process.env.CMS_URI) {
const { BotkitCMSHelper } = require('botkit-plugin-cms');
controller.usePlugin(new BotkitCMSHelper({
uri: process.env.CMS_URI,
token: process.env.CMS_TOKEN
}));
};
// Once the bot has booted up its internal services, you can use them to do stuff.
const path = require('path');
// Express response stub to supply to processWebsocketActivity
// Luckily, the Webex adapter doesn't do anything meaningful with it
class responseStub {
status(){}
end(){}
}
function processWebsocketActivity( event ) {
// Express request stub to fool the Activity processor
let requestStub = {};
// Event details are expected in a 'body' property
requestStub.body = event;
// Hand the event off to the Botkit activity processory
controller.adapter.processActivity( requestStub, new responseStub, controller.handleTurn.bind( controller ) )
}
controller.ready( async () => {
// load developer-created custom feature modules
controller.loadModules(path.join(__dirname, 'features'));
if ( ( !public_url ) && ( process.env.WEBSOCKET_EVENTS !== 'True' ) ) {
console.log( '\n-->No inbound event channel available. Please configure at least one of PUBLIC_URL and/or WEBSOCKET_EVENTS' );
process.exit( 1 );
}
if ( public_url ) {
// Make the app public_url available to feature modules, for use in adaptive card content links
controller.public_url = public_url;
}
if ( process.env.WEBSOCKET_EVENTS == 'True' ) {
await controller.adapter._api.memberships.listen();
controller.adapter._api.memberships.on( 'created', ( event ) => processWebsocketActivity( event ) );
controller.adapter._api.memberships.on( 'updated', ( event ) => processWebsocketActivity( event ) );
controller.adapter._api.memberships.on( 'deleted', ( event ) => processWebsocketActivity( event ) );
await controller.adapter._api.messages.listen();
controller.adapter._api.messages.on('created', ( event ) => processWebsocketActivity( event ) );
controller.adapter._api.messages.on('deleted', ( event ) => processWebsocketActivity( event ) );
await controller.adapter._api.attachmentActions.listen();
controller.adapter._api.attachmentActions.on('created', ( event ) => processWebsocketActivity( event ) );
// Remove unnecessary auto-created webhook subscription
await controller.adapter.resetWebhookSubscriptions();
console.log( 'Using websockets for incoming messages/events');
}
else {
// Register attachmentActions webhook
controller.adapter.registerAdaptiveCardWebhookSubscription( controller.getConfig( 'webhook_uri' ) );
}
});
if (public_url) {
controller.publicFolder('/www', __dirname + '/www');
controller.webserver.get('/', (req, res) => {
res.send(JSON.stringify(controller.botCommons, null, 4));
});
console.log('Health check available at: ' + public_url);
}
controller.commandHelp = [];
controller.checkAddMention = function (roomType, command) {
var botName = adapter.identity.displayName;
if (roomType === 'group') {
return `\`@${botName} ${command}\``
}
return `\`${command}\``
}