top of page
Search
  • Writer's pictureTim Kosmala

Connect your messengers to a Web Server API using AutoResponder

Process and answer incoming messages from different messengers with your website, webhook or API. All you need is the AutoResponder app and, of course, a web server like this*. AutoResponder currently supports WhatsApp, Facebook Messenger, Instagram, Telegram, Signal and Viber. To learn more about AutoResponder, please visit AutoResponder.ai.


When you connect a web server to AutoResponder, you can, for example, retrieve data from a database, create statistics, take orders, use third-party APIs apart from Dialogflow or do something completely different. There are no limits to what you can do. The important thing is that your webhook returns immediate responses to AutoResponder. However, you can always decide if a reply should be sent or not.


AutoResponder webhook integration

Follow these instructions to connect AutoResponder to your web server


2. Get webhosting from a webhoster like Hostinger*. (If you use a different one, make sure it supports php if you want to follow this php example.)

Get webhosting from Hostinger

3. Log in to your web server console.

4. Once your web server is ready, create a new .php file with any name in your website directory.

5. Copy the following code and paste it in your new .php file. Please then read the // comments in the code. They will help you complete the web server configuration.


View code on GitHub.

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// add this php file to your web server and enter the complete url in AutoResponder (e.g. https://www.example.com/api_autoresponder.php)

// to allow only authorized requests, you need to configure your .htaccess file and set the credentials with the Basic Auth option in AutoResponder

// access a custom header added in your AutoResponder rule
// replace XXXXXX_XXXX with the name of the header in UPPERCASE (and with '-' replaced by '_')
$myheader = $_SERVER['HTTP_XXXXXX_XXXX'];
  
// get posted data
$data = json_decode(file_get_contents("php://input"));
  
// make sure json data is not incomplete
if(
	!empty($data->query) &&
	!empty($data->appPackageName) &&
	!empty($data->messengerPackageName) &&
	!empty($data->query->sender) &&
	!empty($data->query->message)
){
	
	// package name of AutoResponder to detect which AutoResponder the message comes from
	$appPackageName = $data->appPackageName;
	// package name of messenger to detect which messenger the message comes from
	$messengerPackageName = $data->messengerPackageName;
	// name/number of the message sender (like shown in the Android notification)
	$sender = $data->query->sender;
	// text of the incoming message
	$message = $data->query->message;
	// is the sender a group? true or false
	$isGroup = $data->query->isGroup;
	// name/number of the group participant who sent the message if it was sent in a group, otherwise empty
	$groupParticipant = $data->query->groupParticipant;
	// id of the AutoResponder rule which has sent the web server request
	$ruleId = $data->query->ruleId;
	// is this a test message from AutoResponder? true or false
	$isTestMessage = $data->query->isTestMessage;
	
	
	
	// process messages here
	
	
	
	// set response code - 200 success
	http_response_code(200);

	// send one or multiple replies to AutoResponder
	echo json_encode(array("replies" => array(
		array("message" => "Hey " . $sender . "!\nThanks for sending: " . $message),
		array("message" => "Success ✅")
	)));
	
	// or this instead for no reply:
	// echo json_encode(array("replies" => array()));
}

// tell the user json data is incomplete
else{
	
	// set response code - 400 bad request
	http_response_code(400);
	
	// send error
	echo json_encode(array("replies" => array(
		array("message" => "Error ❌"),
		array("message" => "JSON data is incomplete. Was the request sent by AutoResponder?")
	)));
}
?>

6. Add a new AutoResponder rule.

7. In your AutoResponder rule, set the received messages that should be processed by your web server. It is usually recommended to use the All button in the upper right corner to process all messages with your web server.

8. Select the Web Server option in your AutoResponder rule and enter the complete URL of the .php file you have just created (e.g. https://www.example.com/api_autoresponder.php).

9. The Basic Auth and Headers options are optional. Please refer to the comments in the code above if you like to use them. It is recommended to add some kind of authorization if your API allows access to sensitive information.

10. That's it! You are now ready to test your web server integration.


Tip: You can use AutoResponder's answer replacements (e.g. %name%) in your webserver reply messages or in the Headers value field in AutoResponder which gets sent to your web server.


Technical details


Your web service will receive a HTTP POST request with JSON objects from AutoResponder every time the rule is executed. For more information search the web for REST API.


Request to your web server

{
    "appPackageName": "tkstudio.autoresponderforwa",
    "messengerPackageName": "com.whatsapp",
    "query": {
        "sender": "John Smith",
        "message": "This is an example!",
        "isGroup": false,
        "groupParticipant": "",
        "ruleId": 42,
        "isTestMessage": false
}

Your web server response

{
    "replies": [
        {
            "message": "Example reply 1"
        },
        {
            "message": "Example reply 2"
        }
    ]
}

Don't worry, this has already been taken into account in the code above.

Just follow the instructions :)

*affiliate link

27,165 views

Related Posts

See All
bottom of page