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.
- Open the discourse.handler.js file in your file editor.
- Find the
switch
statement with thecase
for thetellmeabout
intent. - Create a new
case
for theweather
intent. - 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. - Determine whether the
park
context variable is present in the responser
. Look forcontext.park
. - If
context.park
is present, useParksDatabase
to look up the park and then get the park’slat
andlon
.
Note: Ifcontext.park
is not present, you can’t get alat
andlon
, and therefore can’t get the weather. In this case, returnr lat
unaltered.
Ifcontext.park
is present, pass thelat
andlon
to the function or module you created in Step 4.
The response from Step 4 likely occurs asynchronously. So far, thediscourseHandler(r)
is synchronous. You need to adjust the return value of thediscourseHandler(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. - Extract the
observation
property from your function or module’s response value. - Add the value of the
observation
property tor.output.weather
.
The UI expects the following code in the response:cardType = weather
r.output.weather
contains the weather response’sbody.observation
property. The UI needs this information to render properly.