Skip to content

Instantly share code, notes, and snippets.

@hang15
Last active December 18, 2020 15:59
Show Gist options
  • Save hang15/9a73ff348bcf4e0e5bf4f942630cd028 to your computer and use it in GitHub Desktop.
Save hang15/9a73ff348bcf4e0e5bf4f942630cd028 to your computer and use it in GitHub Desktop.
// Load Environment variables from dotenv
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
// Import Botkit's core features
const { Botkit } = require("botkit");
// Import a platform-specific adapter for slack.
const { SlackAdapter, SlackEventMiddleware } = require('botbuilder-adapter-slack');
// Create an express server with json and urlencoded body parsing
const webserver = express();
webserver.use((req, res, next) => {
req.rawBody = '';
req.on('data', function (chunk) {
req.rawBody += chunk;
});
next();
});
webserver.use(bodyParser.json());
webserver.use(bodyParser.urlencoded({ extended: true }));
// Adaptor for Slack
const slackAdaptor = new SlackAdapter({
// REMOVE THIS OPTION AFTER YOU HAVE CONFIGURED YOUR APP!
enable_incomplete: true,
// The following options are sufficient for a single team application
// parameters used to secure webhook endpoint
verificationToken: process.env.SLACK_VERIFICATION_TOKEN,
clientSigningSecret: process.env.SLACK_CLIENT_SIGNING_SECRET,
// auth token for a single-team app
botToken: process.env.SLACK_BOT_TOKEN
});
// Use SlackEventMiddleware to emit events that match their original Slack event types.
slackAdaptor.use(new SlackEventMiddleware());
// Controller for Slack
const slackController = new Botkit({
webhook_uri: process.env.SLACK_WEBHOOK_URI,
adapter: slackAdaptor,
webserver: webserver
});
// Controller for Teams
const teamsController = new Botkit({
webhook_uri: process.env.TEAMS_WEBHOOK_URI,
adapterConfig: {
appId: process.env.TEAMS_APP_ID,
appPassword: process.env.TEAMS_APP_PASSWORD,
},
webserver: webserver
});
// load developer-created local custom feature modules.
// Load common features for slack and teams
slackController.loadModules(__dirname + '/features/common');
teamsController.loadModules(__dirname + '/features/common');
// Load platform specific features seperately
slackController.loadModules(__dirname + '/features/slack');
teamsController.loadModules(__dirname + '/features/teams');
// For running server locally for development purposes
if (require.main === module) {
const http = require("http");
const port = process.env.PORT || 3000;
const httpserver = http.createServer(webserver);
httpserver.listen(port, function () {
console.log("Slack Webhook endpoint online: http://127.0.0.1:" + port + process.env.SLACK_WEBHOOK_URI);
console.log("Teams Webhook endpoint online: http://127.0.0.1:" + port + process.env.TEAMS_WEBHOOK_URI);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment