使用wcf.js和node.js与Alexa联系WCF服务无法正常工作?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用wcf.js和node.js与Alexa联系WCF服务无法正常工作?相关的知识,希望对你有一定的参考价值。
使用Alexa-sdk node.js lambda aws服务wcf.js xml2js和VB.NET Web服务。
我的想法是将两个字符串发送到Web服务并获得一个字符串作为回报,然后alexa会说话。一切正常,直到Alexa应该说出返回的字符串。
这是一个修改过的Hello World应用程序。将两个字符串发送到WCF服务。但是,proxy.send之后的console.log行似乎在代理行完成之前运行,并且stringspeech变量未定义,就好像代理行还没有完成运行一样。
码:
'use strict';
var Alexa = require("alexa-sdk");
// For detailed tutorial on how to making a Alexa skill,
// please visit us at http://alexa.design/build
exports.handler = function(event, context) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest': function () {
this.emit('SayHello');
},
'HelloWorldIntent': function () {
this.emit('SayHello');
},
'MyNameIsIntent': function () {
this.emit('SayHelloName');
},
'SayHello': function () {
console.log("start");
var stringspeech;
var parseString = require('xml2js').parseString;
var BasicHttpBinding = require('wcf.js').BasicHttpBinding
, Proxy = require('wcf.js').Proxy
, binding = new BasicHttpBinding()
, proxy = new Proxy (binding, "http://service.menofnoblegirth.com/service1.svc")
, message = "<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<Header />" +
"<Body>" +
"<GetData xmlns='http://tempuri.org/'>" +
"<value>test</value>" +
"<value2>test2</value2>" +
"</GetData>" +
"</Body>" +
"</Envelope>"
proxy.send(message, "http://tempuri.org/IService1/GetData", function (response, ctx) {
parseString(response, function(error, result) {
console.log(JSON.stringify(result));
var stringfun = JSON.stringify(result);
var objectValue = JSON.parse(stringfun);
console.log(objectValue["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0]);
stringspeech = objectValue["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0];
console.log("The variable stringspeech is now set to: " + stringspeech);
});
});
console.log("The variable stringspeech is FINALLY set to: " + stringspeech);
this.response.speak(stringspeech)
.cardRenderer(stringspeech, stringspeech);
this.emit(':responseReady');
},
'SayHelloName': function () {
var name = this.event.request.intent.slots.name.value;
this.response.speak('Hello ' + name)
.cardRenderer('hello world', 'hello ' + name);
this.emit(':responseReady');
},
'SessionEndedRequest' : function() {
console.log('Session ended with reason: ' + this.event.request.reason);
},
'AMAZON.StopIntent' : function() {
this.response.speak('Bye');
this.emit(':responseReady');
},
'AMAZON.HelpIntent' : function() {
this.response.speak("You can try: 'alexa, hello world' or 'alexa, ask hello world my" +
" name is awesome Aaron'");
this.emit(':responseReady');
},
'AMAZON.CancelIntent' : function() {
this.response.speak('Bye');
this.emit(':responseReady');
},
'Unhandled' : function() {
this.response.speak("Sorry, I didn't get that. You can try: 'alexa, hello world'" +
" or 'alexa, ask hello world my name is awesome Aaron'");
}
};
LogFile结果:
START RequestId: 4345330e-1c06-11e8-90fc-970a92c343fd Version: $LATEST
2018-02-27T21:36:26.067Z 4345330e-1c06-11e8-90fc-970a92c343fd Warning: Application ID is not set
2018-02-27T21:36:26.106Z 4345330e-1c06-11e8-90fc-970a92c343fd start
2018-02-27T21:36:29.789Z 4345330e-1c06-11e8-90fc-970a92c343fd The variable stringspeech is FINALLY set to: undefined
2018-02-27T21:36:30.208Z 4345330e-1c06-11e8-90fc-970a92c343fd
{
"s:Envelope": {
"$": {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
},
"s:Body": [
{
"GetDataResponse": [
{
"$": {
"xmlns": "http://tempuri.org/"
},
"GetDataResult": [
"You entered test test2"
]
}
]
}
]
}
}
2018-02-27T21:36:30.226Z 4345330e-1c06-11e8-90fc-970a92c343fd You entered test test2
2018-02-27T21:36:30.226Z 4345330e-1c06-11e8-90fc-970a92c343fd The variable stringspeech is now set to: You entered test test2
END RequestId: 4345330e-1c06-11e8-90fc-970a92c343fd
REPORT RequestId: 4345330e-1c06-11e8-90fc-970a92c343fd Duration: 4349.27 ms Billed Duration: 4400 ms Memory Size: 128 MB Max Memory Used: 48 MB
答案
proxy.send调用正在运行异步。要解决这个问题,我必须更改proxy.send行,以便可以从proxy.send函数中访问this.response.speak。它现在看起来像这样:
proxy.send(message, "http://tempuri.org/IService1/GetData", (response, ctx) => {
parseString(response, function(error, result) {
console.log(JSON.stringify(result));
var stringfun = JSON.stringify(result);
var objectValue = JSON.parse(stringfun);
console.log(objectValue["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0]);
stringspeech = objectValue["s:Envelope"]["s:Body"][0]["GetDataResponse"][0]["GetDataResult"][0];
console.log("The variable stringspeech is now set to: " + stringspeech);
console.log("The variable stringspeech is FINALLY set to: " + stringspeech);
this.response.speak(stringspeech).cardRenderer(stringspeech, stringspeech);
this.emit(':responseReady');
});
});
以上是关于使用wcf.js和node.js与Alexa联系WCF服务无法正常工作?的主要内容,如果未能解决你的问题,请参考以下文章
智能家居开源系统:Alexa、Home Assistant、HomeKit生态系介绍
通过 API-KEY 使用来自外部 API 的数据的 Alexa 技能
适用于 Linux (WSL) 的 Windows 子系统使用与 Windows 共享的 Node.js 安装:Node.js npm 和 npx 二进制文件不起作用