Last active
July 17, 2017 19:53
-
-
Save robcalcroft/468ad6dfacf4de3007a1075e3c643447 to your computer and use it in GitHub Desktop.
Opsview Alexa skill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run `npm i --save alexa-sdk request` for dependencies | |
const Alexa = require('alexa-sdk'); | |
const request = require('request'); | |
const handlers = { | |
opsviewAuthenticate() { | |
request({ | |
method: 'POST', | |
uri: '********/api/auth', | |
qs: { | |
opsview_username: process.env.OPSVIEW_USERNAME, | |
opsview_password: process.env.OPSVIEW_PASSWORD, | |
user: process.env.OPSVIEW_USERNAME, | |
}, | |
}, (error, response, body) => { | |
if (error) { | |
return this.emit(':tell', 'Oops, there was an error talking to Richards bot'); | |
} | |
const json = JSON.parse(body); | |
if (json.user) { | |
return this.emit(':tell', 'Authentication complete lmao'); | |
} | |
return this.emit(':tell', 'Authentication not complete, could not see user in response'); | |
}); | |
}, | |
serviceStatus() { | |
// | |
// status | |
}, | |
hostStatus() { | |
request({ | |
uri: '********/api/host-status', | |
qs: { | |
type: 'HOST_STATUS', | |
user: process.env.OPSVIEW_USERNAME, | |
host: this.event.request.intent.slots.HOST_OR_SERVICE.value, | |
}, | |
}, (error, response, body) => { | |
if (error) { | |
return this.emit(':tell', 'Oops, there was an error talking to the bot'); | |
} | |
const json = JSON.parse(body); | |
return this.emit(':tell', `Status of ${this.event.request.intent.slots.HOST_OR_SERVICE.value} is ${json.status}`); | |
}); | |
}, | |
Unhandled() { | |
this.emit(':tell', 'I\'m a stupid robot who doesn\'t work, end'); | |
}, | |
}; | |
exports.handler = (event, context) => { | |
const alexaInstance = Alexa.handler(event, context); | |
alexaInstance.APP_ID = process.env.ALEXA_APP_ID; | |
alexaInstance.appId = process.env.ALEXA_APP_ID; | |
alexaInstance.registerHandlers(handlers); | |
alexaInstance.execute(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment