[英]Receive media message from whatsApp twilio via webhook
I'm trying to get media file from incoming WhatsApp message, for that I tried git example shared by Twilio site GITHUB我正在尝试从传入的 WhatsApp 消息中获取媒体文件,为此我尝试了 Twilio 站点GITHUB共享的 git 示例
Here is my code snip这是我的代码片段
//-----------------------------------------------------------------
[HttpPost]
public TwiMLResult Index(SmsRequest incomingMessage, int numMedia)
{
MessagingResponse messagingResponse = new MessagingResponse();
if (numMedia>0)
{
GetMediaFilesAsync(numMedia,incomingMessage).GetAwaiter().GetResult();
messagingResponse.Append(new Twilio.TwiML.Messaging.Message().Body("Media received"));
return TwiML(messagingResponse);
}
// first authorize incoming message
TwilioClient.Init(accountSid, authToken);
messagingResponse = GetResponseMsg(incomingMessage);
return TwiML(messagingResponse);
}
private async Task GetMediaFilesAsync(int numMedia, SmsRequest incomingMessage)
{
try
{
for (var i = 0; i < numMedia; i++)
{
var mediaUrl = Request.Form[$"MediaUrl{i}"];
Trace.WriteLine(mediaUrl);
var contentType = Request.Form[$"MediaContentType{i}"];
var filePath = GetMediaFileName(mediaUrl, contentType);
await DownloadUrlToFileAsync(mediaUrl, filePath);
}
}
catch (Exception ex)
{
}
}
private string GetMediaFileName(string mediaUrl,string contentType)
{
string SavePath = "~/App_Data/";
return Server.MapPath(
// e.g. ~/App_Data/MExxxx.jpg
SavePath +
System.IO.Path.GetFileName(mediaUrl) +
GetDefaultExtension(contentType)
);
}
private static async Task DownloadUrlToFileAsync(string mediaUrl,string filePath)
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(mediaUrl);
var httpStream = await response.Content.ReadAsStreamAsync();
using (var fileStream = System.IO.File.Create(filePath))
{
await httpStream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
}
public static string GetDefaultExtension(string mimeType)
{
// NOTE: This implementation is Windows specific (uses Registry)
var key = Registry.ClassesRoot.OpenSubKey(
@"MIME\Database\Content Type\" + mimeType, false);
var ext = key?.GetValue("Extension", null)?.ToString();
return ext ?? "application/octet-stream";
}
//----------------------------------------------------------------------
but it's not working, for normal text message its working well but not for media, I tried it by sending a .jpg file.但它不起作用,对于普通短信,它运行良好,但不适用于媒体,我通过发送 .jpg 文件进行了尝试。
I checked debugger, but unable to understand what I missed.我检查了调试器,但无法理解我错过了什么。 this is what I receive这就是我收到的
sourceComponent "14100"
httpResponse "502"
url "https://myUrl.com/WAResponse/index"
ErrorCode "11200"
LogLevel "ERROR"
Msg "Bad Gateway"
EmailNotification "false"
Please let me know if I need to perform changes in my code to receive the media.如果我需要更改代码以接收媒体,请告诉我。 Thank you!谢谢!
After detailing from Twilio support, found that the current code is fine, I made a little change and made it async so its work.从 Twilio 支持部门详细了解后,发现当前代码很好,我做了一些更改并使其异步,以便其工作。
public async Task<TwiMLResult> Index(SmsRequest incomingMessage, int numMedia)
You may need to grant access permission to the directory if required如果需要,您可能需要授予对目录的访问权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.