Integrating Oracle Digital Assistant with WhatsApp (API)

a person holding a cellphone with logo on the screen
Photo by Anton on Pexels.com

This article was written by Anshuman Panda, and co-published with the Oracle Digital Assistant Blog.

Introduction

WhatsApp Cloud API allows businesses to send and receive messages using cloud hosted version of its WhatsApp Business API. By offering free, secure, and cloud-based hosting, businesses can scale their customer communication and reduce the go-live time.

Sandbox Setup

Sandbox is for testing. It is easy to use. You need to have an app created in Meta for Developers my apps. Here are the steps to create a new app if not already created.

  1. Go to Meta for Developers. If you are visiting for the first time, you will see get started button. Click on “Get Started”. Else, go to step 2.
  2. Navigate to MyApps buttons on the top right corner.

3. Click on create app and choose “Business” as app type and click on next.

4. Enter the display name for the app.

5. If you have business account, then select it or leave it as it is and click on submit button.

6. Scroll down on the page and select WhatsApp for setup.

7. Now create a meta business account by selecting “Create a business account” and click on continue.

8. Now you will see the get started page where you can see the temporary access token. Copy this token.

9. Here a temporary phone number(phone number id) is generated for testing purpose. Copy this temporary phone number id.

10. Copy the WhatsApp Business Account Id 

11. Add a recipient (To) phone number and verify it with the code which WhatsApp will send on the number. Update the same number at the time of WhatsApp Cloud API Integration with ODA

12. Now click on test button, for testing and getting the messages on the “Phone Number” mentioned in the sandbox page. Test the flow with the API which is given and see how messages are flowing from test number to the recipient number.

13. After testing, you can add a phone number and verify it using a code which WhatsApp sends. This is actual business number which you want to use for messaging your customer. This number will get added to the from number on the get started page.

14. Check the API version from the configuration section from the left panel in the setting->advanced tab as shown below. You will need to enter it on the webhook at the time of integration.

Create Webhook Channel in Oracle Digital Assistant

  1. Open Oracle Digital Assistant and open the Channels option under Development Tab

                

2. Press Add Channel Button and name it to as ODA_WhatsApp and a dummy Outgoing Webhook URI as https://www.oracle.com (temporarily until I host my webhook app somewhere) as shown in the image below.

3. Now  the channel is created.

4. Assign a skill and enable the channel

5. Scroll down to find the Secret key and Webhook URL . Copy both the values as we will need these in our webhook code.

6. Now open the webhook folder which I have provided and go to index.js file. Here you need to change the url and secret with the above copied Webhook URL and secret key of yours in line number 21 and 22.

// add webhook integration to Oracle Cloud
const webhook = new WebhookClient({
    channel: {
        url: YOUR ODA WEBHOOK CHANNEL WEBHOOK URL,
        secret: 'YOUR SECRET KEY'
    }
});

7. Now you need to host your code and expose it through a server. You can host it via ngrok, Heroku, render or any such providers as well.

8. After the app is hosted, we need to make sure to pass the environment variables to the server. I have created two environment variables  MYTOKEN, TOKEN which needs to be passed.

MYTOKEN can be any text such as my name as well which we need to enter same in Webhook config page of WhatsApp as well  . Whatever MYTOKEN value I enter here needs to be entered in verify token as well as shown below.

TOKEN can be obtained from getting started tab as shown below. It’s the temporary access token. You can also apply for a permanent token from facebook for your app

9. Once the environment variables are set then host your web server and if your hit the webserver url in your browser then you will get the below message

Once you get this message means that your webhook is up and running. Copy the webhook host url and append it with /webhook for whats app and /bot/message for ODA


Outgoing Webhook URI in ODA : https://*********.*************/bot/message

            Callback URL In WhatsAPP : https://*********.*************/webhook

10. Change the Outgoing Webhook URI from https://www.oracle.com to the above URL

 Setup the WhatsApp Webhook

You will need to create a webhook entry for getting messages from Oracle Digital Assistant.

Please take the node app which I have created and do make the following changes into that

Enter the URL: https://anshwhatsapp.*************/webhook

Verify Token: ******************. (As entered in your webhook environment variable MYTOKEN )

Once you have saved the webhook, click on manage and give the messaging permissions shown in the below given screen shot

Subscribe to “messages” for sure. You can select others as well as I have done below

Once you have given the permission it will show as “subscribe” as shown in the screen shot below.


Once we are done with the above steps, we are ready to try out from WhatsApp.


Attachments

Webhook Sample Code (ZIP)