Skip to content

Commit a4a43cc

Browse files
authored
Merge pull request Eleirbag89#282 from siamakdals/master
Add my_chat_member update type
2 parents 19017fa + 9d384f1 commit a4a43cc

File tree

1 file changed

+79
-35
lines changed

1 file changed

+79
-35
lines changed

Telegram.php

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ class Telegram
7979
* Constant for type Left Chat Member.
8080
*/
8181
const LEFT_CHAT_MEMBER = 'left_chat_member';
82+
/**
83+
* Constant for type My Chat Member.
84+
*/
85+
const MY_CHAT_MEMBER = 'my_chat_member';
8286

8387
private $bot_token = '';
8488
private $data = [];
8589
private $updates = [];
8690
private $log_errors;
8791
private $proxy;
92+
private $update_type;
8893

8994
/// Class constructor
9095

@@ -114,7 +119,7 @@ public function __construct($bot_token, $log_errors = true, array $proxy = [])
114119
*/
115120
public function endpoint($api, array $content, $post = true)
116121
{
117-
$url = 'https://api.telegram.org/bot'.$this->bot_token.'/'.$api;
122+
$url = 'https://api.telegram.org/bot' . $this->bot_token . '/' . $api;
118123
if ($post) {
119124
$reply = $this->sendAPIRequest($url, $content);
120125
} else {
@@ -736,7 +741,7 @@ public function stopPoll(array $content)
736741
*/
737742
public function downloadFile($telegram_file_path, $local_file_path)
738743
{
739-
$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;
740745
$in = fopen($file_url, 'rb');
741746
$out = fopen($local_file_path, 'wb');
742747

@@ -838,22 +843,34 @@ public function Caption()
838843
* \return the String users's chat_id.
839844
*/
840845
public function ChatID()
846+
{
847+
$chat = $this->Chat();
848+
return $chat['id'];
849+
}
850+
851+
/**
852+
* \return the Array chat.
853+
*/
854+
public function Chat()
841855
{
842856
$type = $this->getUpdateType();
843857
if ($type == self::CALLBACK_QUERY) {
844-
return @$this->data['callback_query']['message']['chat']['id'];
858+
return @$this->data['callback_query']['message']['chat'];
845859
}
846860
if ($type == self::CHANNEL_POST) {
847-
return @$this->data['channel_post']['chat']['id'];
861+
return @$this->data['channel_post']['chat'];
848862
}
849863
if ($type == self::EDITED_MESSAGE) {
850-
return @$this->data['edited_message']['chat']['id'];
864+
return @$this->data['edited_message']['chat'];
851865
}
852866
if ($type == self::INLINE_QUERY) {
853-
return @$this->data['inline_query']['from']['id'];
867+
return @$this->data['inline_query']['from'];
868+
}
869+
if ($type == self::MY_CHAT_MEMBER) {
870+
return @$this->data['my_chat_member']['chat'];
854871
}
855872

856-
return $this->data['message']['chat']['id'];
873+
return $this->data['message']['chat'];
857874
}
858875

859876
/// Get the message_id of the current message
@@ -1095,6 +1112,7 @@ public function messageFromGroup()
10951112
}
10961113

10971114
/// Get the contact phone number
1115+
10981116
/**
10991117
* \return a String of the contact phone number.
11001118
*/
@@ -1133,10 +1151,10 @@ public function messageFromGroupTitle()
11331151
public function buildKeyBoard(array $options, $onetime = false, $resize = false, $selective = true)
11341152
{
11351153
$replyMarkup = [
1136-
'keyboard' => $options,
1154+
'keyboard' => $options,
11371155
'one_time_keyboard' => $onetime,
1138-
'resize_keyboard' => $resize,
1139-
'selective' => $selective,
1156+
'resize_keyboard' => $resize,
1157+
'selective' => $selective,
11401158
];
11411159
$encodedMarkup = json_encode($replyMarkup, true);
11421160

@@ -1179,7 +1197,8 @@ public function buildInlineKeyboardButton(
11791197
$switch_inline_query_current_chat = null,
11801198
$callback_game = '',
11811199
$pay = ''
1182-
) {
1200+
)
1201+
{
11831202
$replyMarkup = [
11841203
'text' => $text,
11851204
];
@@ -1211,8 +1230,8 @@ public function buildInlineKeyboardButton(
12111230
public function buildKeyboardButton($text, $request_contact = false, $request_location = false)
12121231
{
12131232
$replyMarkup = [
1214-
'text' => $text,
1215-
'request_contact' => $request_contact,
1233+
'text' => $text,
1234+
'request_contact' => $request_contact,
12161235
'request_location' => $request_location,
12171236
];
12181237

@@ -1229,7 +1248,7 @@ public function buildKeyBoardHide($selective = true)
12291248
{
12301249
$replyMarkup = [
12311250
'remove_keyboard' => true,
1232-
'selective' => $selective,
1251+
'selective' => $selective,
12331252
];
12341253
$encodedMarkup = json_encode($replyMarkup, true);
12351254

@@ -1245,7 +1264,7 @@ public function buildForceReply($selective = true)
12451264
{
12461265
$replyMarkup = [
12471266
'force_reply' => true,
1248-
'selective' => $selective,
1267+
'selective' => $selective,
12491268
];
12501269
$encodedMarkup = json_encode($replyMarkup, true);
12511270

@@ -1692,57 +1711,82 @@ public function serveUpdate($update)
16921711
*/
16931712
public function getUpdateType()
16941713
{
1714+
if ($this->update_type) {
1715+
return $this->update_type;
1716+
}
1717+
16951718
$update = $this->data;
16961719
if (isset($update['inline_query'])) {
1697-
return self::INLINE_QUERY;
1720+
$this->update_type = self::INLINE_QUERY;
1721+
return $this->update_type;
16981722
}
16991723
if (isset($update['callback_query'])) {
1700-
return self::CALLBACK_QUERY;
1724+
$this->update_type = self::CALLBACK_QUERY;
1725+
return $this->update_type;
17011726
}
17021727
if (isset($update['edited_message'])) {
1703-
return self::EDITED_MESSAGE;
1728+
$this->update_type = self::EDITED_MESSAGE;
1729+
return $this->update_type;
17041730
}
17051731
if (isset($update['message']['text'])) {
1706-
return self::MESSAGE;
1732+
$this->update_type = self::MESSAGE;
1733+
return $this->update_type;
17071734
}
17081735
if (isset($update['message']['photo'])) {
1709-
return self::PHOTO;
1736+
$this->update_type = self::PHOTO;
1737+
return $this->update_type;
17101738
}
17111739
if (isset($update['message']['video'])) {
1712-
return self::VIDEO;
1740+
$this->update_type = self::VIDEO;
1741+
return $this->update_type;
17131742
}
17141743
if (isset($update['message']['audio'])) {
1715-
return self::AUDIO;
1744+
$this->update_type = self::AUDIO;
1745+
return $this->update_type;
17161746
}
17171747
if (isset($update['message']['voice'])) {
1718-
return self::VOICE;
1748+
$this->update_type = self::VOICE;
1749+
return $this->update_type;
17191750
}
17201751
if (isset($update['message']['contact'])) {
1721-
return self::CONTACT;
1752+
$this->update_type = self::CONTACT;
1753+
return $this->update_type;
17221754
}
17231755
if (isset($update['message']['location'])) {
1724-
return self::LOCATION;
1756+
$this->update_type = self::LOCATION;
1757+
return $this->update_type;
17251758
}
17261759
if (isset($update['message']['reply_to_message'])) {
1727-
return self::REPLY;
1760+
$this->update_type = self::REPLY;
1761+
return $this->update_type;
17281762
}
17291763
if (isset($update['message']['animation'])) {
1730-
return self::ANIMATION;
1764+
$this->update_type = self::ANIMATION;
1765+
return $this->update_type;
17311766
}
17321767
if (isset($update['message']['sticker'])) {
1733-
return self::STICKER;
1768+
$this->update_type = self::STICKER;
1769+
return $this->update_type;
17341770
}
17351771
if (isset($update['message']['document'])) {
1736-
return self::DOCUMENT;
1772+
$this->update_type = self::DOCUMENT;
1773+
return $this->update_type;
17371774
}
17381775
if (isset($update['message']['new_chat_member'])) {
1739-
return self::NEW_CHAT_MEMBER;
1776+
$this->update_type = self::NEW_CHAT_MEMBER;
1777+
return $this->update_type;
17401778
}
17411779
if (isset($update['message']['left_chat_member'])) {
1742-
return self::LEFT_CHAT_MEMBER;
1780+
$this->update_type = self::LEFT_CHAT_MEMBER;
1781+
return $this->update_type;
1782+
}
1783+
if (isset($update['my_chat_member'])) {
1784+
$this->update_type = self::MY_CHAT_MEMBER;
1785+
return $this->update_type;
17431786
}
17441787
if (isset($update['channel_post'])) {
1745-
return self::CHANNEL_POST;
1788+
$this->update_type = self::CHANNEL_POST;
1789+
return $this->update_type;
17461790
}
17471791

17481792
return false;
@@ -1751,7 +1795,7 @@ public function getUpdateType()
17511795
private function sendAPIRequest($url, array $content, $post = true)
17521796
{
17531797
if (isset($content['chat_id'])) {
1754-
$url = $url.'?chat_id='.$content['chat_id'];
1798+
$url = $url . '?chat_id=' . $content['chat_id'];
17551799
unset($content['chat_id']);
17561800
}
17571801
$ch = curl_init();
@@ -1807,7 +1851,7 @@ private function sendAPIRequest($url, array $content, $post = true)
18071851
function curl_file_create($filename, $mimetype = '', $postname = '')
18081852
{
18091853
return "@$filename;filename="
1810-
.($postname ?: basename($filename))
1811-
.($mimetype ? ";type=$mimetype" : '');
1854+
. ($postname ?: basename($filename))
1855+
. ($mimetype ? ";type=$mimetype" : '');
18121856
}
18131857
}

0 commit comments

Comments
 (0)