如何使用Azure Logic应用程序将使用Twilio发送的MMS中的图像提取到Azure Blob存储中?

时间:2017-12-01 21:19:16

标签: azure twilio azure-storage-blobs azure-logic-apps

my question on getting text messages into Azure 我能够将一条SMS消息传入Azure Table Store。

如果文本消息包含图像,因此已作为MMS发送,我想将图像下载到Azure Blob存储中。

Twilo有a guide on how to download and receive images

但我不知道如何使用Logic App

来使用它

我尝试了this Twilio guide并从GitHub克隆了代码。 令人困惑的是,DownloadMmsImages与BlockSpamCalls项目处于同一解决方案中。我不清楚这两个项目之间的关系。

我尝试添加Twilio GetMessage操作,但是我被提示放入消息SID的唯一属性是Body。

(稍微混淆)请求主体JSON模式

{
  "body": {
    "$content": "VG9Db3VudHJ etc",
    "$content-type": "application/json",
    "$formdata": [
      {
        "key": "ToCountry",
        "value": "AU"
      },
      {
        "key": "ToState",
        "value": ""
      },
      {
        "key": "SmsMessageSid",
        "value": "SMeda21902 etc"
      },
      {
        "key": "NumMedia",
        "value": "0"
      },
      {
        "key": "ToCity",
        "value": ""
      },
      {
        "key": "FromZip",
        "value": ""
      },
      {
        "key": "SmsSid",
        "value": "SMeda2 etc"
      },
      {
        "key": "FromState",
        "value": ""
      },
      {
        "key": "SmsStatus",
        "value": "received"
      },
      {
        "key": "FromCity",
        "value": ""
      },
      {
        "key": "Body",
        "value": "Tskez7"
      },
      {
        "key": "FromCountry",
        "value": "AU"
      },
      {
        "key": "To",
        "value": "+61 etc"
      },
      {
        "key": "ToZip",
        "value": ""
      },
      {
        "key": "NumSegments",
        "value": "1"
      },
      {
        "key": "MessageSid",
        "value": "SMeda2 etc"
      },
      {
        "key": "AccountSid",
        "value": "AC7aa etc"
      },
      {
        "key": "From",
        "value": "+61 etc"
      },
      {
        "key": "ApiVersion",
        "value": "2010-04-01"
      }
    ]
  },
  "headers": {
    "Accept": "*/*",
    "Cache-Control": "max-age=259200",
    "Connection": "close",
    "Content-Length": "381",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "etc.logic.azure.com:443",
    "User-Agent": "TwilioProxy/1.1",
    "X-Twilio-Signature": "sQf etc"
  }
}

1 个答案:

答案 0 :(得分:2)

免责声明如果此答案中的某些内容不起作用,我会提前道歉。我没有Twilio帐户,无法端到端地测试解决方案。请将其视为草案,以展示实现预期结果所需的主要步骤和关键细节。

获取消息SID

当您尝试检索邮件(或邮件的媒体)时,您确实需要提供邮件和/或媒体的SID / SIDs,这些邮件基本上都是标识符。我假设,这个Logic App实例正在接收SID作为传入HttpRequest触发器的一部分。

例如,您可能需要Logic App的使用者在HTTP GET请求的URL中提供消息SID,如下所示。

{
  "kind": "Http",
  "inputs": {
    "schema": {},
    "method": "GET",
    "relativePath": "message/{mms_message_sid}"
  }
}

HttpGet

然后,您将能够在Logic App的更多步骤中引用此SID。

{
  "inputs": {
    "host": {
      "connection": {
        "name": "@parameters('$connections')['twilio']['connectionId']"
      }
    },
    "method": "get",
    "path": "/Messages/@{encodeURIComponent(triggerOutputs()['relativePathParameters']['mms_message_sid'])}.json",
    "authentication": "@parameters('$authentication')"
  }
}

UseSid

正在检索消息

我简要地看了一下Twilio连接器,让我建议你不要使用它,因为它会使你的Logic App变得复杂很多

相反,您可以使用Get Media API通过Http连接器。您需要构建以下格式的网址:https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages/{mms_message_sid}/Media.json

retrive_media_list

然后,您应该会收到一条回复,其中包含media_list content_type消息uri以及可用于下载彩信实际内容的{ "first_page_uri":"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json?PageSize=50&Page=0", "media_list":[ { "sid":"ME85ebf7e12cb821f84b319340424dcb02", "account_sid":"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "parent_sid":"MM800f449d0399ed014aae2bcc0cc2f2ec", "content_type":"image/png", "date_created":"Wed, 25 Sep 2013 22:47:18 +0000", "date_updated":"Wed, 25 Sep 2013 22:47:19 +0000", "uri":"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media/ME85ebf7e12cb821f84b319340424dcb02.json" }, { "sid":"ME8d8f717e2d6e5383055b3cd150ac5f54", "account_sid":"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "parent_sid":"MM800f449d0399ed014aae2bcc0cc2f2ec", "content_type":"image/png", "date_created":"Wed, 25 Sep 2013 22:47:18 +0000", "date_updated":"Wed, 25 Sep 2013 22:47:19 +0000", "uri":"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media/ME8d8f717e2d6e5383055b3cd150ac5f54.json" } ], "previous_page_uri":null, "uri":"/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/MM800f449d0399ed014aae2bcc0cc2f2ec/Media.json?PageSize=50&Page=0", "page_size":50, "next_page_uri":null, "page":0 }

media_list

解析响应JSON

由于响应主体是一个表示为字符串的JSON文档,我们无法迭代body数组,除非我们跳过一些箍。

只需添加一个解析JSON步骤,该步骤使用上一步的HTTP{ "inputs": { "content": "@body('HTTP')", "schema": { "type": "object", "properties": { "first_page_uri": { "type": "string" }, "media_list": { "type": "array", "items": { "type": "object", "properties": { "sid": { "type": "string" }, "account_sid": { "type": "string" }, "parent_sid": { "type": "string" }, "content_type": { "type": "string" }, "date_created": { "type": "string" }, "date_updated": { "type": "string" }, "uri": { "type": "string" } }, "required": [ "sid", "account_sid", "parent_sid", "content_type", "date_created", "date_updated", "uri" ] } }, "previous_page_uri": {}, "uri": { "type": "string" }, "page_size": { "type": "number" }, "next_page_uri": {}, "page": { "type": "number" } } } } } )作为内容。对于架构,请提供以下文档:

media_list

parse_HTTP_response_body_json

通过`media_list`

迭代

现在事情相对容易了。您可以为上一步中的DASSessionDataPublisherImpl添加For For步骤。

对于每种媒体,我们需要进行实际检索其二进制内容的API调用。您可能希望指定与媒体类型相关的HTTP标头。玩弄它,看看它有用。

retrieve_media_content

保存到Azure Blob存储

我不会在此提供任何细节,因为您说过,您已经知道如何为SMS做到这一点。只需确保在HTTP2之后添加必要的步骤(内部 For Each步骤)。

希望,这有帮助。逻辑应用程序不是轻松的集成挤压"。