|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the TelegramBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Console; |
|
12
|
|
|
|
|
13
|
|
|
use Longman\TelegramBot\DB; |
|
14
|
|
|
use Longman\TelegramBot\Http\Client; |
|
15
|
|
|
use Longman\TelegramBot\Http\Request; |
|
16
|
|
|
use Longman\TelegramBot\Http\Response; |
|
17
|
|
|
use Longman\TelegramBot\Telegram; |
|
18
|
|
|
|
|
19
|
|
|
class Kernel |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The application implementation. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Longman\TelegramBot\Telegram |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $app; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create a new HTTP kernel instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Longman\TelegramBot\Telegram $app |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Telegram $app) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->app = $app; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Handle an incoming HTTP request. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Longman\TelegramBot\Http\Request $request |
|
44
|
|
|
* |
|
45
|
|
|
* @param null $limit |
|
46
|
|
|
* @param null $timeout |
|
47
|
|
|
* @return \Longman\TelegramBot\Http\Response |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function handle(Request $request, $limit = null, $timeout = null) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
if (! DB::isDbConnected() && ! $this->app->getupdates_without_database) { |
|
54
|
|
|
return new Response( |
|
55
|
|
|
[ |
|
56
|
|
|
'ok' => false, |
|
57
|
|
|
'description' => 'getUpdates needs MySQL connection! (This can be overridden - see documentation)', |
|
58
|
|
|
], |
|
59
|
|
|
$this->app->getBotUsername() |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$offset = 0; |
|
64
|
|
|
|
|
65
|
|
|
// Take custom input into account. |
|
66
|
|
|
if ($custom_input = $this->app->getCustomInput()) { |
|
67
|
|
|
$response = new Response(json_decode($custom_input, true), $this->app->getBotUsername()); |
|
68
|
|
|
} else { |
|
69
|
|
|
if (DB::isDbConnected()) { |
|
70
|
|
|
// Get last update id from the database |
|
71
|
|
|
$last_update = DB::selectTelegramUpdate(1); |
|
72
|
|
|
$last_update = reset($last_update); |
|
73
|
|
|
|
|
74
|
|
|
$this->app->last_update_id = isset($last_update['id']) ? $last_update['id'] : null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->app->last_update_id !== null) { |
|
78
|
|
|
$offset = $this->app->last_update_id + 1; //As explained in the telegram bot API documentation |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$response = Client::getUpdates( |
|
82
|
|
|
[ |
|
83
|
|
|
'offset' => $offset, |
|
84
|
|
|
'limit' => $limit, |
|
85
|
|
|
'timeout' => $timeout, |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($response->isOk()) { |
|
91
|
|
|
$results = $response->getResult(); |
|
92
|
|
|
|
|
93
|
|
|
// Process all updates |
|
94
|
|
|
/** @var \Longman\TelegramBot\Entities\Update $result */ |
|
95
|
|
|
foreach ($results as $result) { |
|
96
|
|
|
$this->app->processUpdate($result); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) { |
|
100
|
|
|
// Mark update(s) as read after handling |
|
101
|
|
|
Client::getUpdates( |
|
102
|
|
|
[ |
|
103
|
|
|
'offset' => $this->app->last_update_id + 1, |
|
104
|
|
|
'limit' => 1, |
|
105
|
|
|
'timeout' => $timeout, |
|
106
|
|
|
] |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $response; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.