Connectors

Contact support

Whatsapp - Getting started in Peliqan

Whatsapp - Getting started in Peliqan

The Whatsapp connector can be used to send automated messages to users using Whatsapp, and to receive messages from users. You can send messages based on templates and free text messages. You can also use Whatsapp Flows, which allows sending structured screens with forms, request the location of a user etc.

Setting up Whatsapp For Business (WABA)

Create a Facebook (Meta) app

Go to the Facebook (Meta) developers portal, create a Business Account and add an app:

https://developers.facebook.com

Configure Whatsapp for your Facebook (Meta) app

Go into the details of your app.

In the left pane under “Whatsapp” > Quick start, configure Whatsapp for your app:

  • Add a phone number (can be mobile number or land line)
  • Add the Webhook URL (see below under “Receiving messages”)
  • Configure a System User with a token (see below)

Now add a connection in Peliqan for Whatsapp and add your WABA id and access token.

Find your WABA id

To find the ID of a WhatsApp Business Account (WABA):

Go to Business Manager > Business Settings > Accounts > WhatsApp Business Accounts.

Create a token

Go to Meta Business Manager > Business Settings > Users > System users.

  • Add a System User
  • Assign permissions to your app
  • Generate a new token that does not expire, enable the permissions for Whatsapp: whatsapp_business_management, whatsapp_business_messaging

Sending messages to Whatsapp users

You can only send a message to a new user using a template. A free text message can only be sent after you have received a response from a user or when a user has sent a message to you first. Whatsapp wants to avoid that spam is sent to users.

Sending a template message

Create a template first in your Whatsapp app:

In your Meta app under the section Whatsapp (left pane) > Quickstart. In the center pane under Whatsapp Business, click on “Message templates”. Add a message template.

Example of sending a template-based message in a Peliqan low-code Python script:

# Get your "from" phone number id
whatsapp_api = pq.connect('Whatsapp')
phonenumbers = whatsapp_api.list('phonenumbers')
st.json(phonenumbers) # Show result

templatemessage = {
    "to": "+32498776655",
    "langcode": "en_US",
    "template_name": "hello_world", # Create your template first
    "from_phonenumber_id": "263567523509780" # From above lookup
}
result = whatsapp_api.add('templatemessage', templatemessage)
st.json(result)
Example code to send a template message with dynamic content and a Whatsapp flow (click to expand)

Sending a text message

Example of sending a text message in a Peliqan low-code Python script:

whatsapp_api = pq.connect('Whatsapp')
text = "Hi there"
textmessage = {
    'to': "+32498776655",
    'text': text.replace('"', "'").replace("\n", ""),
    'from_phonenumber_id': "263567523509790" # See above for details
}
result = whatsapp_api.add('textmessage', textmessage)
st.json(result)

Receiving messages from Whatsapp users

Whatsapp (Facebook) will send webhook events for each message from a user.

There are 2 ways to process incoming webhooks in Peliqan:

  • Using the Peliqan Webhook queue table (batch)
  • Using an API handler script (realtime)

Using the webhook queue (batch)

This approach allows you to process incoming Whatsapp messages (webhooks) in batch. Incoming webhooks are stored in a table in the Peliqan data warehouse. More info:

Incoming webhooks

Configure your Peliqan Account webhook URL for your Whatsapp app.

You can find your Webhook URL in Peliqan under Admin > Settings > Account webhook:

image

Add this URL for your app under Whatsapp > Configuration > Webhook > Callback URL:

image

Note that a new webhook requires a verification from Meta, but this is built-in into Peliqan and will automatically be performed !

Now when a user sends you a message or replies to your message, an incoming webhook event will be stored in your account. You can find these events in the following table in your Peliqan Data Warehouse: webhook.incoming_webhooks.

You can configure a scheduled script that runs e.g. every 5 minutes to process new incoming messages incrementally and send responses back to the user in Whatsapp.

Example script that will process webhooks incrementally, by storing a “state” (click here to expand)

Using webhook queue (realtime)

This approach allows you to process incoming Whatsapp messages in real-time using an API handler script in Peliqan. More info:

Publish APIs

Add a script of type “API handler” to handle incoming API calls (webhooks) from Whatsapp.

Example code for a real-time chatbot in Whatsapp using OpenAI ChatGPT (click to expand)

Configure a public API endpoint with method GET. Example:

image

In the Whatsapp configuration, enter the detail of your API endpoint as Callback URL.

Click on “Verify and Save”. Facebook will now make an API call to your script to get the URL verified.

The script will send the “challenge” in the response.

Once the verification is done, change your API endpoint method from GET to POST.

From now on, when you send a Whatsapp message, the script will send out a Whatsapp message and echo back your message.