whatsapp.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. $base_url = 'https://api.zuwinda.com/v1/message/send-whatsapp';
  3. $secret_key = 'your zuwinda secret key webhook';
  4. $zuwinda_token = 'your zuwinda token';
  5. $post_data = file_get_contents('php://input');
  6. $json = json_decode($post_data);
  7. $signature = hash_hmac('sha256', json_encode($json->data), $secret_key);
  8. $headers = getallheaders();
  9. if ($headers['X-Zuwinda-Signature'] == $signature) {
  10. $data = $json->data;
  11. // log response from webhook
  12. error_log(json_encode($data));
  13. // filter by event message received
  14. if ($data->event == "WHATSAPP_MESSAGE_RECEIVED") {
  15. // your action
  16. if ($data->content == "!halo") {
  17. $ch = curl_init( $base_url );
  18. $payload = json_encode( array( "instances_id" => $data->instances_id, "to" => $data->from, "content" => "Halo zuwinda 🥳" ) );
  19. curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
  20. curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'x-access-key:' . $zuwinda_token));
  21. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  22. $result = curl_exec($ch);
  23. curl_close($ch);
  24. }
  25. if ($data->content == "!button1") {
  26. $buttons = array(
  27. (object)[
  28. "buttonId" => "id1", "buttonText" => (object)["displayText" => "button 1", "type" => 1]
  29. ]
  30. );
  31. $button_message = [
  32. "text" => "hello zuwinda 🥳",
  33. "footer" => "zuwinda button",
  34. "buttons" => $buttons,
  35. "headerType" => 1
  36. ];
  37. $ch = curl_init( $base_url );
  38. $payload = json_encode( array( "instances_id" => $data->instances_id, "to" => $data->from, "content" => $button_message ) );
  39. curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
  40. curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'x-access-key:' . $zuwinda_token));
  41. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  42. $result = curl_exec($ch);
  43. curl_close($ch);
  44. }
  45. if ($data->content == "!button2") {
  46. $buttons = array(
  47. (object)[
  48. "index" => 1, "urlButton" => (object)["displayText" => "zuwinda", "url" => "https://zuwinda.com"]
  49. ],
  50. (object)[
  51. "index" => 2, "callButton" => (object)["displayText" => "call me", "url" => "+6285156172902"]
  52. ]
  53. );
  54. $button_message = [
  55. "text" => "hello zuwinda 🥳",
  56. "footer" => "zuwinda button 2",
  57. "templateButtons" => $buttons,
  58. ];
  59. $ch = curl_init( $base_url );
  60. $payload = json_encode( array( "instances_id" => $data->instances_id, "to" => $data->from, "content" => $button_message ) );
  61. curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
  62. curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'x-access-key:' . $zuwinda_token));
  63. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  64. $result = curl_exec($ch);
  65. curl_close($ch);
  66. }
  67. if ($data->content == "!button3") {
  68. $buttons = array(
  69. (object)[
  70. "title" => "Check Status", "rows" => array((object)["title" => "Healthy", "rowId" => "option1"], (object)["title" => "Love", "rowId" => "option2", "description" => "This is example description"])
  71. ],
  72. );
  73. $button_message = [
  74. "text" => "hello zuwinda 🥳",
  75. "footer" => "zuwinda button 3",
  76. "title" => "Title Example",
  77. "buttonText" => "Required, text on the button to view the list",
  78. "sections" => $buttons,
  79. ];
  80. $ch = curl_init( $base_url );
  81. $payload = json_encode( array( "instances_id" => $data->instances_id, "to" => $data->from, "content" => $button_message ) );
  82. curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
  83. curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'x-access-key:' . $zuwinda_token));
  84. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  85. $result = curl_exec($ch);
  86. curl_close($ch);
  87. }
  88. }
  89. }
  90. ?>