Recently I was working on a project that needs to set up a webhook to fetch incoming messages. So I thought I should share the code & the procedure with you. So let’s get started.
If you are going to setup WhatsApp API so I assume you are aware of the WATI Dashboard interface, so I am not going to explain the dashboard features
- To set up a callback URL where WATI pushes data on new messages. Navigate to 3 lines in the menu
- Click on the Add webhook to push data onto your server and add the URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php // Fetch Data from Webhook $data = file_get_contents("php://input"); //Decode from json $events = json_decode($data, true); //Store data into variables $id = $events['id']; $created = $events['created']; $conversationId = $events['conversationId']; $ticketId = $events['ticketId']; $text = $events['text']; $type = $events['type']; $data = $events['data']; $timestamp = $events['timestamp']; $owner = $events['owner']; $eventType = $events['eventType']; $statusString = $events['statusString']; $avatarUrl = $events['avatarUrl']; $assignedId = $events['assignedId']; $operatorName = $events['operatorName']; $waId = $events['waId'] = '918802915988'; $messageConatct = $events['messageConatct']; //Store to your databas $sql = "INSERT INTO test (id,created,conversationId,ticketId,text,type,data,timestamp,owner,eventType,statusString,avatarUrl,assignedId,operatorName,waId,messageConatct) VALUES('$id','$created','$conversationId','$ticketId','$text','$type','$data','$timestamp','$owner','$eventType','$statusString','$avatarUrl','$assignedId','$operatorName','$waId','$messageConatct')"; $con->query($sql); |
Please Like Share & Comment. Your support encourages us to post more helpful content. Have a good day