-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
190 lines (136 loc) · 5.29 KB
/
bot.js
File metadata and controls
190 lines (136 loc) · 5.29 KB
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
182
183
184
185
186
187
188
189
// __ __ ___ ___
// |__) / \ | |__/ | |
// |__) \__/ | | \ | |
// This is the main file for the guild-bot bot.
// Import Botkit's core features
const Botkit = require('botkit');
const { BotkitCMSHelper } = require('botkit-plugin-cms');
// Import a platform-specific adapter for slack.
const { SlackAdapter, SlackMessageTypeMiddleware, SlackEventMiddleware } = require('botbuilder-adapter-slack');
// const { MongoDbStorage } = require('botbuilder-storage-mongodb');
// Load process.env values from .env file
require('dotenv').config();
let storage = null;
// if (process.env.MONGO_URI) {
// storage = mongoStorage = new MongoDbStorage({
// url : process.env.MONGO_URI,
// });
// }
// const adapter = new SlackAdapter({
// // REMOVE THIS OPTION AFTER YOU HAVE CONFIGURED YOUR APP!
// // enable_incomplete: true,
// // parameters used to secure webhook endpoint
// verificationToken: process.env.VERIFICATION_TOKEN,
// clientSigningSecret: process.env.CLIENT_SIGNING_SECRET,
// // auth token for a single-team app
// botToken: process.env.BOT_TOKEN,
// // credentials used to set up oauth for multi-team apps
// clientId: process.env.CLIENT_ID,
// clientSecret: process.env.CLIENT_SECRET,
// scopes: ['bot'],
// redirectUri: process.env.REDIRECT_URI,
// oauthVersion: 'v2',
// // functions required for retrieving team-specific info
// // for use in multi-team apps
// // getTokenForTeam: getTokenForTeam,
// // getBotUserByTeam: getBotUserByTeam,
// });
// // Use SlackEventMiddleware to emit events that match their original Slack event types.
// adapter.use(new SlackEventMiddleware());
// // Use SlackMessageType middleware to further classify messages as direct_message, direct_mention, or mention
// adapter.use(new SlackMessageTypeMiddleware());
const controller = Botkit.slackbot({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
clientSigningSecret: process.env.VERIFICATION_TOKEN,
debug: true,
scopes: ['bot']
});
exports.controller = controller;
// const controller = Botkit.slackbot({
// webhook_uri: '/api/messages',
// adapter: adapter,
// storage
// });
let bot = controller.spawn({
token: process.env.BOT_TOKEN
});
// controller.hears('guilds list', function (bot, message) {
bot.api.conversations.list({ exclude_archived: true, limit: 999 }, (err, response) => {
const allChannels = response.channels;
const channelsList = allChannels.filter((channel) => /^g-.*$/.test(channel.name)).map((channel) => channel.id);
// to get the channel purpose: channel.purpose.value
// console.log('allChannels', allChannels);
let formattedResults = `\n*Guilds (${channelsList.length}):*\n`;
channelsList.forEach((channel) => {
formattedResults += `<#${channel}>\n`;
});
console.log(formattedResults);
// bot.reply(message, formattedResults);
// })
})
// })
// controller.webserver.get('/', (req, res) => {
// res.send(`This app is running Botkit ${ controller.version }.`);
// });
// controller.webserver.get('/channels', (req, res) => {
// adapter.processActivity(req, res, async (context) => {
// console.log('CONTEXT', context);
// console.log('RES', res);
// res.send(`hi`);
// })
// });
// controller.webserver.get('/talk', async (req, res) => {
// let bot = await controller.spawn('T024FPLDT');
// await bot.startConversationWithUser('esun');
// await bot.say('hi');
// })
// controller.webserver.get('/install', (req, res) => {
// // getInstallLink points to slack's oauth endpoint and includes clientId and scopes
// res.redirect(controller.adapter.getInstallLink());
// });
// controller.webserver.get('/install/auth', async (req, res) => {
// try {
// const results = await controller.adapter.validateOauthCode(req.query.code);
// console.log('FULL OAUTH DETAILS', results);
// // Store token by team in bot state.
// tokenCache[results.team_id] = results.bot.bot_access_token;
// // Capture team to bot id
// userCache[results.team_id] = results.bot.bot_user_id;
// res.json('Success! Bot installed.');
// } catch (err) {
// console.error('OAUTH ERROR:', err);
// res.status(401);
// res.send(err.message);
// }
// });
// let tokenCache = {};
// let userCache = {};
// if (process.env.TOKENS) {
// tokenCache = JSON.parse(process.env.TOKENS);
// }
// if (process.env.USERS) {
// userCache = JSON.parse(process.env.USERS);
// }
// async function getTokenForTeam(teamId) {
// if (tokenCache[teamId]) {
// return new Promise((resolve) => {
// setTimeout(function() {
// resolve(tokenCache[teamId]);
// }, 150);
// });
// } else {
// console.error('Team not found in tokenCache: ', teamId);
// }
// }
// async function getBotUserByTeam(teamId) {
// if (userCache[teamId]) {
// return new Promise((resolve) => {
// setTimeout(function() {
// resolve(userCache[teamId]);
// }, 150);
// });
// } else {
// console.error('Team not found in userCache: ', teamId);
// }
// }