Skip to content

Commit

Permalink
Integrate message dialog from email
Browse files Browse the repository at this point in the history
  • Loading branch information
pravin-sivabalan committed Jul 21, 2018
1 parent edcf32b commit 5ca90cc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 121 deletions.
43 changes: 42 additions & 1 deletion src/routes/mailgun-router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const mailgun = require('mailgun-js')({ apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN });

router.post('/messages', (req, res, next) => {
Expand All @@ -10,7 +11,35 @@ router.post('/messages', (req, res, next) => {
bodyPlain: req.body['body-plain']
}

res.sendStatus(204);
const assignee = await roundRobin();
const name = 'Sawyer';
axios.post('https://slack.com/api/chat.postMessage', qs.stringify({
token: process.env.SLACK_AUTH_TOKEN,
channel: assignee,
text: `Bzz! Bzz! Hey, you've been assigned an email`,
attachments: JSON.stringify([{
"text": receivedEmail.subject,
"fallback": "Sorry you were unable to reply",
"callback_id": "wopr_game",
"color": "#FEE224",
"attachment_type": "default",
"actions": [
{
"name": "reply",
"text": "Reply",
"type": "button",
"value": "reply"
}
]
}
]),
})).then((result) => {
console.log('sendConfirmation: %o', result.data);
res.sendStatus(204);
}).catch((err) => {
console.error(err);
res.sendStatus(500);
});
});


Expand Down Expand Up @@ -40,4 +69,16 @@ router.post('/send', async (req, res, next) => {
}
});

function getTheUser() {
const user = unassignedUsers[Math.floor((Math.random() * unassignedUsers.length) + 1)];
if (unassignedUsers.length === 1) {
unassignedUsers = '';
return unassignedUsers[0];
}
unassignedUsers = unassignedUsers.filter(function(el) {
return el !== user;
})
return user;
}

module.exports = router;
120 changes: 0 additions & 120 deletions src/routes/receive-router.js
Original file line number Diff line number Diff line change
@@ -1,120 +0,0 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const qs = require('qs');
var unassignedUsers;

router.post('/', async (req, res, next) => {

const { token, text, trigger_id } = req.body;
const assignee = await roundRobin();
console.log(assignee);
const name = 'Sawyer';
if (token === process.env.SLACK_VERIFICATION_TOKEN) {
console.log('sendConfirmation');
axios.post('https://slack.com/api/chat.postMessage', qs.stringify({
token: process.env.SLACK_AUTH_TOKEN,
channel: assignee,
text: `Bzz! Bzz! Hey, you've been assigned an email`,
attachments: JSON.stringify([{
"text": "Subject",
"fallback": "Sorry you were unable to reply",
"callback_id": "wopr_game",
"color": "#FEE224",
"attachment_type": "default",
"actions": [
{
"name": "reply",
"text": "Reply",
"type": "button",
"value": "reply"
}
]
}
]),
})).then((result) => {
console.log('sendConfirmation: %o', result.data);
}).catch((err) => {
console.log('sendConfirmation error: %o', err);
console.error(err);
});
}

});

// Take in a user list and
async function roundRobin() {
if (unassignedUsers === undefined || unassignedUsers.length == 0) {
// Reset unassignedUsers!
const channelID = 'CBUAADA1Y';
const tokenWithChannel = {
token: process.env.SLACK_AUTH_TOKEN,
channel: 'CBUAADA1Y'
};
try {
console.log((await axios.post('https://slack.com/api/channels.info', qs.stringify(tokenWithChannel))).data.channel.members);
unassignedUsers = (await axios.post('https://slack.com/api/channels.info', qs.stringify(tokenWithChannel))).data.channel.members;
return getTheUser();
} catch (error) {
console.log('error', error);
}
} else {
return getTheUser();
}
// Do the Round Robin:

};

function getTheUser() {
const user = unassignedUsers[Math.floor((Math.random() * unassignedUsers.length) + 1)];
console.log('user', user);
// unassignedUsers.remove(user);
if (unassignedUsers.length === 1) {
unassignedUsers = '';
return unassignedUsers[0];
}
unassignedUsers = unassignedUsers.filter(function(el) {
return el !== user;
})

console.log('user: ', user);

console.log('Unassigned: ', unassignedUsers);
return user;
}

const msg = {
"text": "Hi friend! Bzz Bzz. You've been assigned an email!",
"attachments": [
{
"text": "Choose something you need help with:",
"fallback": "You are unable to choose a task",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "qa",
"text": "Question/Answer",
"type": "button",
"value": "qa"
},
{
"name": "code",
"text": "Code Snippet",
"type": "button",
"value": "code"
},
{
"name": "url",
"text": "URL",
"type": "button",
"value": "url"
}
]
}
]
}


module.exports = router;

0 comments on commit 5ca90cc

Please sign in to comment.