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

  • Software Engineering

    When There’s Too Much to Fix: How Smart Prioritization Unlocks Revenue at Scale

    Every operations team has a backlog. The question isn’t whether you can clear it — it’s whether you’re clearing it in the right order. For most teams, the honest answer is no. And that gap between the order work gets done, and the order it should get done is quietly costing organizations millions. The Volume Problem High-volume exception processing shows up across […]

  • Software Engineering

    Creating Reusable Code Templates to Reduce Client Project Startup Time

    In consulting, one of the least visible but most expensive phases of a project is the beginning. Teams can spend days or weeks setting up repositories, agreeing on structure, wiring basic infrastructure, and solving problems that have already been solved many times before. Code templates are a practical way to reduce overhead while improving consistency. […]

  • Software Engineering

    Player Three Has Entered the Game: How AI Is Finally Bridging the Divide Between Design and Engineering

    As AI begins to become more prominent in our day-to-day lives, I find myself in a unique position. As a practicing software engineer and UI/UX designer, I am genuinely happy to see the introduction of AI tools begin to take shape in our industry. But more importantly, I am happy to start seeing the effects it is having on what has historically been a pretty challenging relationship: the […]

  • Software Engineering

    The Disappearing Middle of Software Work: Why the Bookends – Strategy & Impact – Matter Most Now

    Here’s a question nobody in enterprise software wants to sit with: what happens to the middle? Not the middle of the org chart. The middle of the work. The vast, expensive layer of effort that has defined enterprise software delivery for thirty years—translating what the business wants into working code. The requirements-to-implementation pipeline. The “build phase.” […]

  • Software Engineering

    Zero-Code Telemetry with OpenTelemetry’s OBI

    Full distributed tracing and exception capture for any application — without writing a single line of instrumentation code. View the source code on GitHub → The Premise Observability is essential for understanding what’s happening inside your services, but instrumenting an application by hand — adding trace spans, logging calls, and metric counters throughout your codebase […]

  • Software Engineering

    Building a Consultant in the Trenches: How Playing Offensive Line Shaped My Consulting Career

    People often ask me the same question when they find out that I played college football: “Do you miss it?” On the surface, it’s a bad question with an obvious answer. Yes. However, if I give myself a minute to sit with that question, the answer is more nuanced. Yes, I miss playing football, but […]