GigilinE

Untitled

Sep 7th, 2021 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. function exec_curl_request($handle) {
  2. $response = curl_exec($handle);
  3.  
  4. if ($response === false) {
  5. $errno = curl_errno($handle);
  6. $error = curl_error($handle);
  7. error_log("Curl returned error $errno: $error\n");
  8. curl_close($handle);
  9. return false;
  10. }
  11. $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
  12. curl_close($handle);
  13. if ($http_code >= 500) {
  14. // do not wat to DDOS server if something goes wrong
  15. sleep(10);
  16. return false;
  17. } else if ($http_code != 200) {
  18. $response = json_decode($response, true);
  19. error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
  20. if ($http_code == 401) {
  21. throw new Exception('Invalid access token provided');
  22. }
  23. return false;
  24. } else {
  25. $response = json_decode($response, true);
  26. if (isset($response['description'])) {
  27. error_log("Request was successfull: {$response['description']}\n");
  28. }
  29. $response = $response['result'];
  30. }
  31. return $response;
  32. }
  33. function apiRequest($method, $parameters) {
  34. if (!is_string($method)) {
  35. error_log("Method name must be a string\n");
  36. return false;
  37. }
  38. if (!$parameters) {
  39. $parameters = array();
  40. } else if (!is_array($parameters)) {
  41. error_log("Parameters must be an array\n");
  42. return false;
  43. }
  44. foreach ($parameters as $key => &$val) {
  45. // encoding to JSON array parameters, for example reply_markup
  46. if (!is_numeric($val) && !is_string($val)) {
  47. $val = json_encode($val);
  48. }
  49. }
  50.  
  51. $url = 'https://api.telegram.org/bot'.BOT_TOKEN.'/' . $method . '?' . http_build_query($parameters);
  52. $handle = curl_init($url);
  53. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  54. curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  55. curl_setopt($handle, CURLOPT_TIMEOUT, 60);
  56. return exec_curl_request($handle);
  57. }
  58. function sendMessage($chat_id, $text_message) {
  59. $return_send = apiRequest("sendMessage", array(
  60. 'chat_id' => $chat_id,
  61. 'parse_mode' => "html",
  62. 'disable_web_page_preview' => true,
  63. "text" => $text_message
  64. ));
  65. return $return_send;
  66. }
  67. function sendPhotoMessage($chat_id, $text_message, $photo) {
  68. $result_send = apiRequest("sendPhoto", array(
  69. 'chat_id' => $chat_id,
  70. 'parse_mode' => "html",
  71. 'caption' => $text_message,
  72. 'photo' => $photo
  73. ));
  74. return $return_send;
  75. }
  76.  
Add Comment
Please, Sign In to add comment