A sample bot and a custom client communicating to each other using the Direct Line API.
The minimum prerequisites to run this sample are:
- The latest update of Visual Studio 2015. You can download the community version here for free.
- The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
- Register your bot with the Microsoft Bot Framework. Please refer to this for the instructions. Once you complete the registration, update the Bot's Web.config file with the registered config values (Bot Id, MicrosoftAppId and MicrosoftAppPassword)
Credentials for the Direct Line API must be obtained from the Bot Framework developer portal, and will only allow the caller to connect to the bot for which they were generated. In the Bot Framework developer portal, enable Direct Line in the channels list and then, configure the Direct Line secret and update its value in the client's App.config file alongside with the Bot Id. Make sure that the checkbox for version 3.0 [PREVIEW] is checked. Refer to this for more information on how to configure channels.
Also, in order to be able to run and test this sample you must publish your bot, for example to Azure. Alternatively, you can use Ngrok to interact with your local bot in the cloud.
The Direct Line API is a simple REST API for connecting directly to a single bot. This API is intended for developers writing their own client applications, web chat controls, or mobile apps that will talk to their bot. The Direct Line v3.0 Nuget package simplifies access to the underlying REST API.
Each conversation on the Direct Line channel must be explicitly started using the DirectLineClient.Conversations.StartConversationAsync
.
Check out the client's Program.cs class which creates a new DirectLineClient
and starts a new conversation.
DirectLineClient client = new DirectLineClient(directLineSecret);
var conversation = await client.Conversations.StartConversationAsync();
User messages are sent to the Bot using the Direct Line Client Conversations.PostActivityAsync
method using the ConversationId
generated in the previous step.
while (true)
{
string input = Console.ReadLine().Trim();
if (input.ToLower() == "exit")
{
break;
}
else
{
if (input.Length > 0)
{
Activity userMessage = new Activity
{
From = new ChannelAccount(fromUser),
Text = input,
Type = ActivityTypes.Message
};
await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
}
}
}
Messages from the Bot are continually polled from the API in another Thread in the ReadBotMessagesAsync
method. Check out the Program.cs usage of GetActivitiesAsync
method which retrieves conversation messages newer than the stored watermark. Activities are then filtered to receive messages from the Bot only.
var activitySet = await client.Conversations.GetActivitiesAsync(conversationId, watermark);
watermark = activitySet?.Watermark;
var activities = from x in activitySet.Activities
where x.From.Id == botId
select x;
DirectLine v3.0 (unlike version 1.1) has support for Attachments (see Adding Attachments to a Message for more information about attachments). Check out the ReadBotMessagesAsync
method in Program.cs to see how the Attachments are retrieved and rendered appropriately based on their type.
if (activity.Attachments != null)
{
foreach (Attachment attachment in activity.Attachments)
{
switch (attachment.ContentType)
{
case "application/vnd.microsoft.card.hero":
RenderHeroCard(attachment);
break;
case "image/png":
Console.WriteLine($"Opening the requested image '{attachment.ContentUrl}'");
Process.Start(attachment.ContentUrl);
break;
}
}
}
To run the sample, you'll need to run both Bot and Client apps.
- Running Bot app
- In the Visual Studio Solution Explorer window, right click on the DirectLineBot project.
- In the contextual menu, select Debug, then Start New Instance and wait for the Web application to start.
- Running Client app
- In the Visual Studio Solution Explorer window, right click on the DirectLineSampleClient project.
- In the contextual menu, select Debug, then Start New Instance and wait for the Console application to start.
To test the Attachments type show me a hero card
or send me a botframework image
and you should see the following outcome.
To get more information about how to get started in Bot Builder for .NET and Conversations please review the following resources: