Skip to content

Commit 9d384f1

Browse files
committed
Better getUpdateType method
1 parent 227a9a9 commit 9d384f1

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

Telegram.php

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Telegram
8989
private $updates = [];
9090
private $log_errors;
9191
private $proxy;
92+
private $update_type;
9293

9394
/// Class constructor
9495

@@ -118,7 +119,7 @@ public function __construct($bot_token, $log_errors = true, array $proxy = [])
118119
*/
119120
public function endpoint($api, array $content, $post = true)
120121
{
121-
$url = 'https://api.telegram.org/bot'.$this->bot_token.'/'.$api;
122+
$url = 'https://api.telegram.org/bot' . $this->bot_token . '/' . $api;
122123
if ($post) {
123124
$reply = $this->sendAPIRequest($url, $content);
124125
} else {
@@ -740,7 +741,7 @@ public function stopPoll(array $content)
740741
*/
741742
public function downloadFile($telegram_file_path, $local_file_path)
742743
{
743-
$file_url = 'https://api.telegram.org/file/bot'.$this->bot_token.'/'.$telegram_file_path;
744+
$file_url = 'https://api.telegram.org/file/bot' . $this->bot_token . '/' . $telegram_file_path;
744745
$in = fopen($file_url, 'rb');
745746
$out = fopen($local_file_path, 'wb');
746747

@@ -846,6 +847,7 @@ public function ChatID()
846847
$chat = $this->Chat();
847848
return $chat['id'];
848849
}
850+
849851
/**
850852
* \return the Array chat.
851853
*/
@@ -1110,6 +1112,7 @@ public function messageFromGroup()
11101112
}
11111113

11121114
/// Get the contact phone number
1115+
11131116
/**
11141117
* \return a String of the contact phone number.
11151118
*/
@@ -1148,10 +1151,10 @@ public function messageFromGroupTitle()
11481151
public function buildKeyBoard(array $options, $onetime = false, $resize = false, $selective = true)
11491152
{
11501153
$replyMarkup = [
1151-
'keyboard' => $options,
1154+
'keyboard' => $options,
11521155
'one_time_keyboard' => $onetime,
1153-
'resize_keyboard' => $resize,
1154-
'selective' => $selective,
1156+
'resize_keyboard' => $resize,
1157+
'selective' => $selective,
11551158
];
11561159
$encodedMarkup = json_encode($replyMarkup, true);
11571160

@@ -1194,7 +1197,8 @@ public function buildInlineKeyboardButton(
11941197
$switch_inline_query_current_chat = null,
11951198
$callback_game = '',
11961199
$pay = ''
1197-
) {
1200+
)
1201+
{
11981202
$replyMarkup = [
11991203
'text' => $text,
12001204
];
@@ -1226,8 +1230,8 @@ public function buildInlineKeyboardButton(
12261230
public function buildKeyboardButton($text, $request_contact = false, $request_location = false)
12271231
{
12281232
$replyMarkup = [
1229-
'text' => $text,
1230-
'request_contact' => $request_contact,
1233+
'text' => $text,
1234+
'request_contact' => $request_contact,
12311235
'request_location' => $request_location,
12321236
];
12331237

@@ -1244,7 +1248,7 @@ public function buildKeyBoardHide($selective = true)
12441248
{
12451249
$replyMarkup = [
12461250
'remove_keyboard' => true,
1247-
'selective' => $selective,
1251+
'selective' => $selective,
12481252
];
12491253
$encodedMarkup = json_encode($replyMarkup, true);
12501254

@@ -1260,7 +1264,7 @@ public function buildForceReply($selective = true)
12601264
{
12611265
$replyMarkup = [
12621266
'force_reply' => true,
1263-
'selective' => $selective,
1267+
'selective' => $selective,
12641268
];
12651269
$encodedMarkup = json_encode($replyMarkup, true);
12661270

@@ -1707,60 +1711,82 @@ public function serveUpdate($update)
17071711
*/
17081712
public function getUpdateType()
17091713
{
1714+
if ($this->update_type) {
1715+
return $this->update_type;
1716+
}
1717+
17101718
$update = $this->data;
17111719
if (isset($update['inline_query'])) {
1712-
return self::INLINE_QUERY;
1720+
$this->update_type = self::INLINE_QUERY;
1721+
return $this->update_type;
17131722
}
17141723
if (isset($update['callback_query'])) {
1715-
return self::CALLBACK_QUERY;
1724+
$this->update_type = self::CALLBACK_QUERY;
1725+
return $this->update_type;
17161726
}
17171727
if (isset($update['edited_message'])) {
1718-
return self::EDITED_MESSAGE;
1728+
$this->update_type = self::EDITED_MESSAGE;
1729+
return $this->update_type;
17191730
}
17201731
if (isset($update['message']['text'])) {
1721-
return self::MESSAGE;
1732+
$this->update_type = self::MESSAGE;
1733+
return $this->update_type;
17221734
}
17231735
if (isset($update['message']['photo'])) {
1724-
return self::PHOTO;
1736+
$this->update_type = self::PHOTO;
1737+
return $this->update_type;
17251738
}
17261739
if (isset($update['message']['video'])) {
1727-
return self::VIDEO;
1740+
$this->update_type = self::VIDEO;
1741+
return $this->update_type;
17281742
}
17291743
if (isset($update['message']['audio'])) {
1730-
return self::AUDIO;
1744+
$this->update_type = self::AUDIO;
1745+
return $this->update_type;
17311746
}
17321747
if (isset($update['message']['voice'])) {
1733-
return self::VOICE;
1748+
$this->update_type = self::VOICE;
1749+
return $this->update_type;
17341750
}
17351751
if (isset($update['message']['contact'])) {
1736-
return self::CONTACT;
1752+
$this->update_type = self::CONTACT;
1753+
return $this->update_type;
17371754
}
17381755
if (isset($update['message']['location'])) {
1739-
return self::LOCATION;
1756+
$this->update_type = self::LOCATION;
1757+
return $this->update_type;
17401758
}
17411759
if (isset($update['message']['reply_to_message'])) {
1742-
return self::REPLY;
1760+
$this->update_type = self::REPLY;
1761+
return $this->update_type;
17431762
}
17441763
if (isset($update['message']['animation'])) {
1745-
return self::ANIMATION;
1764+
$this->update_type = self::ANIMATION;
1765+
return $this->update_type;
17461766
}
17471767
if (isset($update['message']['sticker'])) {
1748-
return self::STICKER;
1768+
$this->update_type = self::STICKER;
1769+
return $this->update_type;
17491770
}
17501771
if (isset($update['message']['document'])) {
1751-
return self::DOCUMENT;
1772+
$this->update_type = self::DOCUMENT;
1773+
return $this->update_type;
17521774
}
17531775
if (isset($update['message']['new_chat_member'])) {
1754-
return self::NEW_CHAT_MEMBER;
1776+
$this->update_type = self::NEW_CHAT_MEMBER;
1777+
return $this->update_type;
17551778
}
17561779
if (isset($update['message']['left_chat_member'])) {
1757-
return self::LEFT_CHAT_MEMBER;
1780+
$this->update_type = self::LEFT_CHAT_MEMBER;
1781+
return $this->update_type;
17581782
}
17591783
if (isset($update['my_chat_member'])) {
1760-
return self::MY_CHAT_MEMBER;
1784+
$this->update_type = self::MY_CHAT_MEMBER;
1785+
return $this->update_type;
17611786
}
17621787
if (isset($update['channel_post'])) {
1763-
return self::CHANNEL_POST;
1788+
$this->update_type = self::CHANNEL_POST;
1789+
return $this->update_type;
17641790
}
17651791

17661792
return false;
@@ -1769,7 +1795,7 @@ public function getUpdateType()
17691795
private function sendAPIRequest($url, array $content, $post = true)
17701796
{
17711797
if (isset($content['chat_id'])) {
1772-
$url = $url.'?chat_id='.$content['chat_id'];
1798+
$url = $url . '?chat_id=' . $content['chat_id'];
17731799
unset($content['chat_id']);
17741800
}
17751801
$ch = curl_init();
@@ -1825,7 +1851,7 @@ private function sendAPIRequest($url, array $content, $post = true)
18251851
function curl_file_create($filename, $mimetype = '', $postname = '')
18261852
{
18271853
return "@$filename;filename="
1828-
.($postname ?: basename($filename))
1829-
.($mimetype ? ";type=$mimetype" : '');
1854+
. ($postname ?: basename($filename))
1855+
. ($mimetype ? ";type=$mimetype" : '');
18301856
}
18311857
}

0 commit comments

Comments
 (0)