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\Entry\MessageEntry; |
16
|
|
|
use Telegram\Exceptions\TelegramCoreException; |
17
|
|
|
use Telegram\Types\Message; |
18
|
|
|
use Telegram\Types\User; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Bot |
22
|
|
|
* @package Telegram |
23
|
|
|
*/ |
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 = []) |
65
|
|
|
{ |
66
|
4 |
|
$response = $this->prepareResponse($this->client |
67
|
4 |
|
->post($method, [ |
68
|
4 |
|
'form_params' => $params, |
69
|
|
|
])); |
70
|
|
|
|
71
|
3 |
|
return $this->buildResponse(array_get($response, 'result', [])); |
72
|
|
|
} |
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) |
91
|
|
|
{ |
92
|
3 |
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function getMe() |
96
|
|
|
{ |
97
|
2 |
|
return new User($this->call('getMe')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param MessageEntry $message |
102
|
|
|
* @return Message |
103
|
|
|
*/ |
104
|
2 |
|
public function sendMessage(MessageEntry $message): Message |
105
|
|
|
{ |
106
|
2 |
|
return new Message($this->call('sendMessage', $message->toArray())); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function sendTextMessage($to, $text) |
110
|
|
|
{ |
111
|
|
|
return $this->sendMessage(MessageEntry::create() |
112
|
|
|
->to($to) |
113
|
|
|
->text($text)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testCall() |
117
|
|
|
{ |
118
|
|
|
$replyMarkup = [ |
119
|
|
|
'keyboard' => [ |
120
|
|
|
["A", "B"], |
121
|
|
|
], |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
return $this->call('sendMessage', [ |
125
|
|
|
'chat_id' => \MessagesTest::CHAT_ID, |
126
|
|
|
'text' => 'test', |
127
|
|
|
'reply_markup' => json_encode($replyMarkup), |
128
|
|
|
]); |
129
|
|
|
} |
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: