gpt4 book ai didi

twilio - 无法使用无服务器框架发送 xml 响应

转载 作者:行者123 更新时间:2023-12-01 01:51:51 24 4
gpt4 key购买 nike

我正在使用 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()
});
};

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'"

回复:
enter image description here
enter image description here

如果我在代码中犯了一些错误,请告诉我
还创建了一个 issue在 github 上

谢谢,
因扎曼·马利克

最佳答案

您不需要过多地使用 serverless.yml。这是简单的方法:

在 serverless.yml 中...

functions:
voice:
handler: handler.voice
events:
- http:
path: voice
method: post

(响应、标题、内容类型、模板和状态代码不是必需的)

然后你可以在你的函数中设置 statusCode 和 Content-Type 。

所以删除这部分...
context.succeed({
body: twiml.toString()
});

...并将其替换为:
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/xml',
},
body: twiml.toString(),
};

callback(null, response);

Lambda 代理集成(这是默认设置)将其组合成正确的响应。

我个人觉得这种方式更简单,更易读。

关于twilio - 无法使用无服务器框架发送 xml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43829577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com