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

    Energy Evolution 2025: Balancing Economic Growth and Carbon Reduction

    Now that the election is behind us, we have an opportunity to anticipate the possible effects on the energy industry under this new administration. What strategies will be impacted? What will remain the same? What opportunities can we take advantage of in 2025? This blog is meant to dig into these questions and provide some […]

  • Digital Products

    The Growing Importance of Digital Accessibility

    Embracing Digital Accessibility: A Moral and Business Imperative In today’s digital landscape, accessibility has become crucial for businesses and organizations. With increasing awareness and legal requirements, ensuring that digital products are accessible to all users, including those with disabilities, is not just a compliance issue but a moral imperative. At UDig, we champion ADA compliance […]

  • 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. […]