[英]Serve audio file using Twilio serverless SDK
I'm trying to develop a Twilio function using the serverless SDK that can serve audio files directly to HTTP requests, following the guide here which I've got working fine using the service editor in the Twilio console. I'm trying to develop a Twilio function using the serverless SDK that can serve audio files directly to HTTP requests, following the guide here which I've got working fine using the service editor in the Twilio console. However I'm now trying to use the serverless SDK to develop the service locally and the exact same setup just doesn't work.
但是,我现在尝试使用无服务器 SDK 在本地开发服务,但完全相同的设置不起作用。
I've got the mp3 audio file as a private asset at assets/hello.mp3
, and the Node function functions/handler.js
which is public.我在
assets/hello.mp3
有 mp3 音频文件作为私有资产,并且节点 function functions/handler.js
/handler.js 是公共的。 My function is:我的 function 是:
const fs = require('fs');
exports.handler = function(context, event, callback) {
var phrase = event.phrase;
var filePath = Runtime.getAssets()[`/${phrase}.mp3`].path;
var buffer = fs.readFileSync(filePath);
var stat = fs.statSync(filePath);
var response = new Twilio.Response();
response.setBody(buffer);
response.appendHeader('Content-Type', 'audio/mpeg');
response.appendHeader('Content-Length', stat.size);
return callback(null, response);
};
The only modification from the example in the docs is that I'm allowing the user to specify the file name with the GET parameter phrase
.文档中示例的唯一修改是我允许用户使用 GET 参数
phrase
指定文件名。
Whenever I try to request to http://localhost:3000/handler?phrase=hello
Chrome detects that it's receiving an audio file but doesn't load or play it.每当我尝试向
http://localhost:3000/handler?phrase=hello
请求时,Chrome 检测到它正在接收音频文件,但没有加载或播放它。 The repsonse is:回应是:
{type: "Buffer",…}
data: [255, 243, 68, 196, 0, 17, 82, 130, 20, 0, 24, 68, 185, 52, 68, 74, 44, 234, 110, 238, 250, 23, 204,…]
type: "Buffer"
This indicates to me that the buffer is being parsed as JSON data rather than the raw audio data.这向我表明缓冲区被解析为 JSON 数据而不是原始音频数据。 However I've tried all the variants of
buffer.toString()
, different encodings and file streams that I can think of and nothing fixes it.但是,我已经尝试了所有我能想到的
buffer.toString()
变体、不同的编码和文件流,但没有任何解决方法。 There are no errors and the HTTP status code is 200. The HTTP response headers are:没有错误,并且 HTTP 状态代码为 200。 HTTP 响应标头为:
HTTP/1.1 200 OK
X-Powered-By: Express
Surrogate-Control: no-store
Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate
Pragma: no-cache
Expires: 0
Content-Type: audio/mpeg; charset=utf-8
Content-Length: 27077
ETag: W/"69c5-hX8H/5UaULdPYVqq2abJK7EeJmg"
Date: Mon, 22 Aug 2022 21:03:35 GMT
Connection: keep-alive
Keep-Alive: timeout=5
I've tried following a lot of examples where people have had similar problems serving non-text data with Node but the Twilio Response object seems fairly restrictive and I can't get any of them to work.我尝试了很多示例,其中人们在使用 Node 提供非文本数据时遇到了类似的问题,但是 Twilio 响应 object 似乎相当严格,我无法让它们中的任何一个工作。 Thanks for any suggestions!
感谢您的任何建议!
Also the reason I'm trying to set up the function like this is that I've got quite a large number of possible sentences that I might want to play in a call, so I want to store several short phrases as assets and concatenate them together in a function.另外,我尝试像这样设置 function 的原因是我有很多可能的句子,我可能想在通话中播放,所以我想将几个短语存储为资产并将它们连接起来一起在 function 中。 I want the clips to be private assets so they're accessible to the function with minimal latency, and the concatenation needs to be really fast so I don't want to use lots of TwiML
<Play>
verbs in a row which might introduce pauses between words.我希望剪辑是私有资产,因此 function 可以以最小的延迟访问它们,并且连接需要非常快,所以我不想连续使用大量 TwiML
<Play>
动词,这可能会引入暂停词之间。
this appears to be a bug in the handling of binary files in the Twilio Serverless Toolkit .这似乎是Twilio Serverless Toolkit中处理二进制文件的错误。 I uploaded the same JavaScript to a Functions service and it ran successfully.
我将相同的 JavaScript 上传到 Functions 服务并成功运行。
The workaround for this is to deploy your function and test remotely, not locally.解决方法是部署您的 function 并远程测试,而不是本地测试。
This is obviously not ideal, can you raise an issue here (so that you can stay informed) and we will do our best to fix it.这显然不理想,您能否在这里提出问题(以便您随时了解情况),我们会尽力解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.