亚马逊Alexa技能套件 - 没有回应Servicenow

时间:2017-12-15 02:29:14

标签: amazon-web-services aws-lambda alexa alexa-skills-kit

当我去输入一个样本话语时,例如"列出我的事件"在开发人员控制台的测试选项卡上,它返回错误:

  

回复无效

这是我的lambda函数上的index.js代码:

var querystring = require('querystring');
var https = require('https');
var fs = require('fs');
function buildLinkAccountResponse() {
var alexaResponse = {
version : "1.0",
response : {
card : {
type : "LinkAccount"
},
outputSpeech : {
ssml : "<speak>Please link your account</speak>"
}
}
};
return alexaResponse;
}
exports.handler = function(event, context, callback) {
console.info('Received event', event);
// Grab the access token from the JSON payload
var accessToken = event.session.user.accessToken;
// Check if access token is present and if not ask for account linking
if (!accessToken) {
callback(null, buildLinkAccountResponse());
return;
}
// Null out the access token so we don't get leakage during logging on 
ServiceNow instance
// end
event.session.user.accessToken = "";
event.context.System.user.accessToken = "";
// Build the post string from the modified json payload
var post_data = JSON.stringify(event);
// An object of options to indicate where to post to
var post_options = {
host : 'nztedev.service-now.com',
port : '443',
path : '/api/x_nzte_ask_api/restapi/skill/route',
method : 'POST',
headers : {
'Content-Type' : 'application/json',
'Content-Length' : post_data.length,
'Authorization' : 'Bearer ' + accessToken,
}
};
var post_request = https.request(post_options, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
// Custom to ServiceNow REST enpoint
var error = response.error;
if (error) {
// Build link account card
response = buildLinkAccountResponse();
}
response.debug = {};
response.debug.attributes = {};
response.debug.attributes.sn_host = post_options.host;
response.debug.attributes.sn_api = post_options.path;
if (error) {
response.debug.attributes.sn_err = error;
}
console.info('OK: ' + JSON.stringify(response));
callback(null, response);
});
res.on('error', function(e) {
// On error send link acount card
var response = buildLinkAccountResponse();
response.debug = {};
response.debug.attributes = {};
response.debug.attributes.error = e;
console.log('ERROR: ' + JSON.stringify(response));
callback(null, response);
});
});
// post the data
post_request.write(post_data);
post_request.end();
};

我跟着this pdf并一步一步地做了。

0 个答案:

没有答案