Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JordanSchuetz/9688d8b5b10fadeda4ef9906871bec6e to your computer and use it in GitHub Desktop.
Save JordanSchuetz/9688d8b5b10fadeda4ef9906871bec6e to your computer and use it in GitHub Desktop.
export default (request, response) => {
const pubnub = require('pubnub');
const xhr = require('xhr');
const db = require("kvstore");
console.log('request',request);
//extract from typeform
const tf_data = JSON.parse(request.body);
console.log('tf_data', tf_data)
const { answers } = tf_data.form_response;
console.log('answers', answers);
const full_name = answers.filter(a => a.field.id==='shiw8XAduZWu')[0].text;
const company = answers.filter(a => a.field.id==='Tx8ZY4soSdrk')[0].text;
const email = answers.filter(a => a.field.id==='OlvdHHQ58mN2')[0].text;
let host_name = answers.filter(a => a.field.id==='VkkU6SMbEQmT')[0].choice.label;
host_name = host_name.replace(/\(\@/,'(<@').replace(/\)/,'>)'); //format to slack
//format slack message
let message = `:wave: ${host_name}, *${full_name}* from *${company}* is at the door.`;
//send to slack
const http_options = {
"headers": {
"Content-Type": "application/json"
},
"method": "POST", // or PUT
"body": JSON.stringify({
"text": message,
"link_names":1,
"parse":"full",
"attachments": [{
"text": ":question: Should I let them in?",
"fallback": ":question: Should I let them in?",
"callback_id": "wopr_game",
"color": "#FFA500",
"attachment_type": "default",
"actions": [
{
"name": "open_door",
"text": "Yes",
"type": "button",
"value": true
},
{
"name": "open_door",
"text": "No",
"style": "danger",
"type": "button",
"value": false,
"confirm": {
"title": "Are you sure?",
"text": "You wouldn't want them to come steal your candies :candy:, would you?",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
})
};
const url = "https://hooks.slack.com/services/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";
return xhr.fetch(url, http_options).then((x) => {
//console.log(body);
return response.send();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment