Inz*_*lik 5 twilio aws-lambda aws-api-gateway serverless-framework twilio-twiml
我正在使用 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()
});
};
Run Code Online (Sandbox Code Playgroud)
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'"
Run Code Online (Sandbox Code Playgroud)
请让我知道我是否在代码中犯了一些错误也在github 上创建了一个问题
谢谢, Inzamam Malik
小智 6
您不需要过多地使用 serverless.yml。这是简单的方法:
在 serverless.yml 中...
functions:
voice:
handler: handler.voice
events:
- http:
path: voice
method: post
Run Code Online (Sandbox Code Playgroud)
(响应、标题、内容类型、模板和状态代码不是必需的)
然后你可以在你的函数中设置 statusCode 和 Content-Type 。
所以删除这部分...
context.succeed({
body: twiml.toString()
});
Run Code Online (Sandbox Code Playgroud)
...并将其替换为:
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/xml',
},
body: twiml.toString(),
};
callback(null, response);
Run Code Online (Sandbox Code Playgroud)
Lambda 代理集成(这是默认设置)将其组合成正确的响应。
我个人觉得这种方式更简单,更易读。
小智 1
您需要 lambda 为“代理”类型,因此您设置了 body 属性。但只是尝试做
context.succeed(twiml.toString());
Run Code Online (Sandbox Code Playgroud)
这将直接发送“字符串”作为结果
或使用回调参数:
function(event, context, callback) {
callback(null, twiml.toString())
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2178 次 |
| 最近记录: |