通过 webhook 从 whatsApp twilio 接收媒体消息

Receive media message from whatsApp twilio via webhook

我正在尝试从传入的 WhatsApp 消息中获取媒体文件,为此我尝试了 git Twilio 站点共享的示例 GITHUB

这是我的代码片段

//-----------------------------------------------------------------

[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";
    }
//----------------------------------------------------------------------

但它不起作用, 对于普通文本消息,它运行良好但对于媒体则不行,我通过发送 .jpg 文件进行了尝试。

我检查了调试器,但无法理解我遗漏了什么。 这是我收到的

sourceComponent "14100"
httpResponse "502"
url "https://myUrl.com/WAResponse/index"
ErrorCode "11200"
LogLevel "ERROR"
Msg "Bad Gateway"
EmailNotification "false"

如果我需要更改代码以接收媒体,请告诉我。 谢谢!

在详细了解 Twilio 支持后,发现当前代码没问题,我做了一些改动并使其异步,以便正常工作。

public async Task<TwiMLResult> Index(SmsRequest incomingMessage, int numMedia)

如果需要,您可能需要授予对该目录的访问权限