How to send OTP Message to WhatsApp with BotSailor API by PHP

blog

3 Nov 2022

5031

BotSailor API is now available for use in integrating BotSailor with third-party websites or apps. You may now send WhatsApp messages from your website or application by utilizing the BotSailor API.

 

This article will walk you through the process of sending an OTP message to WhatsApp using API.

 

 

 

Step 1:

 

First, from the dashboard of BotSailor click on your user account and then select API developer. Then, to obtain a new API code, click the Generate API Key Button.

 

User Account > API Developer > Generate API Key

 

 

 

 

There are two types of WhatsApp API, one is “Send API” and the other one is “Subscriber/Contact API”. We will use the “Send API” to send WhatsApp messages.

 

 

 

We will show an example of sending OTP message to WhatsApp by using a PHP script.

 

We must utilize a Template Message for a business-initiated message and messages sent outside of the 24-hour chat window. We can only send messages to individuals who are active on chat within 24 hours. But a Template message could be sent to anyone even if we add a subscriber manually.

 

 

 

Step 2: (Creating Template Message for OTP)

 

 

 

To create a Template message, go to WhatsApp Bot Manager under WhatsApp. Select the WhatsApp account and then go to Message Template.

 

WhatsApp > Bot Manager > Select Bot Account > Message Template

 

 

 

Before we create a message template, we'll create a variable that will be used on the message template. This is due to the fact that various users will receive different OTPs. To create a variable, click on the “Create” button from the Template variables.

 

 

 

 

 

 

Give a name for the variable and click on the save button.

 

 

 

 

 

 

Now, from the message template settings click on the “Create” button to create a template.

 

 

 

 

 

 

As soon as we complete that, a template creation form will appear. Give the Template a name and choose a locale. Then choose the template's category, and because we are making an OTP template, select the type "OTP." If you want to add a header, select the Header type; it is not required.

 

 

 

Write the message in the body and add the variable we just generated for the OTP message by selecting it from the list. Then there's the footer, which is optional for a template. Because there are no buttons on this template, we shall leave this as "None." Finally, press the save button.

 

 

 

 

 

 

The template should be in the list after it has been created.

 

 

 

 

 

 

Click the check status button to discover if the template has been approved or not. They must immediately approve or disapprove.

 

 

 

 

 

 

Yes, the template was created and approved successfully.

 

 

 

 

 

Step 3:

 

Now, let’s get back to the API developer again. We need to generate API end-point to send a template message. So, let’s go to the “Generate API End-point : Send Template Message (GET)”

 

Choose the WhatsApp account from which to send the message. Simply enter a phone number (Use your WhatsApp phone number). Then, we will have to select the Template message (OTP message template) from the list. Select the Message Template we have just created.

 

 

 

 

 

 

As soon we select the Message template it will ask us to put a name for it. Just put OTP on the field and click on the “Get API end-point”

 

 

 

 

 

 

It will generate the API End-point. We will use this code in the PHP script to send OTP messages to WhatsApp users.

 

 

 

 

 

 

In the PHP script, we will put this generated code into a variable. Then we need to write a few lines of code.

 

 

 

Code:

 

 

 

 $url="https://botsailor.com/api/v1/whatsapp/send/template?apiToken=1|bF90hdFU4Mm2uFxIFDbvvEIcxYqweyJuwrO25yEM&phoneNumberID=100761702762631&botTemplateID=712&templateVariable-OTP-1=OTP-VALUE&sendToPhoneNumber=SEND-TO-PHONE-NUMBER";

 

 

    $ch=curl_init();

 

    curl_setopt($ch, CURLOPT_URL, $url);

 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

 

    echo $response=curl_exec($ch);

 

?>

 

 

 

We need to change the OPT-VALUE and SEND-TO-PHONE-NUMBER from the API end-point to variables. Just like below.

 

 

 

 

 

 

The final code will look like this after updating the values with variables.

 

 

 

Final Code:

 

 

 

    $otp= random_int(10000, 99999);

 

    $send_number="8801722977459";

 

 

    $url="https://botsailor.com/api/v1/whatsapp/send/template?apiToken=1|bF90hdFU4Mm2uFxIFDbvvEIcxYqweyJuwrO25yEM&phoneNumberID=100761702762631&botTemplateID=712&templateVariable-OTP-1={$otp}&sendToPhoneNumber={$send_number}";

 

    $ch=curl_init();

 

    curl_setopt($ch, CURLOPT_URL, $url);

 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

 

    echo $response=curl_exec($ch);

 

?>

 

 

 

[ Use must use your generated API End-point ]

 

If we just run this code, the message should be delivered to the WhatsApp number.

 

So, this is how we can utilize the WhatsApp API of BotSailor to deliver OTP messages to WhatsApp numbers.

 

 

 

 

 

Video instructions on How to Send OTP Message to WhatsApp with BotSailor API:

 

 

 

 

 

 

 

 

 

 

 

Share This Post
author
Written by

Kamrul Hasan

➢  more from this author

Comments (3)

  • author
    KSR DATAVIZON PVT LTD
    2 months ago
    • author
      KSR DATAVIZON PVT LTD 2 months ago
  • author
    BotSailor
    1 year ago