Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Creating a Bot Application Using the Microsoft Bot Framework | Part 2

Creating a Bot Application Using the Microsoft Bot Framework | Part 2
Back to insights

Part 2 – Integrating Your Bot Application With a Real-Time API

In Part 1 of this blog series, we created a simple bot application using the Microsoft Bot Framework. Now we are going to extend our capabilities by connecting a bot application to a real-time API. For this example I am going to connect to the OpenWeatherMap API. To complete this exercise you will need to register for a free account and get an API key.

First we’ll create a Bot Framework application just like we did in [Part 1]. I’m calling this WeatherBot. We will need a library to parse the JSON returned by the API, so in Solution Explorer, right-click the project and select Manage NuGet Packages. In the search box, type Newtonsoft.Json and click the Install button to add the library. Now right-click the project and add a class called CurrentConditions. We will parse the JSON into this model. Add the properties and classes shown here. These are just the properties we will use for this example. For a fully fleshed-out class that reflects all the properties returned in the JSON, you can download the source for this blog post from GitHub.

Now right-click the project and add another class called OpenWeather. Add your API key as a string constant and create the GetWeatherAsync method as shown:

The GetWeatherAsync method takes the zip code input by the user and calls the OpenWeatherMap API to get the current conditions for that area. We parse the JSON into a CurrentConditions object and return that object.

Now, to wire up the bot to call the API. Open the MessagesController class. Note that the Post method takes an Activity object, and if it is a Message type it spins up a new Dialog.

In the Dialog class we will add our code to call the API. When the Dialog is created, it waits for a message to be received. Replace the boilerplate code in MessageReceivedAsync with the following code that takes the input from the user (zip code) and calls OpenWeather.GetWeatherAsync, then formats the returned object into a string to display to the user:

Now when we start the bot app (F5), load it into the bot emulator, and enter a zip code, we get a nice message back:

For a real bot application we would need to add a welcome message and some error handling, and possibly provide more than just the current conditions (a forecast, for example). We will dive into that in the next blog post, Adding Natural Language Processing (NLP) to Your Bot Application with Microsoft LUIS.

Download the source code for this blog post at UDig’s GitHub.

Digging In

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]

  • Digital Products

    Mastering Legacy Application Modernization: Strategies for Success

    The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]

  • Digital Products

    CTO Confessions Podcast

    In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.

  • Digital Products

    Navigating the Challenges of On Premise to Cloud Migration

    In today’s rapidly evolving technological landscape, the shift from on premise solutions to cloud-based infrastructure has become a pivotal transformation for organizations seeking to modernize their IT operations. This transition holds the promise of increased agility, cost savings, and enhanced scalability. However, it is not without its set of formidable challenges that organizations must navigate. […]

  • Digital Products

    The Power of Transferrable Skills in Tech Projects

    Every project has its own unique elements that require flexibility to be effective and achieve success. This often requires picking up new pieces of a tech stack, learning a new programming language, or a new project methodology. Fortunately, there are also many transferrable skills that carry over from one project to the next. In my […]

  • Digital Products

    The Four Pillars of Effective Digital Product Development

    In 2020 alone, approximately two billion consumers purchased at least one digital product. From software licenses to mobile apps and tech tools, consumers are becoming increasingly active in the digital product market, a trend that has naturally spurred brands across a wide range of industries to reevaluate their digital product design and development process workflows. […]