Azure ボットから添付ファイルをダウンロードする

以下の手順で、オムニチャネルの Azure ボットから添付ファイルをダウンロードします。

  1. ボットの Microsoft App ID とクライアント シークレットを入力して、ボット用のトークンを取得します

  2. 添付ファイルの URL から attachmentId を取得します。

    たとえば URL が https://us-api.asm.skype.com/v1/objects/0-eus-d1-5360689c55c308cb4e3b51722e46b801/ の場合、attachmentId0-eus-d1-5360689c55c308cb4e3b51722e46b801 です。

  3. RequestUri 変数に attachmentId を挿入し、次のように GET 要求で RequestUri を使用します。

    string requestUri = $"https://botapi.skype.com/amer/v3/attachments/{attachmentId}/views/original";
    var httpRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
    
    var authorization = new AuthenticationHeaderValue("bearer", <add the botToken here>);
    var requestHeaders = new Dictionary<string, string>()
      {
         { "Authorization", authorization.ToString() }
      };
    
    foreach (var header in requestHeaders)
      {
          httpRequest.Headers.Add(header.Key, header.Value);
      }
    
    HttpResponseMessage response = await client.SendAsync(httpRequest);
    

添付ファイルを管理する

注意

このセクションの情報は、Government Community Cloud (GCC) にのみ適用されます。

このセクションでは、オムニチャネル ボット サービス メッセージング プラットフォームでファイル添付ファイルを管理する方法について説明します。

まず、オムニチャネル ボット サービス チャネルのファイル添付形式を簡単に確認してみましょう。

添付ファイルの形式

ただし、オムニチャネル ボット サービスチャネルで Customer Service 用オムニチャネルから Azure ボットにファイル添付が送信される場合、Activity.ChannelData プロパティの amsReferences および amsMetadata フィールドにファイルのダウンロードに必要な情報が渡されます。

オムニチャネル ボット サービス チャネル

{
   "recipient":{
      "id":"8:acs:5ecf37b1-11 Oc-414g-ab33-804ffd6b4a33_eooe0010-7c57-1ceb-nec-113aOdOOb272",
      "name":"Omnichannel-test-bot",
      "aadObjectId":null,
      "role":null
   },
   "attachments ":null,
   "channelData":{
      "tags":"Channelld-lcw,FromCustomer",
      "deliveryMode":"bridged",
      "fromUserId":"8:acs:5ecf37b1-110c-4149-ab33-804ffd6b4a33_00000010-61 b9-ab1 d-3dfe-9c3aOd009ea4",
      "amsReferences":[
         "0-wus-d6-20e7797d208fab388cc11b09674d166"
      ],
      "amsMetadata":[
         {
            "contentType":"image/png",
            "fileName":"SurnmerTime.png"
         }
      ],
      "sourceChannelId":"omnichannel"
   }
}

Azure ボット コードで添付ファイルを管理する方法

添付ファイル情報は、オムニチャネル ボット サービス チャネルで渡され、次の例に示すように、ボット コードでアクセスできます。

// 1. Retrieve Attachment ID from ChannelData["amsReferences"]
if (turnContext.Activity.ChannelData != null &&
    turnContext.Activity.ChannelData is JObject incomingRequestChannelData &&
    incomingRequestChannelData.TryGetValue("amsReferences", out JToken amsReferencesArray))
{
    string attachmentId = JsonConvert.DeserializeObject<string[]>(amsReferencesArray.ToString()).FirstOrDefault();

    // 2. Build HTTP request for specified attachment ID.
    string requestUri = $"https://botapi.skype.com/amer/v3/attachments/{attachmentId}/views/original";
    var httpRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);

    // 3. Acquire authentication token and add it to request headers
    var token = await new MicrosoftAppCredentials("botAppId", "botAppSecret").GetTokenAsync();
    var authorization = new AuthenticationHeaderValue("bearer", token);
    httpRequest.Headers.Add("Authorization", authorization.ToString());

    // 4. Add Azure Communication Services Bot ID to request header. This is required to achieve good download performance.
    httpRequest.Headers.Add("BotAcsId", turnContext.Activity.Recipient.Id);

    // 5. Use HttpClient to execute the request and download attachment
    var response = await client.SendAsync(httpRequest);
    
    // 6. Save HTTP response stream to the file
    var responseContentStream = await response.Content.ReadAsStreamAsync();
    using (FileStream fileCreateStream = new FileStream("file path", FileMode.Create))
    {
        fileCreateStream.CopyTo(responseContentStream);
    }
}

参照

チャネルによるカードのサポート
ライブ チャットと非同期チャネルのサポート
新しいオムニチャネル メッセージング プラットフォームに向けた移行の変更