Skip to content

Instantly share code, notes, and snippets.

@crimeminister
Created November 22, 2019 17:44
Show Gist options
  • Save crimeminister/7f4d38c9dd83ca516eedf013ac25ee85 to your computer and use it in GitHub Desktop.
Save crimeminister/7f4d38c9dd83ca516eedf013ac25ee85 to your computer and use it in GitHub Desktop.
const slackConfig = {
// YOUR CONFIG GOES HERE
};
const adapter = new SlackAdapter(slackConfig);
// 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());
// HACK write our own middleware to handle new format of messages received from Slack.
adapter.use(new SlackHackMiddleware());
const controller = new Botkit({
webhook_uri: '/api/messages',
adapter: adapter,
storage,
});
"use strict";
// ADAPTED FROM: botbuilder-adapter-slack/lib/messagetype_middleware.js
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const botbuilder_1 = require("botbuilder");
/**
* This hackish middleware enables BotKit developers using the BotBuilder SlackAdapter class to
* use Slack App-created bots. These new bots send different event types than legacy bots. Without
* an update like this any controller.hears() in your bots won't be triggered as the 'text' attribute
* isn't set and the botkitEventType isn't something recognizable, e.g. 'direct_mention' instead of
* the 'app_mention' value that is now sent.
*
* NB: this isn't a complete implementation, it's just enough to get you started with a new-style
* Slack bot. Ultimately I expect this functionality to land in the official Slack adapter / middleware.
* Use that once it's available.
*/
class SlackHackMiddleware extends botbuilder_1.MiddlewareSet {
/**
* Not for direct use - implements the MiddlewareSet's required onTurn function used to process the event
* @param context
* @param next
*/
onTurn(context, next) {
return __awaiter(this, void 0, void 0, function* () {
if (context.activity.type === 'event' && context.activity.channelData) {
let adapter = context.adapter;
const bot_user_id = yield adapter.getBotUserByTeam(context.activity);
var mentionSyntax = '<@' + bot_user_id + '(\\|.*?)?>';
var mention = new RegExp(mentionSyntax, 'i');
var direct_mention = new RegExp('^' + mentionSyntax, 'i');
if (context.activity.channelData.type === 'app_mention') {
context.activity.text = context.activity.channelData.text
.replace(direct_mention, '')
.replace(/^\s+/, '')
.replace(/^:\s+/, '')
.replace(/^\s+/, '')
;
context.activity.channelData.botkitEventType = 'direct_mention';
}
// Ignore bot_messages; switch the botkit event type to 'bot_message'
// and the activity type to 'event' (stops it from being included in dialogs).
if (context.activity.channelData && context.activity.channelData.bot_id) {
context.activity.channelData.botkitEventType = 'bot_message';
context.activity.type = botbuilder_1.ActivityTypes.Event;
}
}
yield next();
});
}
}
exports.SlackHackMiddleware = SlackHackMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment