The world’s leading publication for data science, AI, and ML professionals.

Meet WondyBot

A story of how a software developer built a chatbot to help manage a stock portfolio. Powered by free Microsoft tools such as LUIS, Bot…

Photo by Jason Leung on Unsplash
Photo by Jason Leung on Unsplash

What is WondyBot

To put it simply, WondyBot is a chatbot (no, really). The dream was to create a chatbot that would keep track of my stocks and shares portfolio, alerting me of any price changes, giving me details and news about companies, analysing and recommending new potential opportunities. The hook being that all the functionality could be accessed using well-established UIs, such as Telegram and Facebook Messenger.

The concept was good, the excitement was strong, so I got straight to work. And that was my first mistake. I was attempting to create all the functionality, perfectly first time. I wasted too much time aiming for the final product, rather than using this as a learning platform. I started creating a backend API that would be able to do everything, then I would get stuck because I didn’t have the knowledge nor experience of how a chatbot should work end-to-end.

I took a step back; I went about making a proof of concept (POF).


WondyBot 1.0 (POF)

The first part that I wanted to achieve, was to return a company profile, on request, in Telegram. This consisted of 3 key requirements:

  1. Allow communication with a Telegram bot.
  2. Understand the users intent.
  3. Retrieve market data about a listed company.

1 Telegram allows you to create a free bot without even leaving their app, all you have to do is talk to their brilliantly named "BotFather". I then created a .Net Core console application, utilising webhooks and the "Telegram.Bot" NuGet package. I used this walkthrough to get my C# code talking to my telegram bot, creating a simple echo bot:

Telegram Desktop (left) has sent a message to the Command Line (right) where the text is returned. Image by author
Telegram Desktop (left) has sent a message to the Command Line (right) where the text is returned. Image by author

The walkthrough, however, uses a stack of IF/ELSE statements in their attempts to respond to user input. It gives us a starting point, but it would be painful for the users and even more so for the developers.

2 How can we get around this – Enter LUIS! It’s Microsofts Natural Language Understanding offering. There are several out there, one of which is IBM Watson; my friend (Stephen Chapendama) went down this route in his no-code chatbot attempt, you can read about it here.

So, how does LUIS work? A magical mix of Machine Learning, Natural Language Processing and Natural Language Understanding. Using LUIS, you can train your application to understand the users’ natural language and then return to you the intent and any entities. You have to generate a model which involves giving it examples of user input, but from then on it’s as fundamental as hitting an endpoint. This video helped me out with the setup of LUIS.

The message that is returned from the LUIS endpoint, including intent ("GetCompanyDetails") and entity ("companyName"). Image by author.
The message that is returned from the LUIS endpoint, including intent ("GetCompanyDetails") and entity ("companyName"). Image by author.

As you can see above, the response from LUIS is clean (if you’ve trained it against a good model). We can use the intent to call a method and then pass in the entities as variables. For example, an intent such as "GetCompanyDetails" and entities such as "companyName"…

3 Based on this article, I used Finnhub as my market data source. Again, you call an endpoint and the API returns some data, and the basic functionality is free. I wanted to return a more comprehensive company profile, so each request for company details involved multiple API calls to Finnhub. Combining the Finnhub data and the rest of the application allowed me to produce the following output:

Example of how we can obtain company details via LUIS and the Finnhub API. Image by author.
Example of how we can obtain company details via LUIS and the Finnhub API. Image by author.

WondyBot 2.0 (MVP)

Our POC gave us simple functionality by running a local console app, but we can do better than that, right? For this part we are looking to build a Minimum Viable Product (MVP), we will be guided by the following tasks:

  1. Allow dialogue between the user and the bot.
  2. Separate the frontend Bot and the backend API.
  3. Host in the cloud.

1 There is no way to ask follow-up questions, such as "Did you mean…?". Sticking with the Microsoft theme, I looked into their Bot Framework, the functionality seemed to be a great fit. One of the major draws was the ability to use Channels; dealing with all the connectivity to 3rd party apps (such as Telegram, Teams, Facebook Messenger).

The initial Microsoft example for Bot Framework was so easy to set up directly in Azure, no code necessary. However, when it came to extending this, I was overwhelmed. Other Microsoft examples, YouTube walkthroughs and even a Pluralsight course could not get me through coding a Bot Framework application. Maybe I will revisit this at a later date…

Eventually, I stumbled upon Bot Framework Composer from Microsoft. It’s a no-code solution, plus it has built-in functionality to LUIS and, lets you publish directly to Azure. I followed this tutorial to get me up and running here.

2Moving to the Bot Framework meant that I was able to cleanly keep my concerns separate. By this, I mean that I refactored the original Finnhub functionality into a dedicated API, that could be called directly from the Bot Framework. There is some familiarity between this and the MVC architecture, where the Model is our dedicated API, the View is the Telegram UI, and the Controller is the Bot Framework piece.

While I was working on the API, I also added in a way to calculate the Jaro–Winkler distance, using the "F23.StringSimilarity" NuGet package. This was so the user did not have to enter the company name exactly as listed, but also to handle any spelling mistakes.

The flow of data for GetCompanyDetails function in WondyBot 2.0. Image by author
The flow of data for GetCompanyDetails function in WondyBot 2.0. Image by author

3My experience with Azure is relatively basic. Thankfully, the Microsoft team have made it extremely easy to publish directly to Azure from both the Bot Framework Composer and Visual Studio.

When deploying to Azure from the Composer, you’ll need to perform some initial steps before you can publish your app. This walkthrough gives you all the info you need, whether you have Azure resources setup or not.

Regarding the deployment of the API to Azure, within Visual Studio 2019, you can set up Continuous Deployment. This makes deploying code changes quick and simple; all you have to do is commit to your master branch in source control. For this part, my codebase was in GitHub connected to an Azure DevOps Pipeline. This guide from Microsoft helped me here.

Although everything I have hosted in Azure is free, I was caught out a few times when setting up resources with default settings. If you are not sure, just check the service plan for each resource in Azure before you accidentally rack up a costly bill. Fortunately, for the first month, Microsoft is kind enough to give you some free credits. My advice would be to dive right in and play with as much as you can during this initial month.

With all these steps completed, I was able to build and host this MVP for free. The functionality is basic, but it does do what we set out to do:

A run-through of how WondyBot looks to the user. Image by author.
A run-through of how WondyBot looks to the user. Image by author.

The Next Steps

The road map for WondyBot is to continue development, adding additional functionality like user accounts and a price checker. But before we can look at that, we need to address a few things with the current bot. These include making use of the cache to reduce the FinnHub API calls, as well as adding some security to our .Net Core API. I will be making these changes in the next few weeks with a post to follow.

In this post, we have gone through the basic steps of how I went about making my first chatbot. There are many links scattered throughout the post, most of them are helpful tutorials or examples which have guided me to this stage. I have learnt a lot throughout, but I still feel that this is only the first steps, and I am very much looking forward to continuing development of WondyBot.

If you are attempting this yourself and find a new or different way of doing things, let us know in the comments.


Related Articles