Webhook Get Attachment

You can download the attachment sent to your device using webhook.

This function will only working on device with all feature package.

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

<?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'];
$text= $data['text']; //button text
$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'];
//save the file
if($url){
file_put_contents("fonnte-file.$extension", file_get_contents($url));
}
//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);

If your attachment have message with it, you can find it in the $message.

The attachment will be downloaded and saved in the same path as your webhook url.

To save somewhere else, determine the path where the attachment should be saved.

Note: The attachment will follow file limitation rules. if you are receiving an attachment outside file limitation rules, you will not receive it on your webhook.

Related knowledge

See more
Made with in Indonesia