Building My Own Personal Bot

“What Would Lilian Do?” The moment I found out about the new bot framework Microsoft was releasing, I knew I had to build a WWLD bot. Every time someone asks me to fix their computer, WWLD? Every time someone asks me to about my opinion on something, WWLD? Every time someone wants my advice, WWLD?

This is a perfect solution! No more will the days be full of questions, but full of answers. So it was time to get to work. And so my plan evolved:

Step 1. Make a LUIS model
Step 2. Make a bot
Step 3. ???
Step 4. Profit

Checklist of what I needed:

  • LUIS Model
  • Visual Studio
  • Windows 10
  • Bot Template + SDK

It Started with LUIS

After reading through the documentation and reading the code in the sample apps, I had a “good enough” understanding of how this whole Bot Framework thing worked and started to build my bot.

I started with LUIS (Language Understanding Intelligent Service) and built a model for the user intents I wanted to look out for, this was all based on 6 topics that I had prepared 3-4 responses for:

  • Greeting
  • Colour
  • Advice
  • Work
  • Free/Busy
  • Technical Questions

I then trained the model on what the intent of the above 6 topics looked like, for example:

  • Hey; Hi; Hello; Yo = Greeting
  • What’s your favourite colour; Fav colour; Colour? = Colour (I even trained it with color just for you Americans!)
  • What is life; Help me! = Advice
  • How’s work; What are you up to right now; What are you working on? = Work
  • Are you free; Are you busy; Busy?; Free? = Free/Busy
  • How do I …; What does …; What is … = Technical Question

Pretty simple, but I didn’t want to spend too much time on this as I was still figuring this whole thing out. I think the above, along with several other phrases, were enough to train my model to differentiate between the topics I had written sample responses for.

Dialog and LUISIntent

Next I started writing the dialogs for this, and essentially this was based off just one model: Lilian.cs. This just had a dictionary for each topic along with my 3-4 sample responses for each topic. So every time the LUIS model thought one of the 6 topics was being discussed by the user, it would go to the corresponding dialog method.

Example from my “work dictionary”:

public Dictionary<int, string> workDictionary = new Dictionary<int, string> workDictionary.Add(0, "The same thing I do every night, try to take over the world.");

The dialog method was simple, it generated a random number between 0 – n and used that number to pick a sample response from that specific topic’s dictionary. And that was it. Seriously, that’s all the bot does.

[LuisIntent("work")]
public async Task TalkAboutWork(IDialogContext context, LuisResult result)
{
   var advice = RandomDictionaryValue(wwld.workDictionary);
   await context.PostAsync(advice.ToString());
   context.Wait(MessageReceived);
}

Code Explained:

This method gets called if the LuisIntent recognised is “work”; “work” is the exact name of my work intent inside the LUIS model, which recognises when the user’s intent is to ask about my work.

var advice is a string containing a response from my “work” dictionary. The RandomDictionaryValue method generates a random number between 0 – n (n being the size of the dictionary), and uses the dictionary provided in the parameters of this method to get a random string from that dictionary.

The last two lines of code send the message (the “advice” string variable) back to the user and then waits for a message from the user. This is a very basic dialog using LUIS, here’s more on using the LUIS service in your bot:

http://docs.botframework.com/en-us/node/builder/chat/IntentDialog/

I decided to a have a little fun with one of the dialogs, the technical questions dialog. Rather than just picking from the generic responses in my dictionary, I added an extra “Bing That” functionality. All this did was take the user’s message, URL encode it, then add it as a query to the following URL:

http://letmebingthatforyou.com/?q=USERSMESSAGE

So randomly, every now and then, my bot would provide the user with a let me Bing that for you URL, it was glorious.

static string BingThat(string message)
{
   var query = HttpUtility.UrlEncode(message);
   var url = "http://letmebingthatforyou.com/?q=" + query;
   return url;
}

Registering

So I had my LUIS model, and I had written a method for every intent in that LUIS model. What now? It was time to register it!

I registered my bot and hooked it up to Telegram, this took less than 10 minutes to do:

  1. Deploy the bot to Azure (or any other web hosting service)
    • The bot essentially is a web API you can deploy anywhere. You just need the bot’s endpoint to register it which usually looks like this: https://example.com/api/messages

     

  2. Fill in the registeration form here: https://dev.botframework.com/bots/new
    • By now the bot is registered and ready to go

     

  3. Follow steps for Telegram setup
    • In your bot’s management page there is a list of “channels” you can add as a means for people to interact with your bot, for example there is: Facebook Messenger, Slack, Twilio, Kik and more. Each “channel” has its own simple set of instructions for you to follow which allow integration between that channel and your app.

 

wwld_pic1

 

Final Thoughts

I soon realised how novel this bot was going to be, which to start with is fine. Yes, it gives silly responses randomly based on the topic that my LUIS model thinks you’re talking about. But soon it will have Office 365 integration where you can check if I’m free or busy using my calendar and even book meetings with me through my bot. I can then start adding more machine learning APIs such as the text sentiment API which will also the bot to reply differently based on the positive or negative sentiment of the user’s language. The possibilities are endless.

It has become evident that the more I write about this, the more I realise it’s less of a “What would Lilian do” bot, and more of a “Lilian’s Personal Assistance with some attitude” bot. And that’s perfectly fine with me!


Help me improve my LUIS model! You can speak to my bot on Telegram by searching for WWLD_Bot

You don’t need C# to use this framework, nodejs is available too: https://docs.botframework.com/en-us/node/builder/overview/#navtitle

And finally, check out all the code on GitHub: http://www.github.com/liliankasem/WWLD