我正在使用twilio,当调用我的twilio号码时它会调用webhook,我使用lambda函数作为webhook,
twilio期待来自webhook的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() }); };
阳明:
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上创建了一个issue
谢谢,
Inzamam Malik
解决方法
你不需要乱用serverless.yml.这是一个简单的方法:
在serverless.yml中……
functions: voice: handler: handler.voice events: - http: path: voice method: post
(不需要响应,标题,内容类型,模板和statusCodes)
然后,您可以在函数中设置statusCode和Content-Type.
所以删除这部分……
context.succeed({ body: twiml.toString() });
…并将其替换为:
const response = { statusCode: 200,headers: { 'Content-Type': 'text/xml',},body: twiml.toString(),}; callback(null,response);
Lambda代理集成(默认情况下)将其组合成适当的响应.
我个人觉得这种方式更简单,更易读.