【问题标题】:Receive media message from whatsApp twilio via webhook通过 webhook 从 WhatsApp twilio 接收媒体消息
【发布时间】:2020-12-22 20:14:13
【问题描述】:

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

这是我的代码片段

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

[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"

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

【问题讨论】:

    标签: c# asp.net-mvc twilio whatsapp


    【解决方案1】:

    从 Twilio 支持详细了解后,发现当前代码没问题,我做了一点改动并使其异步,这样它就可以工作了。

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2023-01-24
      • 1970-01-01
      • 2022-12-11
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多