Create .NET Client to Connect to Direct Line App Service extension
Commencing September 1, 2023, it is strongly advised to employ the Azure Service Tag method for network isolation. The utilization of DL-ASE should be limited to highly specific scenarios. Prior to implementing this solution in a production environment, we kindly recommend consulting your support team for guidance.
APPLIES TO: SDK v4
This article describes how to create a .NET client in C# which connects to the Direct Line App Service extension. Please, also read this companion article Configure .NET bot for extension.
Prerequisites
- An Azure account.
- A bot deployed to the Azure portal.
Get the Direct Line secret key
- In your browser, go to the Azure portal.
- In the Azure portal, locate your Azure Bot resource.
- Select Channels under Settings.
- If it isn't already enabled, select the Direct Line channel to enable it.
- Select Direct Line from Channels after enabling it.
- Go to the Sites section. There is typically a Default_Site unless you've deleted or renamed it.
- Select the Show link button (eye icon) to reveal one of the keys; then copy and save its value. You'll use this value in the section Create a C# Direct Line client.
Note
This value is your Direct Line client secret used to connect to Direct Line App Service extension. You can create additional sites if you'd like and use those secret values as well.
Create a C# Direct Line client
Interactions with the Direct Line App Service extension happen differently than traditional Direct Line because most communication happens over a WebSocket. The updated Direct Line client includes helper classes for opening and closing a WebSocket, sending commands through the WebSocket, and receiving Activities back from the bot. This section describes how to create a simple C# client to interact with a bot.
In Visual Studio, create a new .NET Core console application project.
Clone the Direct Line client from GitHub repository and include it in your project.
Create a client and generate a token using a secret. This step is the same as building any other C# Direct Line client except the endpoint you need use in your bot, appended with the .bot/ path as shown next. Do not forget the ending /.
string endpoint = "https://<your_bot_name>.azurewebsites.net/.bot/"; string secret = "<your_bot_direct_line_secret_key>"; var tokenClient = new DirectLineClient( new Uri(endpoint), new DirectLineClientCredentials(secret)); var conversation = await tokenClient.Tokens.GenerateTokenForNewConversationAsync();
Notice the following:
- The endpoint value is the bot URL you obtained when you deployed the bot to Azure. For more information, see Configure .NET bot for extension.
- The secret value shown as YOUR_BOT_SECRET is the value you saved earlier from the sites section.
Once you have a conversation reference from generating a token, you can use this conversation ID to open a WebSocket with the new
StreamingConversations
property on theDirectLineClient
. To do this you need to create a callback that will be invoked when the bot wants to sendActivitySets
to the client:public static void ReceiveActivities(ActivitySet activitySet) { if (activitySet != null) { foreach (var a in activitySet.Activities) { if (a.Type == ActivityTypes.Message && a.From.Id.Contains("bot")) { Console.WriteLine($"<Bot>: {a.Text}"); } } } }
Now you're ready to open the WebSocket on the
StreamingConversations
property using the conversation's token,conversationId
, and yourReceiveActivities
callback:var client = new DirectLineClient( new Uri(endpoint), new DirectLineClientCredentials(conversation.Token)); await client.StreamingConversations.ConnectAsync( conversation.ConversationId, ReceiveActivities);
The client can now be used to start a conversation and send
Activities
to the bot:var startConversation = await client.StreamingConversations.StartConversationAsync(); var from = new ChannelAccount() { Id = "123", Name = "Fred" }; var message = Console.ReadLine(); while (message != "end") { try { var response = await client.StreamingConversations.PostActivityAsync( startConversation.ConversationId, new Activity() { Type = "message", Text = message, From = from }); } catch (OperationException ex) { Console.WriteLine( $"OperationException when calling PostActivityAsync: ({ex.StatusCode})"); } message = Console.ReadLine(); }