'Sending audio message with Twilio Conversations
I am trying to send audio message with twillio conversations. According to the documentation I should send a contentType and a media of type string or Buffer. The Buffer constructor is missing in JS so I used the following method to create a buffer: Here is my send message request (I have tried sending different content types. And even audio in base64. Nothing worked.):
function toBuffer(ab: any) {
const buf = Buffer.alloc(ab.byteLength)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; i += 1) {
buf[i] = view[i]
}
return buf
}
const arrayBuffer = await new FileReader().readAsArrayBuffer(new Blob([message.audio]))
chats[chat].conversation?.sendMessage({
contentType: 'audio/wav',
media: toBuffer(arrayBuffer)
}, {
messageId: uuidv4(),
...attributes
})
I request the audio URL with the function provided by twillio conversations:
message.media.getContentTemporaryUrl()
It returns a URL which doe not contain a valid audio file and cannot be played. Please help me to find the propper way of send a valid audo message and being able to play it. Thanks
Solution 1:[1]
It looks as though you are working with the client side JS library, so using Node features like Buffers won't work.
The media documentation for the JS SDK says:
For JavaScript, you can provide the following as the source for the new media message sent by a Chat-based Conversation Participant:
- A new FormData object containing file information: filename, content-type, size, and all FormData-required information
So you could do it like:
const file = document.getElementById("#formInputFile").files[0];
const formData = new FormData();
formData.append('file', file]);
formData.append('contentType', 'audio/wav')'
chats[chat].conversation?.sendMessage(formData);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | philnash |