How to send a WhatsApp message from AppSheet
You can send WhatsApp messages from AppSheet using the TextMeBot API. No code is required and easy to setup. If you want to check how to receive WhatsApp messages into your AppSheet, please look at this different post.
You can send WA messages from AppSheet using the “Custom Steps” within a Process.
1) Create a new Bot (it was called Automation before) in your App
2) Configure the Event. This will be the trigger for the bot to send the message. For example, when a new row is added into the table.
3) Add a new Step into the Process
4) Select “Create a custom step“
5) Select “Call a webhook”
6) Complete the following fields:
Url: http://api.textmebot.com/send.php?recipient=+91123123123&apikey=abcabcabcabc&text=Hello%20World&json=yes
HTTP Verb: GET
Tip: Please note that instead of using an URL with fix parameters, with you can use variables and expression to build the URL dynamically.
For example, you can use this URL:
http://api.textmebot.com/send.php?recipient=<<[phone]>>&apikey=xxxxxxxxxxxxxx&text=Hello%20<<[name]>>&json=yes
Where “phone” and “name” are the names of the columns in your database. For more information about using a webhook on an Automation/Bot, please check here the google AppSheet Documentation
How to create a Pause between messages
As you probably know, It is recommended to have a pause between messages. Currently, there is a taks within AppSheet to create a pause but the minum time to pause is 5 minutes what is too much for what you are looking for. The recommended pause is 5-8 seconds and you can achive that by using the following workarround.
I have create a simple php code that will just delay the output by 5-8 seconds. The only thing that you need to do is to call to this php endpoint using an AppSheet webhook task and the execution of the AppSheet flow will be “paused” for 5 seconds until the “slow” php page is loaded. Simple as that.
How to do it:
Create a “Call a Webhook” Custom task to GET https://dev.textmebot.com/command/delay5.php between the tasks that are sending the messages.
That delay5.php page will take 5-6 seconds to load so it will create a pause in the flow.
How to capture the response from the API (success or error)
You can receive the response from the API that was executed using a webhook to check if the message was sent successfully or there was an error (like phone number doesn’t exist, wrong APIkey, etc.).
You have probably noticed that there is a parameter &json=yes added at the end of the endpoint. This parameter is intentioanlly added to receive the API response in JSON format that you can interprate in AppSheet to determine if there was an error or not.
Having the reponse as JSON, will allow you to easily determine if there was an error or not.
In order to receive the response from the API, you need to enable the “Return Value” feature in the “Call To Webhook” Step settings. Apart from that, you need to enter the name of the variables. You need to respect the variables names from the API JSON response (“status” and “comment“).
Now, everytime that the “Call To Webhook” Step is executed, it will return the variables <<[SendWA].[status]>> and <<[SendWA].[comment]>> that you can reuse in the bot execution flow.
API Response examples:
Message that has been sent successfully:
Output:
{"status":"success","comment":"Message sent"}
Message failed because the phone number doesn’t use whatsapp:
Output:
{"status":"error","comment":"Invalid Destination WhatsApp number"}
Message failed because of wrong APIkey:
Output:
{"status":"error","comment":"Invalid APIkey"}
Message failed because the API is not connected (QR Scan needed):
Output:
{"status":"error","comment":"Phone number is disconnected from the API (DB)"}
You can use these instructions to capture the response from the webhook call:
https://support.google.com/appsheet/answer/12738438?hl=en
How to Receive WhatsApp Messages in AppSheet
I have created a step-by-step instructions on how to receive WhatsApp messages (answers for example) in AppSheet.
The Instructions are here: https://textmebot.com/receive-whatsapp-in-appsheet/