我正在使用twilio,当调用我的twilio号码时,它调用web钩子,我使用lambda函数作为web钩子,
twilio期望来自web钩子的xml (以前称为twiml)响应,而我无法从lambda函数发送xml响应。
我使用的是无服务器框架
下面是我的代码函数:
module.exports.voice = (event, context, callback) => {
console.log("event", JSON.stringify(event))
var twiml = new VoiceResponse();
twiml.say({ voice: 'alice' }, 'Hello, What type of podcast would you like to listen? ');
twiml.say({ voice: 'alice' }, 'Please record your response after the beep. Press any key to finish.');
twiml.record({
transcribe: true,
transcribeCallback: '/voice/transcribe',
maxLength: 10
});
console.log("xml: ", twiml.toString())
context.succeed({
body: twiml.toString()
});
};yml:
service: aws-nodejs
provider:
name: aws
runtime: nodejs6.10
timeout: 10
iamRoleStatements:
- Effect: "Allow"
Action: "*"
Resource: "*"
functions:
voice:
handler: handler.voice
events:
- http:
path: voice
method: post
integration: lambda
response:
headers:
Content-Type: "'application/xml'"
template: $input.path("$")
statusCodes:
200:
pattern: '.*' # JSON response
template:
application/xml: $input.path("$.body") # XML return object
headers:
Content-Type: "'application/xml'"响应:


如果我在代码中犯了什么错误,请告诉我,我还在github上创建了一个问题
谢谢,Inzamam Malik
发布于 2017-10-12 18:37:49
您不需要那么多地处理serverless.yml。以下是简单的方法:
在serverless.yml..。
functions:
voice:
handler: handler.voice
events:
- http:
path: voice
method: post(响应、标题、内容类型、模板和statusCodes不是必需的)
然后,您可以在函数中设置statusCode和Content。
所以删除这部分..。
context.succeed({
body: twiml.toString()
});..。并将其替换为:
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/xml',
},
body: twiml.toString(),
};
callback(null, response);Lambda代理集成(这是默认的)将它组装成一个适当的响应。
就我个人而言,我发现这种方式更简单、更易读。
发布于 2017-05-07 11:20:11
您需要您的lambda是一个“代理”类型,因此您设置了body属性。但试着去做
context.succeed(twiml.toString());,它将直接发送“字符串”作为结果。
或者使用回调参数:
function(event, context, callback) {
callback(null, twiml.toString())
}发布于 2017-05-14 20:47:41
正如@UXDart所提到的,您将无法使用标准集成来完成这一任务。您应该设置一个与Lambda的代理集成,如这里-http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-nodejs。
这将更好地与您所做的工作,返回xml通过api网关。
https://stackoverflow.com/questions/43829577
复制相似问题