1 | <?php |
||
24 | class Bot |
||
25 | { |
||
26 | /** |
||
27 | * @var Bot |
||
28 | */ |
||
29 | private static $instance; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $token; |
||
35 | |||
36 | /** |
||
37 | * Bot constructor. |
||
38 | * |
||
39 | * @param null|string $token |
||
40 | * @param array $options |
||
41 | * |
||
42 | * @throws TelegramCoreException |
||
43 | */ |
||
44 | 5 | public function __construct($token = null, $options = []) |
|
45 | { |
||
46 | 5 | $this->token = $token ?: getenv('TELEGRAM_TOKEN'); |
|
47 | 5 | if (!$this->token) { |
|
48 | 1 | throw new TelegramCoreException('Token must be defined'); |
|
49 | } |
||
50 | $baseOptions = [ |
||
51 | 4 | 'base_uri' => sprintf('https://api.telegram.org/bot%s/', $token), |
|
52 | 'verify' => false, |
||
53 | 'http_errors' => false, |
||
54 | ]; |
||
55 | 4 | $this->client = new Client(array_merge($baseOptions, $options)); |
|
|
|||
56 | 4 | } |
|
57 | |||
58 | /** |
||
59 | * @param string $method |
||
60 | * @param array $params |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | 4 | public function call($method, $params = []) |
|
73 | |||
74 | 1 | public function __call($name, $arguments) |
|
75 | { |
||
76 | 1 | return $this->call($name, $arguments); |
|
77 | } |
||
78 | |||
79 | 4 | private function prepareResponse(ResponseInterface $response) |
|
80 | { |
||
81 | 4 | $json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true); |
|
82 | 4 | if (array_get($json, 'ok') == false) { |
|
83 | 1 | throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'), |
|
84 | 1 | array_get($json, 'error_code')); |
|
85 | } |
||
86 | |||
87 | 3 | return $json; |
|
88 | } |
||
89 | |||
90 | 3 | private function buildResponse(array $response) |
|
94 | |||
95 | 2 | public function getMe() |
|
96 | { |
||
97 | 2 | return new User($this->call('getMe')); |
|
99 | |||
100 | /** |
||
101 | * @param MessageEntry $message |
||
102 | * @return Message |
||
103 | */ |
||
104 | 2 | public function sendMessage(MessageEntry $message): Message |
|
108 | |||
109 | public function sendTextMessage($to, $text) |
||
115 | |||
116 | public function testCall() |
||
130 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: