Telegram.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: solu
  5. * Date: 2019/3/26
  6. * Time: 5:38 PM
  7. */
  8. class Telegram {
  9. /**
  10. * @param $message
  11. * @throws Exception
  12. */
  13. public static function processMessage($message) {
  14. // process incoming message
  15. $message_id = $message['message_id'];
  16. $chat_id = $message['chat']['id'];
  17. if (isset($message['text'])) {
  18. // incoming text message
  19. $text = $message['text'];
  20. if (strpos($text, "/start") === 0) {
  21. self::apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
  22. 'keyboard' => array(array('Hello', 'Hi')),
  23. 'one_time_keyboard' => true,
  24. 'resize_keyboard' => true)));
  25. } else if ($text === "Hello" || $text === "Hi") {
  26. self::apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => $message['text']));
  27. } else {
  28. self::apiRequest("sendMessage", array('chat_id' => $chat_id, "reply_to_message_id" => $message_id, "text" => 'Cool'));
  29. }
  30. } else {
  31. self::apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'I understand only text messages'));
  32. }
  33. }
  34. /**
  35. * @param $method
  36. * @param $parameters
  37. * @return bool|mixed
  38. * @throws Exception
  39. * @return mixed
  40. */
  41. public static function apiRequestJson($method, $parameters) {
  42. if (!is_string($method)) {
  43. throw new Exception("Method name must be a string\n");
  44. }
  45. if (!$parameters) {
  46. $parameters = array();
  47. } else if (!is_array($parameters)) {
  48. throw new Exception("Parameters must be an array\n");
  49. }
  50. $parameters["method"] = $method;
  51. $handle = curl_init(TELEGRAM_API_URL);
  52. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  53. curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  54. curl_setopt($handle, CURLOPT_TIMEOUT, 60);
  55. curl_setopt($handle, CURLOPT_POST, true);
  56. curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
  57. curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
  58. return self::exec_curl_request($handle);
  59. }
  60. /**
  61. * @param $method
  62. * @param $parameters
  63. * @return mixed
  64. * @throws Exception
  65. */
  66. public static function apiRequest($method, $parameters) {
  67. if (!is_string($method)) {
  68. throw new Exception("Method name must be a string\n");
  69. }
  70. if (!$parameters) {
  71. $parameters = array();
  72. } else if (!is_array($parameters)) {
  73. throw new Exception("Parameters must be an array\n");
  74. }
  75. foreach ($parameters as $key => &$val) {
  76. // encoding to JSON array parameters, for example reply_markup
  77. if (!is_numeric($val) && !is_string($val)) {
  78. $val = json_encode($val);
  79. }
  80. }
  81. $url = TELEGRAM_API_URL.$method.'?'.http_build_query($parameters);
  82. $handle = curl_init($url);
  83. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  84. curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  85. curl_setopt($handle, CURLOPT_TIMEOUT, 60);
  86. return self::exec_curl_request($handle);
  87. }
  88. /**
  89. * @param $handle
  90. * @return bool|mixed
  91. * @throws Exception
  92. * @return mixed
  93. */
  94. public static function exec_curl_request($handle) {
  95. $response = curl_exec($handle);
  96. if ($response === false) {
  97. $errno = curl_errno($handle);
  98. $error = curl_error($handle);
  99. curl_close($handle);
  100. throw new Exception("Curl returned error $errno: $error\n");
  101. }
  102. $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
  103. curl_close($handle);
  104. if ($http_code >= 500) {
  105. // do not wat to DDOS server if something goes wrong
  106. sleep(10);
  107. return false;
  108. } else if ($http_code != 200) {
  109. $response = json_decode($response, true);
  110. throw new Exception("Request has failed with error {$response['error_code']}: {$response['description']}\n");
  111. } else {
  112. $response = json_decode($response, true);
  113. if (isset($response['description'])) {
  114. throw new Exception("Request was successful: {$response['description']}\n");
  115. }
  116. $response = $response['result'];
  117. }
  118. return $response;
  119. }
  120. }