Chatting with Watson to Hook any Tweets: Webhook Tutorial

Erika Agostinelli
Aug 29, 2019 · 6 min read

In this tutorial you will learn how to make external API callouts from Watson Assistant. We will use Webhooks, Cloud Function, and Twitter API, to retrieve the last tweets of any account.

Image for post
Image for post
Photo by Torsten Dettlaff from Pexels

What you will learn:

  1. How to use Webhooks in Watson Assistant
  2. How to use and write Cloud Functions (in Python)

Prerequisite:

  1. Watson Assistant instance
  2. Twitter Developer Account : Consumer key (API key), Consumer Secret (API Secret), Access Token, and Access Token Secret.

Introduction

In this tutorial, we will use the new webhooks feature from Watson Assistant, and cloud functions (FaaS) to achieve this goal and you will be able to ask your virtual assistant to retrieve the last tweets from any Twitter account.

Code for this tutorial is available on Github

Cloud Function

Be sure to work on a CF-Based namespace and the Cloud Function and Watson Assistant instance need to be in the same region.

Pick a name for your function (e.g. “last_tweets”) and runtime (in this case I have picked Python 3) then click on “Create”. You can find the code to copy & paste here but make sure to add your Twitter Dev Account credentials in the appropriate fields (client_key , client_secret) and hit Save:

Now, click on “Parameters” (left banner) and add a variable called “account”. This parameter will receive information from your Assistant when the user explicitly asks for tweets of a specific account. Here, I’m leaving a default value (@taylorswift13) so that I can test my function.

Image for post
Image for post
add a default value to be able to test your function by using “Invoke”

Let’s check if everything is working fine: go back to the “Code” tab (on the left) and invoke your function. You should be able to get the last tweets from Miss Swift (Default Value for now).

Image for post
Image for post
Successfully tested the Action that returns correct values (on the right)

Everything working? If yes, next step:

Since you have completed your cloud function, click on “Endpoints” and take notes of:

  1. the URL of your action
  2. then click on “API-KEY” and the first part of the string (before the colon “:”) is your username and the second part is your password
Image for post
Image for post
API Key — get username and password to access the

Webhooks and Watson Assistant

A Webhook sends a POST request callout to an external application that performs a programmatic function. When used in a dialog skill, a webhook is triggered when the assistant processes a node that has a webhook enabled.

How to use a webhook:

  1. Webhook setup: specify the request URL for an external API you want to invoke
  2. Enable a dialog node: a webhook enabled node will trigger the action and sends specific parameters to the API.

Webhook Setup

  • In Request URL, specify the URL of your action and it’s important to append ?blocking=true parameter to the action URL to force a synchronous call to be made. You cannot make an asynchronous call to a cloud functions action with a webhook. When you send an asynchronous request, only an activation ID is returned.
https://us-south.functions.cloud.ibm.com/api/v1/namespaces/<your-email>/actions/last_tweets?blocking=true
  • In Headers: Click Add authorization, add your credentials to the User name and Password fields, and then click Save.
Image for post
Image for post
How to set up a Webhook in Watson Assistant — super easy

Enable a dialog node

Image for post
Image for post
How to activate the webhooks option in the node
Image for post
Image for post
Inside the “Account Specified” node

In “Account Specified” node (where Webhook option is active) you can see several new fields.

  1. Parameters: data that you want to pass to the external application as key and value pairs
  2. Return variable: response made by the callout is saved to the return variable
  3. Response condition if $webhook_result_1 is recognised: response to show when the webhook callout is successful and a return variable is sent back. Here, I would suggest playing with $webhook_result_1 variable to understand the content of the response and to decide what and in which format you want to show the results to the end-user. (i.e. last tweet:<br>-<? $webhook_result_1.response.result.result[0] ?>)
  4. Response condition if anything_else is recognised: response to show when the callout fails

Try it out!

Image for post
Image for post
“Try it out” — type a phrase specifying any twitter account (use the “@” symbol) and test your function!

Conclusion

Code for this tutorial is available on Github

References

Webhooks

Cloud Functions

IBM Watson

AI Platform for the Enterprise

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store