se strict';
const Alexa = require('alexa-sdk');
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).
var welcomeMessage = "What is the prodcut number?"
var output = '';
var inputUserInput = '';
var querystring = require('querystring');
var https = require('https');
const handlers = {
'LaunchRequest': function () {
output = welcomeMessage;
this.emit(':ask', output);
},
'Unhandled': function () {
this.emit(':tell', 'Sorry this was unhanled');
},
'GetProdCodeIntent': function(){
var that = this;
var productCode = '';
//Make sure that the user did input a valid code
var productCodeSlotValid = isproductCodeSlotValid(this.event.request.intent);
if(productCodeSlotValid)
productCode = this.event.request.intent.slots.productCode.value;
console.log("Product code: " + productCode);
var bodyJson = JSON.stringify({
//Pass in what the query needs and I've hard coded my SAP Cloud Platform User
resourceIdentifiers: [{ "uri": "/Products('HT-" + productCode + "')" }],
username: "P****"
});
var options = {
//SAP CPms host
host: 'hcpms-p******trial.hanatrial.ondemand.com',
//This is the path for the Card Type GUID that you will find in your SAP Cloud Platform SAP Content to Go configuration
path: '/mobileservices/origin/hcpms/CARDS/v1/cardTypes/39649BB5-0C2C-4B91-A96A-10481C8B14D3/cardInstances',
port: '443',
method: 'POST',
headers: {
//You need to BASE64 encode your user and password
'Authorization': 'Basic *********************',
'Content-Type': 'application/json'
}
};
console.log('About to send https request');
var req = https.request(options, function (res) {
var body = '';
var statusCode = res.statusCode;
console.log('Status:', statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
if(statusCode == '201'){
console.log('Successfully processed HTTPS response');
body = JSON.parse(body);
console.log(body);
that.emit(':tell', "The SAP Content to Go Card " + productCode + " was sent");
}
else{
console.log("Card did not send");
that.emit(':tell', "The Card was not sent, make sure it wasn't already sent");
}
});
}).on('error', (e) => { console.error(e);
});
req.write(bodyJson);
req.end();
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
function isproductCodeSlotValid(intent) {
var productCodeSlotFilled = intent && intent.slots && intent.slots.productCode && intent.slots.productCode.value;
return productCodeSlotFilled
}
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
10 | |
10 | |
10 | |
8 | |
8 | |
8 |