-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Receiving and processing audio type messages #45
Comments
Hi Alex, I have implemented sending audio through using media id and a URL link. For the webhook part to receive audio messages I have not yet implemented a concrete class for audio messages. The JSON below shows how the image message is received through the webhook, I guess for audio type will have its corresponding properties. To process the audio message if the audio properties contain a value of either the media id or link then you can process the value to get the download link from WhatsApp which you can use to download the audio message received. Downloading media documentation {
"object": "whatsapp_business_account",
"entry": [{
"id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
"changes": [{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": PHONE_NUMBER,
"phone_number_id": PHONE_NUMBER_ID
},
"contacts": [{
"profile": {
"name": "NAME"
},
"wa_id": "WHATSAPP_ID"
}],
"messages": [{
"from": PHONE_NUMBER,
"id": "wamid.ID",
"timestamp": TIMESTAMP,
"type": "image", // I guess this key changes the message type property to audio
"image": {
"caption": "CAPTION",
"mime_type": "image/jpeg",
"sha256": "IMAGE_HASH",
"id": "ID"
}
}]
},
"field": "messages"
}]
}]
} |
Correct! It's very similar to the image use case.
But, I can't reproduce the audio locally. I think it's probably something like the audio encoding or even something more fundamental that I might be missing. It's an ogg audio file (codec opus) I'll let you know if I manage to solve it. Regards, |
You can try with a different audio codec to see if it works. (only opus codecs, base audio/ogg is not supported) I think the base ogg audio file is not supported by cloud api. |
I will test with a sample audio ogg file to see if I'm able to download it from the webhook then I will let you know. |
Thank you! |
Annoyingly, I've just got it working in PowerShell PowerShell. This works well
This is what I'm trying in my PoC
|
I found the problem. I was missing the "User Agent" from the headers. You just need to added this to make it work
|
I have also added the implementation for downloading media using the media url and testing it out to see if it works. |
It works perfectly with the user agent. I have added the implementation to download media from the media url generated by whatsapp if (messageType.Equals("audio"))
{
var audioMessageReceived = JsonConvert.DeserializeObject<AudioMessageReceived>(Convert.ToString(messageReceived)) as AudioMessageReceived;
audioMessage = new List<AudioMessage>(audioMessageReceived.Entry.SelectMany(x => x.Changes).SelectMany(x => x.Value.Messages));
_logger.LogInformation(JsonConvert.SerializeObject(audioMessage, Formatting.Indented));
var mediaUrlResponse = await _whatsAppBusinessClient.GetMediaUrlAsync(audioMessage.SingleOrDefault().Audio.Id);
_logger.LogInformation(mediaUrlResponse.Url);
// To download media received sent by user
var mediaFileDownloaded = await _whatsAppBusinessClient.DownloadMediaAsync(mediaUrlResponse.Url);
var rootPath = Path.Combine(_webHostEnvironment.WebRootPath, "Application_Files\\MediaDownloads\\");
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
// Get the path of filename
string filename = string.Empty;
if (mediaUrlResponse.MimeType.Contains("audio/mpeg"))
{
filename = $"{mediaUrlResponse.Id}.mp3";
}
if (mediaUrlResponse.MimeType.Contains("audio/ogg"))
{
filename = $"{mediaUrlResponse.Id}.ogg";
}
var filePath = Path.Combine(_webHostEnvironment.WebRootPath, "Application_Files\\MediaDownloads\\", filename);
await System.IO.File.WriteAllBytesAsync(filePath, mediaFileDownloaded);
return Ok(new
{
Message = "Audio Message received"
});
} |
Hi,
Thank you so much for all your work in facilitating the use of the WhatsApp Business Cloud APIs.
I've been trying to create a WhatsApp bot to transcribe voice messages, and I'm struggling to download the audio.
I've downloaded and been able to reproduce voice messages through Postman, but I can't make it work in C#. I looked at your project and couldn't find the implementation to receive and process an audio message.
Is this something you are planning to implement, or did I miss it?
Thanks again
Regards,
Alex
The text was updated successfully, but these errors were encountered: