Chat Bot with Xamarin.Forms

You can create a Bot, with Microsoft Bot Framework, and talk to it via a number of channels, such as Slack or Facebook Messenger. However, what if you want to talk to a bot, directly via your Xamarin.Forms application.

Setup Bot

Before we get started, you need to setup your Bot, via the Microsoft Bot Framework. There are 2 options you can play with here. You can set it up on the Azure Bot Service, and use the DirectLine channel.

The secondary option, which is what I will be using today, just for demonstration purposes, is hosting my own WebAPI, with the Microsoft Bot Framework, and using it via localhost. If you want to see how to set this up, please look at my Introduction to Microsoft Bot Framework.

Communicating with the Bot

As explained in my Introduction to Microsoft Bot Framework, you send activities to the Bot, and it returns an activity back.

Activities, will contain information such as the type of message, message, time stamps, recipients and more. There are two ways to communicate with the Bot. First is use the Nuget Package

Install-Package Microsoft.Bot.Connector

Second, is to custom write, your HTTP REST calls. Obviously using a Microsoft build SDK, would be preferable, but I am going on a deeper dive to see the calls been made.

Activity

There are different types of activities, that include ConversationUpdate, Message, DeleteUserData, Ping, etc. I went through these in the introduction post. Here is a partial example of what an Activity may contain.

public class Activity
{ 
    public string Text { get; set; }
    public string Type { get; set; }
    public string ChannelId { get; set; }
    public string Id { get; set; }
    public List<Member> MembersAdded { get; set; }
    public string Timestamp { get; set; }
    public string LocalTimestamp { get; set; }
    public Member Recipient { get; set; }
    public Conversation Conversation { get; set; }
    public Member From { get; set; }
    public string ServiceUrl { get; set; }
}

The ChannelId is the identifier of the Channel you are using to connect to the Bot, and the Conversation, contains an Id, that defines which conversation you are having with the Bot. Text is the message you are sending or receiving, and the Type is a string that defines what type of activity this is. The other properties are self explanatory.

Normally when you start a conversation, you will send a few ConversationUpdates to notify the Bot of who you are and to establish a conversation.

Then you POST an Activity, similar to this below.

var activity = new Activity()
{
    Text = message,
    Type = ActivityTypes.Message,
    ChannelId = "EmulatorTestChannelId",
    Id = Guid.NewGuid().ToString(),
    Recipient = new Member() { Id = "781ajk076fln20n86", Name = "Bot" },
    From = new Member() { Id = "default-user", Name = "User" },
    Timestamp = DateTime.UtcNow.ToString("o"),
    LocalTimestamp = DateTime.Now.ToString("o"),
    Conversation = new Conversation() { Id = "3kcmig40nn8hiiad3" },
    ServiceUrl = "https://localhost:44338"
};

The response will come back as another activity. You use the ActivityType to determine what it is, then you can choose to display messages such as these below.

Learn More

I have created a sample project XamarinChatBot, that shows a working example. Remember that if you are communicating with a local bot, remove the authentication, and it must go to localhost. Otherwise you will need to register your bot and the url you are using to connect to it. While this example gives you a look at the low level of what is going on, I would recommend you use the SDK provided by Microsoft and deploy using the Azure Bot Service for any production level application. You will then use the DirectLine channel to communicate from your Xamarin.Forms application.


XAMARIN.FORMS MONTHLY NEWSLETTER

JOIN 1,000+ SUBSCRIBERS

  1. Don't miss out on updates
  2. The latest info from this site, Xamarin and the community
  3. Unsubscribe at any time*

* We use MailChimp, with double opt-in, and instant unsubscribe

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer | Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

One Comment

Leave A Comment?