Skip to content

Commit

Permalink
Update doc about token and samples
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoZama committed Oct 25, 2023
1 parent b3016ab commit f185709
Showing 1 changed file with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,25 @@ To connect to the bot you have built with Power Virtual Agents, you'll need to r

### Get Direct Line token

To start a conversation with your Power Virtual Agents bot, you need a Direct Line token. You need to add code that retrieves a Direct Line token with the Token Endpoint from the previous section to your app.
To start a conversation with your Power Virtual Agents bot, you need a Direct Line token. This can be obtained by making a GET request to the endpoint indicated within the PVA screen. This token must then be used as the header for subsequent calls to the directline API.

To request a Direct Line token, issue a GET request to the endpoint below:
Example:

```rest-api
GET /api/botmanagement/v1/directline/directlinetoken
GET <BOT TOKEN ENDPOINT>
```

| Query Parameter | Required |
| --------------- | -------- |
| `TokenEndPoint` | yes |


If the request is successful, will be returned a Direct Line token, expiration time and a conversationId for the requested bot.
Example:

```rest-api
GET <BOT TOKEN ENDPOINT>
```Json
{
"token": "RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0y8qbOF5xPGfiCpg4Fv0y8qqbOF5x8qbOF5xn",
"expires_in": 3600,
"conversationId": "abc123"
}
```

If the request is successful, a Direct Line token will be returned for the requested bot.

#### Sample code example

The following example uses samples from the [Connector sample code](https://github.com/microsoft/PowerVirtualAgentsSamples/tree/master/BotConnectorApp) to get a Direct Line token for a Power Virtual Agents bot.
Expand All @@ -116,40 +114,39 @@ The following example uses samples from the [Connector sample code](https://gith
/// Get directline token for connecting bot
/// </summary>
/// <returns>directline token as string</returns>
public async Task<string> GetTokenAsync()
public async Task<DirectLineToken> GetTokenAsync(string url)
{
string token;
using (var httpRequest = new HttpRequestMessage())
{
httpRequest.Method = HttpMethod.Get;
UriBuilder uriBuilder = new UriBuilder(TokenEndPoint);
httpRequest.RequestUri = uriBuilder.Uri;
using (var response = await s_httpClient.SendAsync(httpRequest))
{
var responseString = await response.Content.ReadAsStringAsync();
token = SafeJsonConvert.DeserializeObject<DirectLineToken>(responseString).Token;
}
}
return token;
try
{
return await _httpClient.GetFromJsonAsync<DirectLineToken>(url);
}
catch (HttpRequestException ex)
{
throw ex;
}
}
```

```C#
/// <summary>
/// class for serialization/deserialization DirectLineToken
/// </summary>
public class DirectLineToken
{
public string Token { get; set; }
}
public class DirectLineToken
{
public string Token { get; set; }
public int Expires_in { get; set; }
public string ConversationId { get; set; }
}
```

The response will be:
The response object will be the same as the GET request we saw earlier

```json
{
"token": "<token>"
}
{
"token": "RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0y8qbOF5xPGfiCpg4Fv0y8qqbOF5x8qbOF5xn",
"expires_in": 3600,
"conversationId": "abc123"
}
```

### Use Direct Line to communicate with the bot
Expand Down

0 comments on commit f185709

Please sign in to comment.