Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- require('dotenv').config();
- /*
- * Configuration
- */
- // NPM packages to get everything working
- const axios = require('axios');
- const bodyParser = require('body-parser');
- const express = require('express');
- const qs = require('querystring');
- const dialogTemplate = require('./dialog');
- // grab the environment variables
- const SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN;
- const SLACK_OAUTH_TOKEN = process.env.SLACK_OAUTH_TOKEN;
- const SLACK_CHANNEL = process.env.SLACK_CHANNEL;
- //const SLACK_POST_TO_CHANNEL = process.env.SLACK_POST_TO_CHANNEL;
- // set up the Express app
- const app = express();
- app.set('port', process.env.PORT || 3000);
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(bodyParser.json());
- // set up Axios
- axios.defaults.baseURL = 'https://slack.com';
- axios.defaults.headers.common['Authorization'] = `Bearer ${SLACK_OAUTH_TOKEN}`;
- axios.defaults.headers.post['Content-Type'] = 'application/json';
- /*
- * Default endpoint
- */
- app.get('/', (req, res) => {
- res.send('<h2>The app is running</h2> <p>Follow the' +
- ' instructions in the README to configure the Slack App and your
- environment variables.</p>');
- });
- /*
- * Endpoint to receive events to which your app is subscribed
- * https://api.slack.com/events-api
- */
- //store the message ts for later use
- app.post('/slack/events', (req, res) => {
- switch (req.body.type) {
- case 'url_verification': {
- // When setting up your app, Slack sends a verification challenge to the URL you specify
- // and expects you to echo it back immediately.
- // https://api.slack.com/events-api#request_url_configuration__amp__verification
- res.send({ challenge: req.body.challenge });
- break;
- }
- case 'event_callback': {
- if (req.body.token === SLACK_VERIFICATION_TOKEN) {
- const event = req.body.event;
- if(event.channel === SLACK_CHANNEL) {
- if(event.type === 'link_shared'){
- res.status(200).end();
- var eventts //variable to store the timestamp of the link message for later use.
- eventts = req.body.event.message_ts;
- console.log(eventts);
- // Checking for channel where link was posted
- if(event.channel === SLACK_CHANNEL){
- // The chat.postEphemeral method posts a message that only a specific user can see
- //attachments are the buttons produced
- // https://api.slack.com/methods/chat.postEphemeral
- axios.post('/api/chat.postEphemeral', {
- channel: event.channel,
- user: event.user,
- text:
- 'It looks like you have posted a SalesForce link. Please indicate urgency.',
- attachments: [
- {
- text: '',
- fallback:
- 'Please indicate urgency.',
- callback_id: 'urg_flag',
- color: '#3AA3E3',
- attachment_type: 'default',
- actions: [
- {
- name: 'chooser',
- text: 'Low',
- type: 'button',
- value: 'Low'
- },
- {
- name: 'chooser',
- text: 'Medium',
- type: 'button',
- value: 'Medium'
- },
- {
- name: 'chooser',
- text: 'High',
- type: 'button',
- value: 'High'
- }
- ]
- }
- ]
- })
- .then(function(res){
- console.log(res); //log out response message
- })
- .catch(function (error) {
- console.log(error);
- });
- }
- /*
- * Endpoint to receive interactive components like message buttons, menus or
- dialogs from Slack.
- * https://api.slack.com/interactive-messages
- */
- app.post('/slack/components', (req, res) => {
- const payload = JSON.parse(req.body.payload);
- console.log(payload); //log the payload
- //verify the request is coming from Slack by validating the token
- if (payload.token === SLACK_VERIFICATION_TOKEN) {
- //respond immediately
- res.status(200).end();
- switch (payload.type){
- case 'interactive_message': {
- // More info on what interactive message submissions looke like here:
- // https://api.slack.com/docs/interactive-message-field-guide#action_payload
- var action = payload.actions[0];
- // the user has clicked the button to indicate urgency
- if(action['value'] === 'High'){
- axios.post('https://slack.com/api/reactions.add', {
- name:'red_circle',
- channel: payload.channel.id,
- timestamp: eventts,
- }
- ).then(function(res){
- console.log(res);
- })
- .catch(function (error) {
- console.log(error);
- });
- }
- }
- case 'interactive_message': {
- // More info on what interactive message submissions looke like here:
- // https://api.slack.com/docs/interactive-message-field-guide#action_payload
- const action = payload.actions[0];
- // the user has clicked the button to indicate urgency
- if(action['value'] === 'Medium'){
- axios.post('https://slack.com/api/reactions.add', {
- name:'yellow_heart',
- channel: payload.channel.id,
- timestamp: eventts,
- }
- ).then(function(res){
- console.log(res);
- })
- .catch(function (error) {
- console.log(error);
- });
- }
- }
- case 'interactive_message': {
- // More info on what interactive message submissions looke like here:
- // https://api.slack.com/docs/interactive-message-field-guide#action_payload
- const action = payload.actions[0];
- // the user has clicked the button to indicate urgency
- if(action['value'] === 'Low'){
- axios.post('https://slack.com/api/reactions.add', {
- name:'green_apple',
- channel: payload.channel.id,
- timestamp: eventts,
- }
- ).then(function(res){
- console.log(res);
- })
- .catch(function (error) {
- console.log(error);
- });
- }
- }
- }
- } else { res.sendStatus(500); }
- });
- }break; //end of overall scope for variable
- }
- }
- }
- }
- });
- /*
- * Start the express server
- */
- app.listen(app.get('port'), () => {
- console.log(`App listening on port ${app.get('port')}!`);
- });
Add Comment
Please, Sign In to add comment