1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: akeinhell |
5
|
|
|
* Date: 22.06.16 |
6
|
|
|
* Time: 12:03 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Telegram; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Telegram\Config\BaseConfig; |
15
|
|
|
use Telegram\Exceptions\TelegramCoreException; |
16
|
|
|
use Telegram\Types\User; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Bot |
20
|
|
|
* @package Telegram |
21
|
|
|
*/ |
22
|
|
|
class Bot |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
private $state; |
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BaseConfig |
30
|
|
|
*/ |
31
|
|
|
private $config; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Bot constructor. |
35
|
|
|
* |
36
|
|
|
* @param null|string $token |
37
|
|
|
* @param array $options |
38
|
|
|
* |
39
|
|
|
* @throws TelegramCoreException |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct($token = null, $options = []) |
42
|
|
|
{ |
43
|
3 |
|
$this->token = $token ?: getenv('TELEGRAM_TOKEN'); |
|
|
|
|
44
|
3 |
|
if (!$this->token) { |
45
|
1 |
|
throw new TelegramCoreException('Token must be defined'); |
46
|
|
|
} |
47
|
|
|
$baseOptions = [ |
48
|
2 |
|
'base_uri' => sprintf('https://api.telegram.org/bot%s/', $token), |
49
|
2 |
|
'verify' => false, |
50
|
2 |
|
'http_errors' => false, |
51
|
2 |
|
]; |
52
|
2 |
|
$this->client = new Client(array_merge($baseOptions, $options)); |
|
|
|
|
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $method |
57
|
|
|
* @param array $params |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
2 |
|
public function call($method, $params = []) |
62
|
|
|
{ |
63
|
2 |
|
$response = $this->prepareResponse($this->client |
64
|
2 |
|
->post($method, [ |
65
|
2 |
|
'form_data' => $params, |
66
|
2 |
|
])); |
67
|
|
|
|
68
|
1 |
|
return $this->buildResponse(array_get($response, 'result', [])); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function __call($name, $arguments) |
72
|
|
|
{ |
73
|
1 |
|
return $this->call($name, $arguments); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
private function prepareResponse(ResponseInterface $response) |
77
|
|
|
{ |
78
|
2 |
|
$json = \GuzzleHttp\json_decode($response->getBody()->getContents(), true); |
79
|
2 |
|
if (array_get($json, 'ok') == false) { |
80
|
1 |
|
throw new TelegramCoreException(array_get($json, 'description', 'error') . array_get($json, 'error_code'), array_get($json, 'error_code')); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $json; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
private function buildResponse(array $response) |
87
|
|
|
{ |
88
|
1 |
|
return $response; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getMe() |
92
|
|
|
{ |
93
|
1 |
|
return new User($this->call('getMe')); |
94
|
|
|
} |
95
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.