Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update doc about token and samples #2222

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,25 @@ To connect to the bot you have built with Microsoft Copilot Studio, you'll need

### Get Direct Line token

To start a conversation with your Microsoft Copilot Studio 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 request a Direct Line token, issue a GET request to the endpoint below:

```rest-api
GET /api/botmanagement/v1/directline/directlinetoken
```

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

To start a conversation with your bot, you need a Direct Line token. This can be obtained by making a GET request to the endpoint indicated within the Copilot Studio screen. This token must then be used as the header for subsequent calls to the directline API.

Example:

```rest-api
GET <BOT TOKEN ENDPOINT>
```

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

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

#### Sample code example

Expand All @@ -118,40 +117,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