Bot conversation through Direct Line API-

Direct Line is a REST API, intended to converse with a Bot either through a custom chat Application such as a web App, mobile app, web chat control and other service to service applications that will connect to a single bot.

The Direct Line is a channel , which acts like a single user context interface for multiple clients to converse with a bot.

Envisioning a Social networking among Bots wherein they communicate through Direct Line API’s. Each Direct Line API is able to converse with a BOT which it is registered for, but since Direct Line API’s can be called as REST service, they can be a pattern of interlinking services and Direct Line API’s of specific Bots can be invoked which can solve the purpose.

Using a Direct Line API we can do the following:-

a) Send messages from client to Bot through HTTP POST message
b) Receive messages by Polling through HTTP GET

Direct Line Authentication:
Direct Line allows you to authenticate all calls with either a secret (retrieved from the Direct Line channel configuration page) or a token (which you may get at runtime by converting your secret). A Direct Line secret is a master key that can access any conversation, and create tokens. Secrets do not expire. A Direct Line token is a key for a single conversation. It expires but can be refreshed.

A service-to-service application, using the secret is simplest and used in the Sample Direct Line call below.

Nuget to be installed: Microsoft.Bot.Connector.DirectLine
Consider a DirectLineInput Model class as below

The message is a string which is the conversation content passed to the Bot, ConversationId is unique pre conversation- is null/empty if it’s a new conversation, fromName is name of the control/app which has started the conversation.
PS: Leaving the fromName field as empty is not suggested.

Reusable class Direct Line Helper:

Explaining the code above:

  • Establish connection to the Direct Line channel in the contructor, so the communication channel is opened once
  • Next time on the Conversation Id is checked , if null then a new conversation is started
  • If conversation id exists then the messages are posted or brought from the same conversation
  • To note, direct Line fetches all messages of the conversation stored on the server upto max 32 KB.
  • It is the clients resposnsibility to save previous conversations and read only the last sent message from the Bots reply.
    The supporting models:

The Output of the method PostMessageThroughDirectLine is “MessageSet” which is a Bot Reply.
We can parse the text content, attachments etc. from Bot Message as show the result on the web app

Looking forward to building samples of internetworking Bots