Condividi tramite


Scaricare gli allegati di file da un bot di Azure

Questo articolo descrive come scaricare gli allegati di file da un bot di Azure usato con la messaggistica in Dynamics 365 Customer Service.

Per scaricare gli allegati da un bot di Azure, seguire questa procedura:

  1. Ottieni il token per il tuo bot utilizzando l'ID app Microsoft del tuo bot e il client secret.

  2. Ottieni attachmentId dall'array amsReferences nei dati del canale dell'attività.

    Ad esempio, se il amsReferences contiene ["0-eus-d1-5360689c55c308cb4e3b51722e46b801"], allora il attachmentId è 0-eus-d1-5360689c55c308cb4e3b51722e46b801.

  3. Inserire in attachmentId una RequestUri variabile e quindi usare RequestUri in una GET richiesta, come illustrato di seguito:

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>);
httpRequest.Headers.Authorization = authorization;
httpRequest.Headers.Add("BotAcsId", activity.Recipient.Id);

HttpResponseMessage response = await client.SendAsync(httpRequest);

Gestire gli allegati di file

Annotazioni

Le informazioni contenute in questa sezione sono applicabili solo a Government Community Cloud (GCC).

Questa sezione descrive come gestire gli allegati di file nel canale di messaggistica.

Prima di tutto, esaminare i formati degli allegati di file nel canale di messaggistica.

Formati di file allegati

Quando gli allegati di file vengono inviati da Dynamics 365 Contact Center all'agente di Azure nel canale di messaggistica, le informazioni necessarie per scaricare i file vengono passate nei campi amsReferences e amsMetadata della proprietà Activity.ChannelData.

Canale di messaggistica

{
   "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"
   }
}

Come gestire gli allegati di file nel codice dell'agente Azure

Le informazioni sugli allegati vengono trasmesse nel canale del servizio Omnichannel e possono essere accessibili nel codice dell'agente, come illustrato nell'esempio seguente.

// 1. Retrieve Attachment ID from ChannelData["amsReferences"]
if (activity.ChannelData != null &&
    activity.ChannelData is Dictionary<string, JsonElement> channelData &&
    channelData.TryGetValue("amsReferences", out var amsReferencesElement))
{
    var amsReferencesString = amsReferencesElement.GetString() ?? amsReferencesElement.ToString();
    string attachmentId = JsonConvert.DeserializeObject<string[]>(amsReferencesString).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
    // Option A: Using IConnections (recommended)
    var connection = connections.GetConnection("ServiceConnection");
    var token = await connection.GetAccessTokenAsync(
        "https://api.botframework.com",
        new[] { "https://api.botframework.com/.default" },
        forceRefresh: false);

    // Option B: Using OAuth client credentials flow
    // var tokenRequest = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token")
    // {
    //     Content = new FormUrlEncodedContent(new Dictionary<string, string>
    //     {
    //         { "grant_type", "client_credentials" },
    //         { "client_id", botAppId },
    //         { "client_secret", botAppSecret },
    //         { "scope", "https://api.botframework.com/.default" }
    //     })
    // };
    // var tokenResponse = await client.SendAsync(tokenRequest);
    // var tokenContent = await tokenResponse.Content.ReadAsStringAsync();
    // var token = JsonConvert.DeserializeObject<dynamic>(tokenContent).access_token;

    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", 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))
    {
        await responseContentStream.CopyToAsync(fileCreateStream);
    }
}

Supporto della carta per canale
Supporto per chat live e canali asincroni