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
|
1 |
|
public function __construct(Telegram $app) |
36
|
|
|
{ |
37
|
1 |
|
$this->app = $app; |
38
|
1 |
|
} |
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
|
1 |
|
public function handle(Request $request, $limit = null, $timeout = null) |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
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
|
1 |
|
$offset = 0; |
64
|
|
|
|
65
|
1 |
|
if (DB::isDbConnected()) { |
66
|
|
|
// Get last update id from the database |
67
|
1 |
|
$last_update = DB::selectTelegramUpdate(1); |
68
|
1 |
|
$last_update = reset($last_update); |
69
|
|
|
|
70
|
1 |
|
$this->app->last_update_id = isset($last_update['id']) ? $last_update['id'] : null; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
if ($this->app->last_update_id !== null) { |
74
|
|
|
$offset = $this->app->last_update_id + 1; //As explained in the telegram bot API documentation |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$response = Client::getUpdates( |
78
|
|
|
[ |
79
|
1 |
|
'offset' => $offset, |
80
|
1 |
|
'limit' => $limit, |
81
|
1 |
|
'timeout' => $timeout, |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
|
85
|
1 |
|
if ($response->isOk()) { |
86
|
1 |
|
$results = $response->getResult(); |
87
|
|
|
|
88
|
|
|
// Process all updates |
89
|
|
|
/** @var \Longman\TelegramBot\Entities\Update $result */ |
90
|
1 |
|
foreach ($results as $result) { |
91
|
|
|
$this->app->processUpdate($result); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) { |
|
|
|
|
95
|
|
|
// Mark update(s) as read after handling |
96
|
|
|
Client::getUpdates( |
97
|
|
|
[ |
98
|
|
|
'offset' => $this->app->last_update_id + 1, |
99
|
|
|
'limit' => 1, |
100
|
|
|
'timeout' => $timeout, |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $response; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Adding a
@return
annotation 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.