Guest User

Untitled

a guest
Apr 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. "use strict";
  2.  
  3. require('dotenv').config();
  4.  
  5. /*
  6. * Configuration
  7. */
  8.  
  9. // NPM packages to get everything working
  10. const axios = require('axios');
  11. const bodyParser = require('body-parser');
  12. const express = require('express');
  13. const qs = require('querystring');
  14.  
  15. const dialogTemplate = require('./dialog');
  16.  
  17. // grab the environment variables
  18. const SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN;
  19. const SLACK_OAUTH_TOKEN = process.env.SLACK_OAUTH_TOKEN;
  20. const SLACK_CHANNEL = process.env.SLACK_CHANNEL;
  21. //const SLACK_POST_TO_CHANNEL = process.env.SLACK_POST_TO_CHANNEL;
  22.  
  23. // set up the Express app
  24. const app = express();
  25.  
  26. app.set('port', process.env.PORT || 3000);
  27. app.use(bodyParser.urlencoded({ extended: true }));
  28. app.use(bodyParser.json());
  29.  
  30. // set up Axios
  31. axios.defaults.baseURL = 'https://slack.com';
  32. axios.defaults.headers.common['Authorization'] = `Bearer ${SLACK_OAUTH_TOKEN}`;
  33. axios.defaults.headers.post['Content-Type'] = 'application/json';
  34.  
  35. /*
  36. * Default endpoint
  37. */
  38. app.get('/', (req, res) => {
  39. res.send('<h2>The app is running</h2> <p>Follow the' +
  40. ' instructions in the README to configure the Slack App and your
  41. environment variables.</p>');
  42. });
  43.  
  44.  
  45. /*
  46. * Endpoint to receive events to which your app is subscribed
  47. * https://api.slack.com/events-api
  48. */
  49.  
  50. //store the message ts for later use
  51. app.post('/slack/events', (req, res) => {
  52. switch (req.body.type) {
  53. case 'url_verification': {
  54. // When setting up your app, Slack sends a verification challenge to the URL you specify
  55. // and expects you to echo it back immediately.
  56. // https://api.slack.com/events-api#request_url_configuration__amp__verification
  57. res.send({ challenge: req.body.challenge });
  58. break;
  59. }
  60. case 'event_callback': {
  61. if (req.body.token === SLACK_VERIFICATION_TOKEN) {
  62. const event = req.body.event;
  63. if(event.channel === SLACK_CHANNEL) {
  64. if(event.type === 'link_shared'){
  65. res.status(200).end();
  66. var eventts //variable to store the timestamp of the link message for later use.
  67. eventts = req.body.event.message_ts;
  68. console.log(eventts);
  69.  
  70. // Checking for channel where link was posted
  71. if(event.channel === SLACK_CHANNEL){
  72. // The chat.postEphemeral method posts a message that only a specific user can see
  73. //attachments are the buttons produced
  74. // https://api.slack.com/methods/chat.postEphemeral
  75. axios.post('/api/chat.postEphemeral', {
  76. channel: event.channel,
  77. user: event.user,
  78. text:
  79. 'It looks like you have posted a SalesForce link. Please indicate urgency.',
  80. attachments: [
  81. {
  82. text: '',
  83. fallback:
  84. 'Please indicate urgency.',
  85. callback_id: 'urg_flag',
  86. color: '#3AA3E3',
  87. attachment_type: 'default',
  88. actions: [
  89. {
  90. name: 'chooser',
  91. text: 'Low',
  92. type: 'button',
  93. value: 'Low'
  94. },
  95. {
  96. name: 'chooser',
  97. text: 'Medium',
  98. type: 'button',
  99. value: 'Medium'
  100. },
  101. {
  102. name: 'chooser',
  103. text: 'High',
  104. type: 'button',
  105. value: 'High'
  106. }
  107. ]
  108. }
  109. ]
  110. })
  111. .then(function(res){
  112. console.log(res); //log out response message
  113. })
  114. .catch(function (error) {
  115. console.log(error);
  116. });
  117. }
  118.  
  119. /*
  120. * Endpoint to receive interactive components like message buttons, menus or
  121. dialogs from Slack.
  122. * https://api.slack.com/interactive-messages
  123. */
  124. app.post('/slack/components', (req, res) => {
  125. const payload = JSON.parse(req.body.payload);
  126. console.log(payload); //log the payload
  127.  
  128. //verify the request is coming from Slack by validating the token
  129. if (payload.token === SLACK_VERIFICATION_TOKEN) {
  130. //respond immediately
  131. res.status(200).end();
  132.  
  133. switch (payload.type){
  134. case 'interactive_message': {
  135. // More info on what interactive message submissions looke like here:
  136. // https://api.slack.com/docs/interactive-message-field-guide#action_payload
  137. var action = payload.actions[0];
  138. // the user has clicked the button to indicate urgency
  139. if(action['value'] === 'High'){
  140. axios.post('https://slack.com/api/reactions.add', {
  141. name:'red_circle',
  142. channel: payload.channel.id,
  143. timestamp: eventts,
  144. }
  145. ).then(function(res){
  146. console.log(res);
  147. })
  148. .catch(function (error) {
  149. console.log(error);
  150. });
  151. }
  152. }
  153. case 'interactive_message': {
  154. // More info on what interactive message submissions looke like here:
  155. // https://api.slack.com/docs/interactive-message-field-guide#action_payload
  156. const action = payload.actions[0];
  157. // the user has clicked the button to indicate urgency
  158. if(action['value'] === 'Medium'){
  159. axios.post('https://slack.com/api/reactions.add', {
  160. name:'yellow_heart',
  161. channel: payload.channel.id,
  162. timestamp: eventts,
  163. }
  164. ).then(function(res){
  165. console.log(res);
  166. })
  167. .catch(function (error) {
  168. console.log(error);
  169. });
  170. }
  171. }
  172. case 'interactive_message': {
  173. // More info on what interactive message submissions looke like here:
  174. // https://api.slack.com/docs/interactive-message-field-guide#action_payload
  175. const action = payload.actions[0];
  176. // the user has clicked the button to indicate urgency
  177. if(action['value'] === 'Low'){
  178. axios.post('https://slack.com/api/reactions.add', {
  179. name:'green_apple',
  180. channel: payload.channel.id,
  181. timestamp: eventts,
  182. }
  183. ).then(function(res){
  184. console.log(res);
  185. })
  186. .catch(function (error) {
  187. console.log(error);
  188. });
  189. }
  190. }
  191. }
  192. } else { res.sendStatus(500); }
  193. });
  194. }break; //end of overall scope for variable
  195. }
  196. }
  197. }
  198. }
  199. });
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. /*
  207. * Start the express server
  208. */
  209. app.listen(app.get('port'), () => {
  210. console.log(`App listening on port ${app.get('port')}!`);
  211. });
Add Comment
Please, Sign In to add comment