Make whatsapp bot using PHP (webhook)

A static chatbot is helpful for a questioning user who just want a piece of information.

But for a dynamic information, fonnte's dashboard cannot help you further.

Then, this is where our webhook shine!

You can get all information you need from your own database with webhook.

This tutorial require you to understand basic programing language mainly PHP. If you don't have any experience or don't know what to do, please consider hire a developer.

Special note : any autoreply feature won't work if you are using webhook.

Prerequisite

Before you can use this webhook, you need to make an account, login and create a device.

Copy the token as API key to be used on webhook.

Then, connect your device first before you can proceed to send a message.

Please use most recent curl with php version of 7.1++ as requirement.

Webhook code

So making whatsapp chatbot using fonnte's webhook will enable you to use your own database data.

Don't worry, we cannot access your database.

Everyting is coded in your own server.

The only thing we need is your file url.

First, copy this code

<?php
header('Content-Type: application/json; charset=utf-8');

$json = file_get_contents('php://input');
$data = json_decode($json, true);
$device = $data['device'];
$sender = $data['sender'];
$message = $data['message'];
$member= $data['member']; //group member who send the message
$name = $data['name'];
$location = $data['location'];
//data below will only received by device with all feature package
//start
$url =  $data['url'];
$filename =  $data['filename'];
$extension=  $data['extension'];
//end

function sendFonnte($target, $data) {
	$curl = curl_init();

	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://api.fonnte.com/send",
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "POST",
	  CURLOPT_POSTFIELDS => array(
	    	'target' => $target,
	    	'message' => $data['message'],
	    	'url' => $data['url'],
	    	'filename' => $data['filename'],
	    ),
	  CURLOPT_HTTPHEADER => array(
	    "Authorization: TOKEN"
	  ),
	));

	$response = curl_exec($curl);

	curl_close($curl);

	return $response;
}

if ( $message == "test" ) {
	$reply = [
		"message" => "working great!",
	];
} elseif ( $message == "image" ) {
	$reply = [
		"message" => "image message",
		"url" => "https://filesamples.com/samples/image/jpg/sample_640%C3%97426.jpg",
	];
} elseif ( $message == "audio" ) {
	$reply = [
	        "message" => "audio message",
		"url" => "https://filesamples.com/samples/audio/mp3/sample3.mp3",
		"filename" => "music",
	];
} elseif ( $message == "video" ) {
	$reply = [
		"message" => "video message",
		"url" => "https://filesamples.com/samples/video/mp4/sample_640x360.mp4",
	];
} elseif ( $message == "file" ) {
	$reply = [
		"message" => "file message",
		"url" => "https://filesamples.com/samples/document/docx/sample3.docx",
		"filename" => "document",
	];
} else {
	$reply = [
		"message" => "Sorry, i don't understand. Please use one of the following keyword :
		    
Hello
Audio
Video
Image
File",
];
}

sendFonnte($sender, $reply);

Function sendFonnte can use any API parameter available on fonnte's API.

You can get the data from database and set as reply message.

Second, save this code and input the file absolute public url to the webhook input.

Example : https://fonnte.com/urlwebhook.php

It's located in menu device->edit.

The last and the most important part is to set autoread to on.

It's located below webhook input.

That's all, your whatsapp bot is now ready to reply with dynamic data.

Related knowledge

See more
Made with in Indonesia