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

Skip to main content

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

    What to Ask in a UX Discovery Session

    Better questions lead to better design. In my years as a UI/UX designer, I’ve learned that many design challenges may come from misalignment at the start. Discovery sessions are where product clarity is either made or missed. These are a few discovery questions I consistently return to in the discovery phase of my UX design […]

  • Digital Products

    Designing For Play and Friction in a Fast-Paced World

    At UDig, play is an integral part of our philosophy. “Having fun” is embedded in our culture, and we always find opportunities to use play to encourage collaboration, ignite creativity, and make room for bold experimentation to build stronger teams and solve problems ranging from the seemingly simple to the most complex. I always have […]

  • Digital Products

    Config 2025 Day Two Recap

    It felt as though Config 2025 ended as soon as it began, and I believe those of us that attended are all the better for it. By the end of the day, various inspirational and informative talks had been given by thought leaders and innovators in the product space. Between sessions, we had the opportunity […]

  • Digital Products

    Inside Config 2025: What’s New in Figma

    Config 2025 kicked off with a bang on Day 1. Figma’s annual conference brings together designers, developers, and all those involved in the making of a product. The 2-day event has a stacked lineup of accomplished speakers ready to share their insights on the world of product building. At today’s opening keynote, the Figma team, […]

  • Digital Products

    Choosing the Right Modernization Approach

    When organizations decide it’s time to modernize their technology infrastructure, choosing the right approach is crucial. Modernization isn’t merely a technical upgrade; it’s a strategic business move that significantly impacts growth, agility, and long-term success. Here’s how your company can effectively begin to select the best modernization strategy tailored to your goals and challenges. In […]

  • Digital Products

    TAG Panel: Differentiate Your Customer Experience

    Join the CX and Product Management Societies to hear from our panel of Human-Centered Design experts on the business value of Agentic AI.