Skip to content

Instantly share code, notes, and snippets.

@seripap
Last active April 2, 2021 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seripap/42248e793001e893f6633f2a0a1f66cc to your computer and use it in GitHub Desktop.
Save seripap/42248e793001e893f6633f2a0a1f66cc to your computer and use it in GitHub Desktop.
Linear.app Slack Bot Autolinker

This is a Slack bot that automatically links to Linear.app issues. Sort of how like the Github autolinks work, except for Slack.

For example, you type Check out SE-12, the bot will then link to https://linear.app/{team}/se-12

const { App, ExpressReceiver } = require('@slack/bolt');
const serverlessExpress = require('@vendia/serverless-express');
// Define team prefixes here
const TEAM_PREFIXES = ['SE'];
// Define linear team
const LINEAR_TEAM = `fsf`;
// ----- That's pretty much it...
const LINEAR_LINK = `https://linear.app/${LINEAR_TEAM}/issue`
const expressReceiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
processBeforeResponse: true
});
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver: expressReceiver
});
const regexp = buildRegex();
app.message(regexp, sendLink);
async function sendLink({ message, context, say }) {
const issue = context.matches[0];
// @todo- validate that the issue is valid based on team provided.
// for example, if team is 'se' and an issue that was found in context was se- (no number),
// it should error. should always check against length of the actual team prefix as well.
const text = `${LINEAR_LINK}/${issue}`
let thread_ts = message.ts
if (message.thread_ts) {
thread_ts = message.thread_ts
}
await say({text, thread_ts});
}
function buildRegex() {
// match prefixes with upper and lowercase values
const matches = TEAM_PREFIXES.map(s => s.toUpperCase()).join('|') + "|" + TEAM_PREFIXES.map(s => s.toLowerCase()).join('|')
// We only care about the first match, no need for a global identifier
const r = `(${matches})-(.*?)[0-9]*`
return new RegExp(r)
}
module.exports.handler = serverlessExpress({
app: expressReceiver.app
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment