Integrate the Weather Company Data service into your server back end

Enhance the sample application to answer simple questions about the current weather at national parks. Write code to integrate the Weather Company Data service into the server.

This section might be a bit challenging. Some steps do not include all the details needed to implement the step. This is meant to be a challenge.

If you get stuck, don’t fret; simply review the solution video.

You need to modify the discourseHandler(r) function in the server/api/services/discourse/discourse.handler.js file.

  1. Open the discourse.handler.js file in your file editor.
  2. Find the switch statement with the case for the tellmeabout intent.
  3. Create a new case for the weather intent.
  4. Create a function (or module) that invokes the Weather Company Data service’s current observations endpoint.
    Your function (or module) should accept the lat and lon value as input. It should use those values to invoke the following endpoint:
    ${WEATHER_API_ROOT}/api/weather/v1/geocode/${lat}/${lon}/observations.json
    The function or module should then return the response body from this above endpoint unaltered.
  5. Determine whether the park context variable is present in the response r. Look for context.park.
  6. If context.park is present, use ParksDatabase to look up the park and then get the park’s lat and lon.
    Note: If context.park is not present, you can’t get a lat and lon, and therefore can’t get the weather. In this case, return r lat unaltered.
    If context.park is present, pass the lat and lon to the function or module you created in Step 4.
    The response from Step 4 likely occurs asynchronously. So far, the discourseHandler(r) is synchronous. You need to adjust the return value of the discourseHandler(r) so that it can asynchronously handle this. After you make this change, you also need to update the server/api/services/discourse/index.js file to also handle the asynchronous response.
    If you get stuck, don’t fret; simply review the solution video.
  7. Extract the observation property from your function or module’s response value.
  8. Add the value of the observation property to r.output.weather.
    The UI expects the following code in the response:

    • cardType = weather
    • r.output.weather contains the weather response’s body.observation property. The UI needs this information to render properly.