Skip to content

Latest commit

 

History

History
161 lines (101 loc) · 5.64 KB

File metadata and controls

161 lines (101 loc) · 5.64 KB

Adding Webhooks to Watson Assistant

You can query for dynamic data using webhooks in IBM Watson Assistant. Our crisis communication chatbot uses two different sources:

Prerequisites

Make use of Discovery to get news information

  1. From your IBM Cloud account, go to Watson Discovery.

Discover Service

  1. Create a new lite service.

Create Discover Service

  1. Make note of the API key and the URL. You need that in the next steps.

Credentials

  1. Open the Watson Discovery NEWS service, which is a prepopulated discovery dataset updated and maintained by the Watson Discovery team.

Watson Discovery NEWS

  1. From the top right corner, open the API tab. Make note of the Collection ID and Environment ID.

NEWS Api info

Get a Weather Company API Key

  1. Go to https://callforcode.weather.com/register/

  2. Signup with your info. Your API key will be emailed to you.

  3. Save the API key for future use.

Creating Cloud Functions

  1. In the IBM Cloud catalog, go to IBM Cloud Functions.

  2. Click Start Creating.

functions

  1. Select Create Action.

create

  1. Name your action. For the Runtime dropdown, select Node.js 10.

environment

  1. Replace the code with action/covid-webhook.js

code

  1. Our code has two main parts. We decide whether to call the COVID-19 API or Watson Discovery based on a parameter sent on the function call. If a query param of type=api is set, you call the COVID-19 API on the summary endpoint.

It returns the data in the following format:

{
  Countries: [
    {
      Country: "",
      Slug: "",
      NewConfirmed: 0,
      TotalConfirmed: 0,
      NewDeaths: 0,
      TotalDeaths: 0,
      NewRecovered: 0,
      TotalRecovered: 0
    },
    {
      Country: " Azerbaijan",
      Slug: "-azerbaijan",
      NewConfirmed: 0,
      TotalConfirmed: 0,
      NewDeaths: 0,
      TotalDeaths: 0,
      NewRecovered: 0,
      TotalRecovered: 0
    },
    ...
  ]
}
  1. If there is specific location (Country/Country Code/US State) selected, you look for that location either using The Weather Company API or in the summary response and return the status for that location.

For example, the response for type=api and location=United States of America is shown below.

{
  "result": "Total Cases: 65778\nTotal Deaths: 942\nTotal Recovered: 361\n\nSource: Johns Hopkins CSSE"
}
  1. If you want to make a call to the Discovery service, you need to set some parameters that let you call the IAM-enabled service. On the left, click on the Parameters tab. Add the following parameters:
    • api_key (Discovery API Key)
    • twcApiKey (API key from The Weather Company)
    • url (Discovery Service URL)
    • collection_id
    • env_id

parameters

  1. Enable the action as a web action. To do so, select the Endpoints tab on the left. Click the checkbox beside "Enable as Web Action."

endpoint

  1. Make note of the HTTP URL. You will use this as the webhook for your assistant. You will have to add .json in the end of this url to make it work as a webhook.

http endpoint

Integrate data sources via a Watson Assistant webhook

  1. Bring up the COVID-19 assistant you created earlier. Find it in your IBM Cloud account under services > IBM Watson Assistant. Open the dialog by clicking the CDC COVID FAQ Dialog.

assistant

  1. Click on Options on the left.

options

  1. Under Options > Webhooks, in the URL text box, paste the URL from the Cloud Funciton step. Make sure to add a .json at the end of the URL.

url

  1. Select Dialog on the left navigation.

dialog

  1. Open up any dialog node you want to add a webhook call for.

  2. After selecting the node, click Customize.

customize

  1. Enable Webhooks by moving the toggle button to On in the Webhooks section. Click Save.

webhooks

  1. Add any parameter your webhook needs. These will be sent as query parameters.

webhook parameters

  1. Test that your webhook integration is working by going to the Try It tab and initiating a dialog that calls the webhook.

webhook test

You can easily use webhooks to give your Watson Assistant access to many external APIs and databases.