ViberApiClient.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. require_once 'ApiClient.php';
  3. class ViberApiClient extends ApiClient {
  4. protected $messages = array();
  5. protected $sender;
  6. public function __construct($api_key, $sender, $source = null) {
  7. $this->sender = $sender;
  8. parent::__construct($api_key, $source);
  9. }
  10. private function getStatus($endpoint) {
  11. try {
  12. $resp = $this->sendRequest($endpoint);
  13. } catch (\Exception $e) {
  14. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  15. return array('error' => $error);
  16. }
  17. $result = json_decode($resp, true);
  18. return $result;
  19. }
  20. public function getStatusByReference($reference) {
  21. return $this->getStatus('viber/reference/' . $reference);
  22. }
  23. public function getStatusById($message_id) {
  24. return $this->getStatus('viber/' . $message_id);
  25. }
  26. public function getPrices($tariff = NULL) {
  27. try {
  28. $resp = $this->sendRequest('viber/prices' . ($tariff !== NULL ? ('/' . $tariff) : ''));
  29. } catch (\Exception $e) {
  30. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  31. return array('error' => $error);
  32. }
  33. $result = json_decode($resp, true);
  34. return $result;
  35. }
  36. public function clearMessages() {
  37. $this->messages = array();
  38. }
  39. /**
  40. * param $to is an array of ['msisdn' => $msisdn, 'reference' => $reference], where 'reference' is optional
  41. * @param $to
  42. * @param $text
  43. * @param $alpha_name
  44. * @param array $viber_options
  45. * @param bool $is_promotional
  46. * @param string $callback_url
  47. */
  48. public function addMessage($to, $text, $viber_options = array(), $alpha_name = null, $is_promotional = true, $callback_url = '') {
  49. $alpha_name = $alpha_name ?: $this->sender;
  50. $message = array();
  51. $message['to'] = $to;
  52. $message['text'] = $text;
  53. $message['alpha_name'] = $alpha_name;
  54. if (!$is_promotional)
  55. $message['is_promotional'] = $is_promotional;
  56. if ($callback_url != '')
  57. $message['callback_url'] = $callback_url;
  58. if (count($viber_options) > 0)
  59. $message['options']['viber'] = $viber_options;
  60. $this->messages[] = $message;
  61. }
  62. public function getMessagesPrice($validity = 86400, $tariff = NULL) {
  63. return $this->sendMessages($validity, $tariff, true);
  64. }
  65. /**
  66. * @param int $validity
  67. * @param null $tariff
  68. * @param bool $only_price
  69. * @return mixed
  70. */
  71. public function sendMessages($validity = 86400, $tariff = NULL, $only_price = false) {
  72. if (count($this->messages) == 0)
  73. return array('error' => 'No messages to send');
  74. $message = array();
  75. $message['validity'] = $validity;
  76. if ($tariff !== NULL)
  77. $message['tariff'] = $tariff;
  78. $message['messages'] = $this->messages;
  79. $endpoint = $only_price ? 'viber/price' : 'viber/create';
  80. try {
  81. $resp = $this->sendRequest($endpoint, json_encode($message), 'PUT');
  82. } catch (\Exception $e) {
  83. $error = 'Request failed (code: ' . $e->getCode() . '): ' . $e->getMessage();
  84. return array('error' => $error);
  85. }
  86. $result = json_decode($resp, true);
  87. return $result;
  88. }
  89. }