v2board/app/Services/TelegramService.php

51 lines
1.1 KiB
PHP
Raw Normal View History

2020-05-17 15:23:39 +08:00
<?php
namespace App\Services;
use \Curl\Curl;
class TelegramService {
protected $api;
2020-05-17 16:00:28 +08:00
public function __construct(string $token = '')
2020-05-17 15:23:39 +08:00
{
2020-05-17 16:00:28 +08:00
$this->api = 'https://api.telegram.org/bot' . config('v2board.telegram_bot_token', $token) . '/';
2020-05-17 15:23:39 +08:00
}
public function sendMessage(int $chatId, string $text, string $parseMode = '')
{
$this->request('sendMessage', [
'chat_id' => $chatId,
'text' => $text,
'parse_mode' => $parseMode
]);
}
public function getMe()
{
$response = $this->request('getMe');
if (!$response->ok) {
return false;
}
return $response;
}
public function setWebhook(string $url)
{
$response = $this->request('setWebhook', [
'url' => $url
]);
if (!$response->ok) {
return false;
}
return $response;
}
private function request(string $method, array $params = [])
{
$curl = new Curl();
$curl->get($this->api . $method, http_build_query($params));
$curl->close();
return $curl->response;
}
}