Whatsapp.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\components;
  3. use Exception;
  4. class Whatsapp {
  5. private $_access_token = "";
  6. public $base_url = "https://graph.facebook.com/%s/%s/%s";
  7. public $components = [];
  8. public $language_code = "es_MX";
  9. public $message_product = "whatsapp";
  10. public $_from = "";
  11. public $_to = "";
  12. public $version = "v14.0";
  13. public $template_name = "";
  14. public function __construct() {
  15. $this->_access_token = "";
  16. $this->_from = "";
  17. $this->_to = "";
  18. $this->template_name = "";
  19. $this->components = [];
  20. }
  21. public function set_access_token($token) {
  22. $this->_access_token = $token;
  23. return $this;
  24. }
  25. public function msg_from($number = "") {
  26. if (trim($number) == "") {
  27. throw new Exception("From is required");
  28. }
  29. $this->_from = $number;
  30. return $this;
  31. }
  32. public function msg_to($number = "") {
  33. if (trim($number) == "") {
  34. throw new Exception("To is required");
  35. }
  36. $this->_to = $number;
  37. return $this;
  38. }
  39. public function template($name) {
  40. if (trim($name) == "") {
  41. // throw error
  42. return;
  43. }
  44. $this->template_name = $name;
  45. return $this;
  46. }
  47. public function add_component($type, $params = []) {
  48. $this->components[$type] = $params;
  49. return $this;
  50. }
  51. public function add_body_param($text, $type = "text") {
  52. if (!isset($this->components["body"])) {
  53. $this->components["body"] = [];
  54. }
  55. $dict = [
  56. "type" => $type,
  57. "text" => $text
  58. ];
  59. $this->components["body"][] = $dict;
  60. return $this;
  61. }
  62. public function send_template_message() {
  63. if (!$this->_to) {
  64. throw new Exception("To is not defined");
  65. }
  66. if (!$this->_from) {
  67. throw new Exception("From is not defined");
  68. }
  69. if (!$this->template_name) {
  70. throw new Exception("Template Name is not defined");
  71. }
  72. $url = sprintf($this->base_url, $this->version, $this->_from, "messages");
  73. $headers = [
  74. 'Authorization: Bearer ' . $this->_access_token,
  75. 'Content-Type: application/json'
  76. ];
  77. $components = [];
  78. if (isset($this->components['header'])) {
  79. $components[] = [
  80. "type" => "header",
  81. "parameters" => $this->components["header"]
  82. ];
  83. }
  84. if (isset($this->components['body'])) {
  85. $components[] = [
  86. "type" => "body",
  87. "parameters" => $this->components["body"]
  88. ];
  89. }
  90. if (isset($this->components['footer'])) {
  91. $components[] = [
  92. "type" => "footer",
  93. "parameters" => $this->components["footer"]
  94. ];
  95. }
  96. $body = [
  97. "messaging_product" => $this->message_product,
  98. "to" => $this->_to,
  99. "type" => "template",
  100. "template" => [
  101. "name" => $this->template_name,
  102. "language" => [
  103. "code" => $this->language_code
  104. ],
  105. "components" => $components
  106. ]
  107. ];
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_URL, $url);
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
  112. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  113. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  114. $resultado = curl_exec($ch);
  115. curl_close($ch);
  116. try {
  117. $json = json_decode($resultado);
  118. return $json;
  119. } catch(Exception $e) {
  120. return null;
  121. }
  122. }
  123. }