TelegramService.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Services;
  3. use \Curl\Curl;
  4. class TelegramService {
  5. protected $api;
  6. public function __construct()
  7. {
  8. $this->api = 'https://api.telegram.org/bot' . config('v2board.telegram_bot_token') . '/';
  9. }
  10. public function sendMessage(int $chatId, string $text, string $parseMode = '')
  11. {
  12. $this->request('sendMessage', [
  13. 'chat_id' => $chatId,
  14. 'text' => $text,
  15. 'parse_mode' => $parseMode
  16. ]);
  17. }
  18. public function getMe()
  19. {
  20. $response = $this->request('getMe');
  21. if (!$response->ok) {
  22. return false;
  23. }
  24. return $response;
  25. }
  26. public function setWebhook(string $url)
  27. {
  28. $response = $this->request('setWebhook', [
  29. 'url' => $url
  30. ]);
  31. if (!$response->ok) {
  32. return false;
  33. }
  34. return $response;
  35. }
  36. private function request(string $method, array $params = [])
  37. {
  38. $curl = new Curl();
  39. $curl->get($this->api . $method, http_build_query($params));
  40. $curl->close();
  41. return $curl->response;
  42. }
  43. }