Skip to content

Instantly share code, notes, and snippets.

@DennisAlund
Last active April 5, 2017 06:24
Show Gist options
  • Select an option

  • Save DennisAlund/74df6777b0b46416bdf2b62c81b26904 to your computer and use it in GitHub Desktop.

Select an option

Save DennisAlund/74df6777b0b46416bdf2b62c81b26904 to your computer and use it in GitHub Desktop.
Firebase cloud function for Slack button actions in medium article https://medium.com/evenbit/151c1c98641d
export const message_action = functions.https.onRequest(async (request, response) => {
if (request.method !== "POST") {
console.error(`Got unsupported ${request.method} request. Expected POST.`);
return response.send(405, "Only POST requests are accepted");
}
if (!request.body && request.body.payload) {
return response.send(405, "Expected a message action payload.");
}
const action = JSON.parse(request.body.payload);
if (action.callback_id !== SLACK_ACTION_REQUEST_PING) {
return response.send(405, "Only ping pong actions are implemented!");
}
// Handle the actions later, Slack expect this request to return within 3000ms
await admin.database().ref("actions").push(action);
// Update the buttons to try and limit the amount of player inputs
if (action.actions[0].name.startsWith("1")) {
action.original_message.attachments[0].actions[0].style = "primary";
action.original_message.attachments[0].actions[0].name = "2.pong";
} else if (action.actions[0].name.startsWith("2")) {
action.original_message.attachments[0].actions[0].style = "danger";
action.original_message.attachments[0].actions[0].name = "3.pong";
} else {
action.original_message.text = `The current game of PING (id ${action.actions[0].value}) is over!`;
action.original_message.attachments[0].actions = [];
}
return response.contentType("json").status(200).send(action.original_message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment