Last active
January 24, 2021 08:20
-
-
Save farski/059659cd6883b313d3b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample event data for a proxy request | |
// { | |
// "resource": "Resource path", | |
// "path": "Path parameter", | |
// "httpMethod": "Incoming request's method name" | |
// "headers": {Incoming request headers} | |
// "queryStringParameters": {query string parameters } | |
// "pathParameters": {path parameters} | |
// "stageVariables": {Applicable stage variables} | |
// "requestContext": {Request context, including authorizer-returned key-value pairs} | |
// "body": "A JSON string of the request payload." | |
// "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode" | |
// } | |
const url = require('url'); | |
const https = require('https'); | |
const querystring = require('querystring'); | |
const SLACK_INCOMING_WEBHOOK_URL = process.env.SLACK_INCOMING_WEBHOOK_URL; | |
const SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN; | |
async function postToWebhook(webhookUrl, payload) { | |
return new Promise((resolve, reject) => { | |
const body = JSON.stringify(payload); | |
// Setup request options | |
const options = url.parse(webhookUrl); | |
options.method = 'POST'; | |
options.headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(body), | |
}; | |
// Request with response handler | |
console.log('Making chat.postMessage API request'); | |
const req = https.request(options, (res) => { | |
res.setEncoding('utf8'); | |
let json = ''; | |
res.on('data', (chunk) => { json += chunk; }); | |
res.on('end', () => { | |
if (res.statusCode === 200) { | |
console.log('Webhook request was successful!'); | |
resolve(); | |
} else { | |
console.log('Webhook request failed!'); | |
reject(new Error(res.statusMessage)) | |
} | |
}); | |
}); | |
// Generic request error handling | |
req.on('error', e => reject(e)); | |
req.write(body); | |
req.end(); | |
}); | |
} | |
function messageText(payload) { | |
if (payload.text && payload.text.indexOf(' ') != -1) { | |
return payload.text; | |
} else { | |
const intro = "Okay folks, let's move this conversation"; | |
const channel = ('#' + payload.text).replace(/##/, '#'); | |
const to = (payload.text ? channel : 'elsewhere'); | |
return `${intro} ${to}.`; | |
} | |
} | |
exports.handler = async (event) => { | |
// The Slack slash command request body is URL encoded | |
const payload = querystring.parse(event.body); | |
if (payload.token !== SLACK_VERIFICATION_TOKEN) { | |
// Invalid Slack verification token on payload | |
throw 'Invalid verification token'; | |
} | |
try { | |
await postToWebhook(SLACK_INCOMING_WEBHOOK_URL, { | |
channel: `#${payload.channel_name}`, | |
text: messageText(payload), | |
link_names: 1, | |
mrkdwn: true | |
}); | |
// Returning an empty body to the Slack slash command request will | |
// preventany message from being posted to the channel (we've already | |
// sent our message using the incoming webhook). | |
return { statusCode: 200, headers: {}, body: '' }; | |
} catch (e) { | |
throw e; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment