Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JordanSchuetz/03d1cb9ac7e4db5b5a147326599c8f37 to your computer and use it in GitHub Desktop.
Save JordanSchuetz/03d1cb9ac7e4db5b5a147326599c8f37 to your computer and use it in GitHub Desktop.
export default (request, response) => {
const kvstore = require('kvstore');
const xhr = require('xhr');
const PubNub = require("pubnub");
let pubnub = new PubNub({
publishKey: 'demo',
subscribeKey: 'demo'
});
let bodyString = request.body;
console.log('request',request); // Log the request envelope passed
var p = request.body
var c = request.body.replace(/payload\=/g, '')
console.log('payload', c)
const payload = JSON.parse(decodeURIComponent(c))
console.log('interactive', payload)
const action = payload.actions[0]
var msg, type = ''
if(action.name === "open_door"){
if(action.value){ //open_door
pubnub.publish({
"channel": "pubnub_iot_house",
"message": {
name: 'garage',
value: 1
}
})
msg = `:door: has been opened.`
}else{
msg = `:no_entry_sign: will remain closed.`
}
}
// // Set the status code - by default it would return 200
response.status = 200;
return response.send({
text: payload.original_message.text.replace(/\+/g,' '),
fallback: payload.original_message.text.replace(/\+/g,' '),
link_names:1,
parse:"full",
attachments:[{
"fallback": msg,
"text": msg,
"color": "#7CD197"
}],
replace_original: true,
response_type: 'in_channel'
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment