Skip to content

Instantly share code, notes, and snippets.

@T1mmyK0
Last active March 9, 2024 17:28
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save T1mmyK0/2e8863919ad9523e12710d844859dd02 to your computer and use it in GitHub Desktop.
Save T1mmyK0/2e8863919ad9523e12710d844859dd02 to your computer and use it in GitHub Desktop.
Example of how to process AutoResponder requests with your web server 🤖 Add this php file to your web server and enter the complete URL in AutoResponder (e.g. https://www.example.com/api_autoresponder.php).
<?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?")
)));
}
?>
@nikoboomer
Copy link

nikoboomer commented Jul 28, 2022

Hi @T1mmyK0, playing many times, I think I succeeded, however, it would be better to add that rule of not responding "to make it work with certain rule"
For example, I have another rule that responds "hello how are you" and putting this rule above the welcome message also cancels this rule that is much higher up.

It would be nice if "don't reply" can only be attached to a certain rule.

In short: the "don't reply" rule cancels all rules and should only cancel the rule it's linked to. That would be great.

@nikoboomer
Copy link

nikoboomer commented Jul 28, 2022

Dear @T1mmyK0 forgive me if I overwhelm you but I also came up with another idea for your great app.

Just as there is a "submenu" panel for rules that are executed after the "parent" rule is triggered and user interaction is required

There should be a menu to execute immediately after the parent rule without requiring user interaction. Let's call him ("menu don't answer")

This could be used to activate the "do not respond" rule

Example:
I create the rule "do not answer"
I put as the base of the rule hello how are you

If it is detected in the message does not respond
But if it doesn't detect, immediately run what's inside the "do not respond menu"

This would be a good idea to implement such a feature more precisely by rule.

PD: Also note that the app doesn't respond to messages with duplicate rules for example.

Scenery:
I have a rule 1 that has as word
"hello" and replies "fine"
Then below it I have a rule 2 that has the word "*" (wildcard) that responds "very well 2"

The user writes:
"hello" should reply:
good (from rule 1)
very good 2 (from rule 2 joker)

However, currently only rule 1 responds "good" and the "wildcard" skips it.

The same happens for repeated rules.

@CalonDirektur
Copy link

Dear @T1mmyK0 first of all thank you for your time

From what I saw when autoresponder for wa gets the number it gets it from the notification. If you saved it as a contact, get the name and if not, get the number +51XXXXX in the case of my country.

In general, the companies do not keep the contact so the number would be obtained. This can be changed in the $sender variable and much more can be done with the help of a database.

That is why lose care in getting false data. You can alert the user to this for better information if you want.

It would help a lot to send a request to the server every so often in the rule options.

Seriously and seriously this would open thousands of possibilities.

I put this scenario.

Suppose the user gets the user of the notification but not the number. This can be easily solved with the help of a database from the same server where the user is responsible and thus obtaining the number.

PHP has millions of possibilities.

I have a little bit problem about submenu with scenario where I used some variable to check logic conditional. My problem cant be solved till now. I have tried with global variable and session usage but I can't call back my variable for conditional reply from user and my logic can't save the latest variable.

Do you have same problem with me too? Do you have solution based on my problem?

Best regards,
Thanks a lot

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 1, 2022

@CalonDirektur I don't fully understand what you mean. And having a database with numbers and users is the responsibility of the user who maintains the server. You can save messages using the Previous message and Previous reply answer replacements :)

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 3, 2022

@nikoboomer Sorry I didn't see your messages for some reason. If you have so many ideas, please contact me directly via mail. Don't reply cannot be attached to a rule, but you can move other rules higher in the list, then they will always be executed. Also only one rule can be triggered at a time. This has more advantages, because it allows to create conditions :)

@nikoboomer
Copy link

@nikoboomer Sorry I didn't see your messages for some reason. If you have so many ideas, please contact me directly via mail. Don't reply cannot be attached to a rule, but you can move other rules higher in the list, then they will always be executed. Also only one rule can be triggered at a time. This has more advantages, because it allows to create conditions :)

Hello dear @T1mmyK0 there are times that it is better, other times it is not.
In any case, a checkable option to decide if the rule is IF or ELSEIF (as it currently is) would be great. It would help a lot.

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 12, 2022

@nikoboomer This is a good idea, thanks :)

@nikoboomer
Copy link

nikoboomer commented Aug 15, 2022

Hello dear @T1mmyK0 It would be great if you implement it.
And the cron for the webserver every "X" (seconds, minutes or hours) would be great.
Much could be done with these tools.

For example:

Verify by php if we have new orders in woocommerce and according to the status of the order send a whatsapp message that the order is being processed, etc. The possibilities are limitless.
I hope you are encouraged to implement it.

I appreciate that you give me your email to send you ideas by that means.

@adith-suhas-sv
Copy link

adith-suhas-sv commented Aug 15, 2022 via email

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 15, 2022

@nikoboomer Only direct replies are possible. That's why there is no periodically webhook request. You can find my email directly within the app or in the Play Store listing of AutoResponder :)

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 15, 2022

@adith-suhas-sv Yea, that's right:)

@737991796
Copy link

كيف اربطه بخادم الويب

@T1mmyK0
Copy link
Author

T1mmyK0 commented Aug 18, 2022

@nikoboomer
Copy link

Hi, thanks for the reply to both of you.
I'm sending a couple of ideas to the email.
Greetings and good luck

@oivanr
Copy link

oivanr commented Aug 29, 2022

Hi @T1mmyK0 .. Can I show all data from MySQL as messages on WhatsApp? This is my code bellow for WA in AutoResponder: elseif ($data->query->message === '/trial') { ($conn = new mysqli( 'localhost', 'u133xxxx_bot_smart', 'Teleee@123', 'u133xxxx_bot_wa' )) or die('Database Error'); if (!$conn) { die('Could not connect: ' . mysqli_error($conn)); } $i = 1; $data = mysqli_query($conn, 'SELECT * FROM history'); while ($d = mysqli_fetch_object($data)) { echo json_encode([ 'replies' => [['message' => 'Data ' .$i++ . ' : ' . $d->command. $d->sender]], ]); }

But in MySQL I have 2 data. Screen Shot 2022-07-21 at 11 36 50

Do you have any suggestions for me to send all data from query SELECT * FROM history to WA? is my echo json_encode still wrong? Unfortunately in WA just show only 1 Data like this (but in my database I have 2 data) :

Screen Shot 2022-07-21 at 11 45 34

But I have tried in PHP with code likes still almost same with above in PHP : <?php // connect to DB $conn = new mysqli("localhost", "u133xxxx_bot_smart", "Teleee@123", "u133xxxx_bot_wa") or die("Database Error"); if ( ! $conn ) { die( 'Could not connect: ' . mysqli_error($conn) ); } echo "Connected successfully"; $i=1; $data = mysqli_query($conn,"SELECT * FROM history"); while ($d = mysqli_fetch_object($data)) { echo "<br/>"; echo $i++; echo "-"; echo $d->command; echo "-"; echo $d->sender; } And success to echo all data from database in website. Screen Shot 2022-07-21 at 11 40 15

Really need your help. Thanks before. Best regards.

Hi, this is Omar. I use this code to implement this solution:
$i = 1;
$consulta = "SELECT * FROM sar_cB_MultasT WHERE cB_NoPlaca = 'P123cbt' AND cB_Multa_pagada = 0";
$data = mysqli_query($dbc, $consulta);
$mensajes = [];
$mensaje = '';
if (mysqli_num_rows($data) > 0) {
while($fila = mysqli_fetch_array($data)){
$mensaje = $mensaje."Multa No. ".$i++.": Monto - Q".$fila['cB_M_monto']." Fecha: ".$fila['cB_M_fecha']."\n";
$mensajes['message'] = $mensaje;
}
echo json_encode(array("replies"=>array(
$mensajes,
array("message"=>"Ingrese el 9 para ir al inicio")
)));
}

Hope this can be useful for you

@adith-suhas-sv
Copy link

adith-suhas-sv commented Oct 11, 2022 via email

@nikoboomer
Copy link

nikoboomer commented Nov 24, 2022

Hello @T1mmyK0
I've noticed that sometimes facebook market place notifications are contained in the $data variable in the [query] array
[groupParticipant] the name of the product and sometimes not.
Why is this?

With which application do you recommend using autoresponder to always get the name of the product via [groupParticipant] in Facebook Messenger MarketPlace?

@T1mmyK0
Copy link
Author

T1mmyK0 commented Nov 24, 2022

@nikoboomer That may happen if the notification looks different. Can you please check how the notification of FM looks if this happens? Is there some pattern? For example always the first unread message etc?

@nikoboomer
Copy link

nikoboomer commented Nov 24, 2022

@nikoboomer That may happen if the notification looks different. Can you please check how the notification of FM looks if this happens? Is there some pattern? For example always the first unread message etc?

Yes it is correct so notice sometimes the facebook notifications are different sometimes it contains the name of the product other times it doesn't.

I think FB autoresponder receives the $data from the notification right?

Is there an option to receive the $data of the message itself?

Have you tried any other option where the notification is always received with the product? either on facebook messenger lite or facebook workplace

It would be very useful to always get the product name from the marketplace messages

@T1mmyK0
Copy link
Author

T1mmyK0 commented Nov 24, 2022

@nikoboomer Can you please send me screenshots of the different notifications to info@autoresponder.ai?
This is important for me to correct reproduce the issue :)

@nikoboomer
Copy link

@nikoboomer Can you please send me screenshots of the different notifications to info@autoresponder.ai? This is important for me to correct reproduce the issue :)

Hello, I sent you the detailed information, thank you.

@ChandraSans
Copy link

ChandraSans commented Nov 26, 2022

How do I create a welcome message from a web server?

@T1mmyK0
Copy link
Author

T1mmyK0 commented Nov 26, 2022

How to write a welcome message How do I create a welcome message from a web server?

You either manage it yourself with your webserver or you use the welcome message option of AutoResponder and maybe connect your webserver with it. In this welcome rule you can then for example use custom headers to tell your webserver that it's the welcome rule and not a general one.

@Ravi-34
Copy link

Ravi-34 commented Jan 2, 2023

How to pass Answer Replacements value eg. %prev_message_ID_1% value to the php script which will be called in the submenu rule?

@T1mmyK0
Copy link
Author

T1mmyK0 commented Jan 2, 2023

@Ravi-34 this is currently not possible. See if you can store these values on the webserver yourself for now :)

@Ravi-34
Copy link

Ravi-34 commented Jan 2, 2023

@Ravi-34 this is currently not possible. See if you can store these values on the webserver yourself for now :)

Oh Ok
A suggestion, In next version can you encode Answer Replacements value in the Headers Value input and then post the php script for us to use it from ther.

@T1mmyK0
Copy link
Author

T1mmyK0 commented Jan 2, 2023

@Ravi-34 Great idea! I'm currently adding answer replacements for the headers value field. It will be available in the next version. There is no specific server side code necessary.

@Ravi-34
Copy link

Ravi-34 commented Jan 25, 2023

@T1mmyK0 In a rule, can we set a fixed reply message and also then connect to our web server?
Example. In a rule, show a reply message "Please Wait...." and then show the replies from the web sever code.

Is this possible in the app now or technically possible in later version?

Thanks

@T1mmyK0
Copy link
Author

T1mmyK0 commented Jan 25, 2023

@Ravi-34 No, I think it's not needed. Because as a user I'm waiting for the reply anyways. And if I leave the chat I only want to receive a notification about the final reply.

@Ravi-34
Copy link

Ravi-34 commented Jan 25, 2023

@Ravi-34 No, I think it's not needed. Because as a user I'm waiting for the reply anyways. And if I leave the chat I only want to receive a notification about the final reply.

@T1mmyK0 Thanks for reply.

Just had a thought, it would be nice from our side to inform user that information has received and also is being processed.

@T1mmyK0
Copy link
Author

T1mmyK0 commented Jan 25, 2023

@Ravi-34 Yea, would be cool too. But I think it makes things just way more complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment