Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

[WIP][CCP-194] Update Botkit #49

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 74 additions & 38 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,97 @@
const dotenv = require('dotenv');
const botkit = require('botkit');
const botkitStoragePostgres = require('botkit-storage-pg');
const server = require('./server');
const userRegistration = require('./components/userRegistration');
const onBoarding = require('./components/onBoarding');
const { Botkit } = require('botkit');
const { SlackAdapter } = require('botbuilder-adapter-slack');
const { MemoryStorage } = require('botbuilder');

const storage = new MemoryStorage();

// const botkitStoragePostgres = require('botkit-storage-pg');
// const server = require('./server');
// const userRegistration = require('./components/userRegistration');
// const onBoarding = require('./components/onBoarding');

dotenv.load(); // Doesn't override already set environment variables

if (
!process.env.SLACK_CLIENT_ID ||
!process.env.SLACK_CLIENT_SECRET ||
!process.env.SLACK_CLIENT_SIGNING_SECRET ||
!process.env.SLACK_TOKEN ||
!process.env.PORT
) {
console.error('Empty or unset Environment Variables');
process.exit(1);
}

const botOptions = {
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
const slackOptions = {
clientSigningSecret: process.env.SLACK_CLIENT_SIGNING_SECRET,
scopes: ['commands', 'bot'],
botToken: process.env.BOT_TOKEN,
clientId: process.env.SLACK_CLIENT_ID, // oauth client id
clientSecret: process.env.SLACK_CLIENT_SECRET, // oauth client secret
scopes: ['bot'], // oauth scopes requested
redirectUri: process.env.REDIRECT_URI, // url to redirect post login defaults to `https://<mydomain>/install/auth`
getTokenForTeam: async (teamId) => {
const team = await storage.read([teamId]);
if (team && team.bot_access_token) {
return team.bot_access_token;
} else {
console.error('Team not found in tokenCache: ', teamId);
}
},
getBotUserByTeam: async (teamId) => {
const team = await storage.read([teamId]);
if (team && team.bot_access_token) {
return team.bot_access_token;
} else {
console.error('Team not found in tokenCache: ', teamId);
}
},
};

if (
process.env.DB_HOST &&
process.env.DB_PORT &&
process.env.DB_NAME &&
process.env.DB_USER &&
process.env.DB_PASSWORD
) {
// Set up custom Postgres storage system to store workspaces, channels and users data.
botOptions.storage = botkitStoragePostgres({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
});
} else {
// Store workspaces, channels and users data in a simple JSON format.
botOptions.json_file_store = '.db_bot/';
}
// Create Slack adapter for multi-team mode
const adapter = new SlackAdapter(slackOptions);

// Create the Botkit controller, which controls all instances of the bot.
const slackController = botkit.slackbot(botOptions);
const botController = new Botkit({ adapter, storage });

botController.webserver.get('/install', (req, res) => {
console.log('I AM HERE');
res.redirect(adapter.getInstallLink());
});

botController.webserver.get('/install/auth', async (req, res) => {
try {
const results = await botController.adapter.validateOauthCode(
req.query.code
);
// store these values in a way they'll be retrievable with getBotUserByTeam and getTokenForTeam
console.log('RESULTS:', results);

storage.write({
[results.team_id]: {
bot_access_token: results.bot.bot_access_token,
bot_user_id: results.bot.bot_user_id,
},
});
res.send('Success! Bot installed.');
} catch (err) {
console.error('OAUTH ERROR:', err);
res.status(401);
res.send(err.message);
}
});

botController.hears('hello', 'message', async (bot, message) => {
console.log('MESSAGE', message);
});


slackController.startTicking();

// Set up express server.
server(slackController);

userRegistration(slackController);
onBoarding(slackController);
// userRegistration(botController);
// onBoarding(botController);

// Load in skills.
const { hears } = require('./skills/hears');
hears(slackController);
require('./skills/slashCommands')(slackController);
// // Load in skills.
// const { hears } = require('./skills/hears');
// hears(botController);
// require('./skills/slashCommands')(botController);
Loading