1 | <?php |
||
23 | class Bot |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $token; |
||
29 | |||
30 | /** |
||
31 | * @var Client |
||
32 | */ |
||
33 | private $client; |
||
34 | |||
35 | /** |
||
36 | * Bot constructor. |
||
37 | * |
||
38 | * @param null|string $token |
||
39 | * @param array $options |
||
40 | * |
||
41 | * @throws TelegramCoreException |
||
42 | */ |
||
43 | 5 | public function __construct($token = null, $options = []) |
|
44 | { |
||
45 | 5 | $this->token = $token ?: getenv('TELEGRAM_TOKEN'); |
|
46 | 5 | if (!$this->token) { |
|
47 | 1 | throw new TelegramCoreException('Token must be defined'); |
|
48 | } |
||
49 | $baseOptions = [ |
||
50 | 4 | 'base_uri' => sprintf('https://api.telegram.org/bot%s/', $token), |
|
51 | 'verify' => false, |
||
52 | 'http_errors' => false, |
||
53 | ]; |
||
54 | 4 | $this->client = new Client(array_merge($baseOptions, $options)); |
|
55 | 4 | } |
|
56 | |||
57 | /** |
||
58 | * @param string $method |
||
59 | * @param array $params |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | 4 | public function call($method, $params = []) |
|
64 | { |
||
65 | 4 | $response = $this->prepareResponse($this->client |
|
66 | 4 | ->post($method, [ |
|
67 | 4 | 'form_params' => $params, |
|
68 | ])); |
||
69 | |||
70 | 3 | return $this->buildResponse(array_get($response, 'result', [])); |
|
71 | } |
||
72 | |||
73 | 1 | public function __call($name, $arguments) |
|
77 | |||
78 | 4 | private function prepareResponse(ResponseInterface $response) |
|
79 | { |
||
88 | |||
89 | 3 | private function buildResponse(array $response) |
|
93 | |||
94 | 2 | public function getMe() |
|
98 | |||
99 | /** |
||
100 | * @param MessageEntry $message |
||
101 | * @return Message |
||
102 | */ |
||
103 | 2 | public function sendMessage(MessageEntry $message): Message |
|
107 | |||
108 | 1 | public function sendTextMessage($to, $text) |
|
114 | } |