A sample bot and a custom client communicating to each other using the Direct Line API.
[![Deploy to Azure][Deploy Button]][Deploy DirectLine/CSharp] [Deploy Button]: https://azuredeploy.net/deploybutton.png [Deploy DirectLine/CSharp]: https://azuredeploy.net
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. 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 Nuget package simplifies access to the underlying REST API.
Each conversation on the Direct Line channel must be explicitly started using the DirectLineClient.Conversations.NewConversationAsync
.
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.NewConversationAsync();
User messages are sent to the Bot using the Direct Line Client Conversations.PostMessageAsync
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)
{
Message userMessage = new Message
{
FromProperty = fromUser,
Text = input
};
await client.Conversations.PostMessageAsync(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 GetMessagesAsync
method which retrieves conversation messages newer than the stored watermark. Messages are then filtered to receive messages from the Bot only.
var messages = await client.Conversations.GetMessagesAsync(conversationId, watermark);
watermark = messages?.Watermark;
var messagesFromBotText = from x in messages.Messages
where x.FromProperty == botId
select x;
The ChannelData property provides a way for you to send native metadata to take advantage of special features or concepts for a channel (see Custom Channel Capabilities for more information.) Check out the ReadBotMessagesAsync
method in Program.cs to where the ChannelData custom content is deserialized and render appropriately.
if (message.ChannelData != null)
{
var channelData = JsonConvert.DeserializeObject<DirectLineChannelData>(message.ChannelData.ToString());
switch (channelData.ContentType)
{
case "application/vnd.microsoft.card.hero":
RenderHeroCard(channelData);
break;
case "image/png":
Console.WriteLine($"Opening the requested image '{channelData.ContentUrl}'");
Process.Start(channelData.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 ChannelData custom messages 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: