From 884232aec3a988f4e177fbd5a254750d78925b79 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 23 Dec 2016 21:06:38 +0100 Subject: [PATCH] random_ids are now computed automagically, added methods to ease working with peers and updated docs --- README.md | 15 +++- build_docs.php | 8 +- .../API_docs/constructors/encryptedMessage.md | 3 +- .../constructors/encryptedMessageService.md | 3 +- docs/API_docs/constructors/index.md | 6 +- docs/API_docs/constructors/updateMessageID.md | 3 +- docs/API_docs/methods/index.md | 20 ++--- .../methods/messages_forwardMessage.md | 3 +- .../methods/messages_forwardMessages.md | 3 +- .../methods/messages_requestEncryption.md | 3 +- .../methods/messages_sendEncrypted.md | 3 +- .../methods/messages_sendEncryptedFile.md | 3 +- .../methods/messages_sendEncryptedService.md | 3 +- .../methods/messages_sendInlineBotResult.md | 3 +- docs/API_docs/methods/messages_sendMedia.md | 3 +- docs/API_docs/methods/messages_sendMessage.md | 3 +- docs/API_docs/methods/messages_startBot.md | 3 +- docs/index.md | 15 +++- .../MTProtoTools/PeerHandler.php | 81 ++++++++++++++++++- src/danog/MadelineProto/TL/TL.php | 17 ++++ testing.php | 27 ++++--- 21 files changed, 167 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 7ffa6bde..9e2ec079 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' var_dump($sentMessage); ``` -The API class also provides some wrapper methods for logging in as a bot or as a normal user: +The API class also provides some wrapper methods for logging in as a bot or as a normal user, and for getting inputPeer constructors to use in sendMessage and other methods: ``` $sentCode = $MadelineProto->phone_login($number); // Send code @@ -213,6 +213,19 @@ for ($x = 0; $x < $sentCode['type']['length']; $x++) { $authorization = $MadelineProto->complete_phone_login($code); // Complete authorization var_dump($authorization); +var_dump($MadelineProto->API->resolve_username('@Palmas2012')); // Always use this method to resolve usernames, but you won't need to call this to get info about peers, as get_peer and get_input_peer will call it for you if needed + +$mention = $MadelineProto->API->get_peer('@veetaw'); // Returns an object of type User or Chat +$mention = $MadelineProto->API->constructor2inputpeer($mention); // Converts an object of type User or Chat to an object of type inputPeer + +$message = "I've installed MadelineProto!"; +foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { + $peer = $MadelineProto->API->get_input_peer($peer); + $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); + var_dump($sentMessage); +} + +// The above works with bots too $authorization = $MadelineProto->bot_login($token); // Note that every time you login as a bot or as a user MadelineProto will logout first, so now MadelineProto is logged in as the bot with token $token, not as the user with number $number var_dump($authorization); ``` diff --git a/build_docs.php b/build_docs.php index 1ec76959..d928deb0 100755 --- a/build_docs.php +++ b/build_docs.php @@ -72,7 +72,7 @@ foreach ($TL->methods->method as $key => $method) { $params = ''; foreach ($TL->methods->params[$key] as $param) { - if ($param['name'] == 'flags') { + if (in_array($param['name'], ['flags', 'random_id'])) { continue; } $stype = 'type'; @@ -110,7 +110,7 @@ foreach ($TL->methods->method as $key => $method) { |----------|:-------------:|---------:| '; foreach ($TL->methods->params[$key] as $param) { - if ($param['name'] == 'flags') { + if (in_array($param['name'], ['flags', 'random_id'])) { continue; } $ptype = str_replace('.', '_', $param[isset($param['subtype']) ? 'subtype' : 'type']); @@ -209,7 +209,7 @@ foreach ($TL->constructors->predicate as $key => $constructor) { $params = ''; foreach ($TL->constructors->params[$key] as $param) { - if ($param['name'] == 'flags') { + if (in_array($param['name'], ['flags', 'random_id'])) { continue; } $stype = 'type'; @@ -255,7 +255,7 @@ foreach ($TL->constructors->predicate as $key => $constructor) { '; $params = ''; foreach ($TL->constructors->params[$key] as $param) { - if ($param['name'] == 'flags') { + if (in_array($param['name'], ['flags', 'random_id'])) { continue; } $ptype = str_replace('.', '_', $param[isset($param['subtype']) ? 'subtype' : 'type']); diff --git a/docs/API_docs/constructors/encryptedMessage.md b/docs/API_docs/constructors/encryptedMessage.md index 7b49b600..6781f75e 100644 --- a/docs/API_docs/constructors/encryptedMessage.md +++ b/docs/API_docs/constructors/encryptedMessage.md @@ -11,7 +11,6 @@ description: encryptedMessage attributes, type and example | Name | Type | Required | |----------|:-------------:|---------:| -|random\_id|[long](../types/long.md) | Required| |chat\_id|[int](../types/int.md) | Required| |date|[int](../types/int.md) | Required| |bytes|[bytes](../types/bytes.md) | Required| @@ -25,5 +24,5 @@ description: encryptedMessage attributes, type and example ### Example: ``` -$encryptedMessage = ['_' => encryptedMessage', 'random_id' => long, 'chat_id' => int, 'date' => int, 'bytes' => bytes, 'file' => EncryptedFile, ]; +$encryptedMessage = ['_' => encryptedMessage', 'chat_id' => int, 'date' => int, 'bytes' => bytes, 'file' => EncryptedFile, ]; ``` \ No newline at end of file diff --git a/docs/API_docs/constructors/encryptedMessageService.md b/docs/API_docs/constructors/encryptedMessageService.md index 29d640e5..0f96d34e 100644 --- a/docs/API_docs/constructors/encryptedMessageService.md +++ b/docs/API_docs/constructors/encryptedMessageService.md @@ -11,7 +11,6 @@ description: encryptedMessageService attributes, type and example | Name | Type | Required | |----------|:-------------:|---------:| -|random\_id|[long](../types/long.md) | Required| |chat\_id|[int](../types/int.md) | Required| |date|[int](../types/int.md) | Required| |bytes|[bytes](../types/bytes.md) | Required| @@ -24,5 +23,5 @@ description: encryptedMessageService attributes, type and example ### Example: ``` -$encryptedMessageService = ['_' => encryptedMessageService', 'random_id' => long, 'chat_id' => int, 'date' => int, 'bytes' => bytes, ]; +$encryptedMessageService = ['_' => encryptedMessageService', 'chat_id' => int, 'date' => int, 'bytes' => bytes, ]; ``` \ No newline at end of file diff --git a/docs/API_docs/constructors/index.md b/docs/API_docs/constructors/index.md index 4a94a709..a9ffeb93 100644 --- a/docs/API_docs/constructors/index.md +++ b/docs/API_docs/constructors/index.md @@ -297,10 +297,10 @@ description: List of constructors

[$encryptedFileEmpty](../constructors/encryptedFileEmpty.md) = \[\]; *** -

[$encryptedMessage](../constructors/encryptedMessage.md) = \['random_id' => [long](../types/long.md), 'chat_id' => [int](../types/int.md), 'date' => [int](../types/int.md), 'bytes' => [bytes](../types/bytes.md), 'file' => [EncryptedFile](../types/EncryptedFile.md), \]; +

[$encryptedMessage](../constructors/encryptedMessage.md) = \['chat_id' => [int](../types/int.md), 'date' => [int](../types/int.md), 'bytes' => [bytes](../types/bytes.md), 'file' => [EncryptedFile](../types/EncryptedFile.md), \]; *** -

[$encryptedMessageService](../constructors/encryptedMessageService.md) = \['random_id' => [long](../types/long.md), 'chat_id' => [int](../types/int.md), 'date' => [int](../types/int.md), 'bytes' => [bytes](../types/bytes.md), \]; +

[$encryptedMessageService](../constructors/encryptedMessageService.md) = \['chat_id' => [int](../types/int.md), 'date' => [int](../types/int.md), 'bytes' => [bytes](../types/bytes.md), \]; ***

[$error](../constructors/error.md) = \['code' => [int](../types/int.md), 'text' => [string](../types/string.md), \]; @@ -1118,7 +1118,7 @@ description: List of constructors

[$updateInlineBotCallbackQuery](../constructors/updateInlineBotCallbackQuery.md) = \['query_id' => [long](../types/long.md), 'user_id' => [int](../types/int.md), 'msg_id' => [InputBotInlineMessageID](../types/InputBotInlineMessageID.md), 'chat_instance' => [long](../types/long.md), 'data' => [bytes](../types/bytes.md), 'game_short_name' => [string](../types/string.md), \]; *** -

[$updateMessageID](../constructors/updateMessageID.md) = \['id' => [int](../types/int.md), 'random_id' => [long](../types/long.md), \]; +

[$updateMessageID](../constructors/updateMessageID.md) = \['id' => [int](../types/int.md), \]; ***

[$updateNewAuthorization](../constructors/updateNewAuthorization.md) = \['auth_key_id' => [long](../types/long.md), 'date' => [int](../types/int.md), 'device' => [string](../types/string.md), 'location' => [string](../types/string.md), \]; diff --git a/docs/API_docs/constructors/updateMessageID.md b/docs/API_docs/constructors/updateMessageID.md index d40effde..65073e4a 100644 --- a/docs/API_docs/constructors/updateMessageID.md +++ b/docs/API_docs/constructors/updateMessageID.md @@ -12,7 +12,6 @@ description: updateMessageID attributes, type and example | Name | Type | Required | |----------|:-------------:|---------:| |id|[int](../types/int.md) | Required| -|random\_id|[long](../types/long.md) | Required| @@ -22,5 +21,5 @@ description: updateMessageID attributes, type and example ### Example: ``` -$updateMessageID = ['_' => updateMessageID', 'id' => int, 'random_id' => long, ]; +$updateMessageID = ['_' => updateMessageID', 'id' => int, ]; ``` \ No newline at end of file diff --git a/docs/API_docs/methods/index.md b/docs/API_docs/methods/index.md index a2332506..a942ab30 100644 --- a/docs/API_docs/methods/index.md +++ b/docs/API_docs/methods/index.md @@ -242,9 +242,9 @@ $MadelineProto->[messages->editMessage](messages_editMessage.md)(\['no_webpage' $MadelineProto->[messages->exportChatInvite](messages_exportChatInvite.md)(\['chat_id' => [int](../types/int.md), \]) == [$ExportedChatInvite](../types/ExportedChatInvite.md) -$MadelineProto->[messages->forwardMessage](messages_forwardMessage.md)(\['peer' => [InputPeer](../types/InputPeer.md), 'id' => [int](../types/int.md), 'random_id' => [long](../types/long.md), \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->forwardMessage](messages_forwardMessage.md)(\['peer' => [InputPeer](../types/InputPeer.md), 'id' => [int](../types/int.md), \]) == [$Updates](../types/Updates.md) -$MadelineProto->[messages->forwardMessages](messages_forwardMessages.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'with_my_score' => [Bool](../types/Bool.md), 'from_peer' => [InputPeer](../types/InputPeer.md), 'id' => \[[int](../types/int.md)\], 'random_id' => \[[long](../types/long.md)\], 'to_peer' => [InputPeer](../types/InputPeer.md), \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->forwardMessages](messages_forwardMessages.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'with_my_score' => [Bool](../types/Bool.md), 'from_peer' => [InputPeer](../types/InputPeer.md), 'id' => \[[int](../types/int.md)\], 'to_peer' => [InputPeer](../types/InputPeer.md), \]) == [$Updates](../types/Updates.md) $MadelineProto->[messages->getAllDrafts](messages_getAllDrafts.md)(\[\]) == [$Updates](../types/Updates.md) @@ -320,7 +320,7 @@ $MadelineProto->[messages->reorderStickerSets](messages_reorderStickerSets.md)(\ $MadelineProto->[messages->reportSpam](messages_reportSpam.md)(\['peer' => [InputPeer](../types/InputPeer.md), \]) == [$Bool](../types/Bool.md) -$MadelineProto->[messages->requestEncryption](messages_requestEncryption.md)(\['user_id' => [InputUser](../types/InputUser.md), 'random_id' => [int](../types/int.md), 'g_a' => [bytes](../types/bytes.md), \]) == [$EncryptedChat](../types/EncryptedChat.md) +$MadelineProto->[messages->requestEncryption](messages_requestEncryption.md)(\['user_id' => [InputUser](../types/InputUser.md), 'g_a' => [bytes](../types/bytes.md), \]) == [$EncryptedChat](../types/EncryptedChat.md) $MadelineProto->[messages->saveDraft](messages_saveDraft.md)(\['no_webpage' => [Bool](../types/Bool.md), 'reply_to_msg_id' => [int](../types/int.md), 'peer' => [InputPeer](../types/InputPeer.md), 'message' => [string](../types/string.md), 'entities' => \[[MessageEntity](../types/MessageEntity.md)\], \]) == [$Bool](../types/Bool.md) @@ -334,17 +334,17 @@ $MadelineProto->[messages->searchGifs](messages_searchGifs.md)(\['q' => [string] $MadelineProto->[messages->searchGlobal](messages_searchGlobal.md)(\['q' => [string](../types/string.md), 'offset_date' => [int](../types/int.md), 'offset_peer' => [InputPeer](../types/InputPeer.md), 'offset_id' => [int](../types/int.md), 'limit' => [int](../types/int.md), \]) == [$messages\_Messages](../types/messages_Messages.md) -$MadelineProto->[messages->sendEncrypted](messages_sendEncrypted.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'random_id' => [long](../types/long.md), 'data' => [bytes](../types/bytes.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) +$MadelineProto->[messages->sendEncrypted](messages_sendEncrypted.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'data' => [bytes](../types/bytes.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) -$MadelineProto->[messages->sendEncryptedFile](messages_sendEncryptedFile.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'random_id' => [long](../types/long.md), 'data' => [bytes](../types/bytes.md), 'file' => [InputEncryptedFile](../types/InputEncryptedFile.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) +$MadelineProto->[messages->sendEncryptedFile](messages_sendEncryptedFile.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'data' => [bytes](../types/bytes.md), 'file' => [InputEncryptedFile](../types/InputEncryptedFile.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) -$MadelineProto->[messages->sendEncryptedService](messages_sendEncryptedService.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'random_id' => [long](../types/long.md), 'data' => [bytes](../types/bytes.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) +$MadelineProto->[messages->sendEncryptedService](messages_sendEncryptedService.md)(\['peer' => [InputEncryptedChat](../types/InputEncryptedChat.md), 'data' => [bytes](../types/bytes.md), \]) == [$messages\_SentEncryptedMessage](../types/messages_SentEncryptedMessage.md) -$MadelineProto->[messages->sendInlineBotResult](messages_sendInlineBotResult.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'random_id' => [long](../types/long.md), 'query_id' => [long](../types/long.md), 'id' => [string](../types/string.md), \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->sendInlineBotResult](messages_sendInlineBotResult.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'query_id' => [long](../types/long.md), 'id' => [string](../types/string.md), \]) == [$Updates](../types/Updates.md) -$MadelineProto->[messages->sendMedia](messages_sendMedia.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'media' => [InputMedia](../types/InputMedia.md), 'random_id' => [long](../types/long.md), 'reply_markup' => [ReplyMarkup](../types/ReplyMarkup.md), \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->sendMedia](messages_sendMedia.md)(\['silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'media' => [InputMedia](../types/InputMedia.md), 'reply_markup' => [ReplyMarkup](../types/ReplyMarkup.md), \]) == [$Updates](../types/Updates.md) -$MadelineProto->[messages->sendMessage](messages_sendMessage.md)(\['no_webpage' => [Bool](../types/Bool.md), 'silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'message' => [string](../types/string.md), 'random_id' => [long](../types/long.md), 'reply_markup' => [ReplyMarkup](../types/ReplyMarkup.md), 'entities' => \[[MessageEntity](../types/MessageEntity.md)\], \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->sendMessage](messages_sendMessage.md)(\['no_webpage' => [Bool](../types/Bool.md), 'silent' => [Bool](../types/Bool.md), 'background' => [Bool](../types/Bool.md), 'clear_draft' => [Bool](../types/Bool.md), 'peer' => [InputPeer](../types/InputPeer.md), 'reply_to_msg_id' => [int](../types/int.md), 'message' => [string](../types/string.md), 'reply_markup' => [ReplyMarkup](../types/ReplyMarkup.md), 'entities' => \[[MessageEntity](../types/MessageEntity.md)\], \]) == [$Updates](../types/Updates.md) $MadelineProto->[messages->setBotCallbackAnswer](messages_setBotCallbackAnswer.md)(\['alert' => [Bool](../types/Bool.md), 'query_id' => [long](../types/long.md), 'message' => [string](../types/string.md), 'url' => [string](../types/string.md), \]) == [$Bool](../types/Bool.md) @@ -358,7 +358,7 @@ $MadelineProto->[messages->setInlineGameScore](messages_setInlineGameScore.md)(\ $MadelineProto->[messages->setTyping](messages_setTyping.md)(\['peer' => [InputPeer](../types/InputPeer.md), 'action' => [SendMessageAction](../types/SendMessageAction.md), \]) == [$Bool](../types/Bool.md) -$MadelineProto->[messages->startBot](messages_startBot.md)(\['bot' => [InputUser](../types/InputUser.md), 'peer' => [InputPeer](../types/InputPeer.md), 'random_id' => [long](../types/long.md), 'start_param' => [string](../types/string.md), \]) == [$Updates](../types/Updates.md) +$MadelineProto->[messages->startBot](messages_startBot.md)(\['bot' => [InputUser](../types/InputUser.md), 'peer' => [InputPeer](../types/InputPeer.md), 'start_param' => [string](../types/string.md), \]) == [$Updates](../types/Updates.md) $MadelineProto->[messages->toggleChatAdmins](messages_toggleChatAdmins.md)(\['chat_id' => [int](../types/int.md), 'enabled' => [Bool](../types/Bool.md), \]) == [$Updates](../types/Updates.md) diff --git a/docs/API_docs/methods/messages_forwardMessage.md b/docs/API_docs/methods/messages_forwardMessage.md index 67a129f6..e9fa210d 100644 --- a/docs/API_docs/methods/messages_forwardMessage.md +++ b/docs/API_docs/methods/messages_forwardMessage.md @@ -12,7 +12,6 @@ description: messages_forwardMessage parameters, return type and example |----------|:-------------:|---------:| |peer|[InputPeer](../types/InputPeer.md) | Required| |id|[int](../types/int.md) | Required| -|random\_id|[long](../types/long.md) | Required| ### Return type: [Updates](../types/Updates.md) @@ -35,5 +34,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->forwardMessage(['peer' => InputPeer, 'id' => int, 'random_id' => long, ]); +$Updates = $MadelineProto->messages->forwardMessage(['peer' => InputPeer, 'id' => int, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_forwardMessages.md b/docs/API_docs/methods/messages_forwardMessages.md index 85e1e792..58a27c80 100644 --- a/docs/API_docs/methods/messages_forwardMessages.md +++ b/docs/API_docs/methods/messages_forwardMessages.md @@ -15,7 +15,6 @@ description: messages_forwardMessages parameters, return type and example |with\_my\_score|[Bool](../types/Bool.md) | Optional| |from\_peer|[InputPeer](../types/InputPeer.md) | Required| |id|Array of [int](../types/int.md) | Required| -|random\_id|Array of [long](../types/long.md) | Required| |to\_peer|[InputPeer](../types/InputPeer.md) | Required| @@ -39,5 +38,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->forwardMessages(['silent' => Bool, 'background' => Bool, 'with_my_score' => Bool, 'from_peer' => InputPeer, 'id' => [int], 'random_id' => [long], 'to_peer' => InputPeer, ]); +$Updates = $MadelineProto->messages->forwardMessages(['silent' => Bool, 'background' => Bool, 'with_my_score' => Bool, 'from_peer' => InputPeer, 'id' => [int], 'to_peer' => InputPeer, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_requestEncryption.md b/docs/API_docs/methods/messages_requestEncryption.md index 23c80038..7d0efe0d 100644 --- a/docs/API_docs/methods/messages_requestEncryption.md +++ b/docs/API_docs/methods/messages_requestEncryption.md @@ -11,7 +11,6 @@ description: messages_requestEncryption parameters, return type and example | Name | Type | Required | |----------|:-------------:|---------:| |user\_id|[InputUser](../types/InputUser.md) | Required| -|random\_id|[int](../types/int.md) | Required| |g\_a|[bytes](../types/bytes.md) | Required| @@ -35,5 +34,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$EncryptedChat = $MadelineProto->messages->requestEncryption(['user_id' => InputUser, 'random_id' => int, 'g_a' => bytes, ]); +$EncryptedChat = $MadelineProto->messages->requestEncryption(['user_id' => InputUser, 'g_a' => bytes, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendEncrypted.md b/docs/API_docs/methods/messages_sendEncrypted.md index 0a3508c0..d3758b1d 100644 --- a/docs/API_docs/methods/messages_sendEncrypted.md +++ b/docs/API_docs/methods/messages_sendEncrypted.md @@ -11,7 +11,6 @@ description: messages_sendEncrypted parameters, return type and example | Name | Type | Required | |----------|:-------------:|---------:| |peer|[InputEncryptedChat](../types/InputEncryptedChat.md) | Required| -|random\_id|[long](../types/long.md) | Required| |data|[bytes](../types/bytes.md) | Required| @@ -35,5 +34,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncrypted(['peer' => InputEncryptedChat, 'random_id' => long, 'data' => bytes, ]); +$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncrypted(['peer' => InputEncryptedChat, 'data' => bytes, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendEncryptedFile.md b/docs/API_docs/methods/messages_sendEncryptedFile.md index a06f2710..da77b55a 100644 --- a/docs/API_docs/methods/messages_sendEncryptedFile.md +++ b/docs/API_docs/methods/messages_sendEncryptedFile.md @@ -11,7 +11,6 @@ description: messages_sendEncryptedFile parameters, return type and example | Name | Type | Required | |----------|:-------------:|---------:| |peer|[InputEncryptedChat](../types/InputEncryptedChat.md) | Required| -|random\_id|[long](../types/long.md) | Required| |data|[bytes](../types/bytes.md) | Required| |file|[InputEncryptedFile](../types/InputEncryptedFile.md) | Required| @@ -36,5 +35,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncryptedFile(['peer' => InputEncryptedChat, 'random_id' => long, 'data' => bytes, 'file' => InputEncryptedFile, ]); +$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncryptedFile(['peer' => InputEncryptedChat, 'data' => bytes, 'file' => InputEncryptedFile, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendEncryptedService.md b/docs/API_docs/methods/messages_sendEncryptedService.md index e30d1c83..38ac9255 100644 --- a/docs/API_docs/methods/messages_sendEncryptedService.md +++ b/docs/API_docs/methods/messages_sendEncryptedService.md @@ -11,7 +11,6 @@ description: messages_sendEncryptedService parameters, return type and example | Name | Type | Required | |----------|:-------------:|---------:| |peer|[InputEncryptedChat](../types/InputEncryptedChat.md) | Required| -|random\_id|[long](../types/long.md) | Required| |data|[bytes](../types/bytes.md) | Required| @@ -35,5 +34,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncryptedService(['peer' => InputEncryptedChat, 'random_id' => long, 'data' => bytes, ]); +$messages_SentEncryptedMessage = $MadelineProto->messages->sendEncryptedService(['peer' => InputEncryptedChat, 'data' => bytes, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendInlineBotResult.md b/docs/API_docs/methods/messages_sendInlineBotResult.md index 7fa46a55..1e6fd0e3 100644 --- a/docs/API_docs/methods/messages_sendInlineBotResult.md +++ b/docs/API_docs/methods/messages_sendInlineBotResult.md @@ -15,7 +15,6 @@ description: messages_sendInlineBotResult parameters, return type and example |clear\_draft|[Bool](../types/Bool.md) | Optional| |peer|[InputPeer](../types/InputPeer.md) | Required| |reply\_to\_msg\_id|[int](../types/int.md) | Optional| -|random\_id|[long](../types/long.md) | Required| |query\_id|[long](../types/long.md) | Required| |id|[string](../types/string.md) | Required| @@ -40,5 +39,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->sendInlineBotResult(['silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'random_id' => long, 'query_id' => long, 'id' => string, ]); +$Updates = $MadelineProto->messages->sendInlineBotResult(['silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'query_id' => long, 'id' => string, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendMedia.md b/docs/API_docs/methods/messages_sendMedia.md index 577fb71a..027d687f 100644 --- a/docs/API_docs/methods/messages_sendMedia.md +++ b/docs/API_docs/methods/messages_sendMedia.md @@ -16,7 +16,6 @@ description: messages_sendMedia parameters, return type and example |peer|[InputPeer](../types/InputPeer.md) | Required| |reply\_to\_msg\_id|[int](../types/int.md) | Optional| |media|[InputMedia](../types/InputMedia.md) | Required| -|random\_id|[long](../types/long.md) | Required| |reply\_markup|[ReplyMarkup](../types/ReplyMarkup.md) | Optional| @@ -40,5 +39,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->sendMedia(['silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'media' => InputMedia, 'random_id' => long, 'reply_markup' => ReplyMarkup, ]); +$Updates = $MadelineProto->messages->sendMedia(['silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'media' => InputMedia, 'reply_markup' => ReplyMarkup, ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_sendMessage.md b/docs/API_docs/methods/messages_sendMessage.md index d31c85df..188eebeb 100644 --- a/docs/API_docs/methods/messages_sendMessage.md +++ b/docs/API_docs/methods/messages_sendMessage.md @@ -17,7 +17,6 @@ description: messages_sendMessage parameters, return type and example |peer|[InputPeer](../types/InputPeer.md) | Required| |reply\_to\_msg\_id|[int](../types/int.md) | Optional| |message|[string](../types/string.md) | Required| -|random\_id|[long](../types/long.md) | Required| |reply\_markup|[ReplyMarkup](../types/ReplyMarkup.md) | Optional| |entities|Array of [MessageEntity](../types/MessageEntity.md) | Optional| @@ -42,5 +41,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->sendMessage(['no_webpage' => Bool, 'silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'message' => string, 'random_id' => long, 'reply_markup' => ReplyMarkup, 'entities' => [MessageEntity], ]); +$Updates = $MadelineProto->messages->sendMessage(['no_webpage' => Bool, 'silent' => Bool, 'background' => Bool, 'clear_draft' => Bool, 'peer' => InputPeer, 'reply_to_msg_id' => int, 'message' => string, 'reply_markup' => ReplyMarkup, 'entities' => [MessageEntity], ]); ``` \ No newline at end of file diff --git a/docs/API_docs/methods/messages_startBot.md b/docs/API_docs/methods/messages_startBot.md index 2a928b1d..9cf5250c 100644 --- a/docs/API_docs/methods/messages_startBot.md +++ b/docs/API_docs/methods/messages_startBot.md @@ -12,7 +12,6 @@ description: messages_startBot parameters, return type and example |----------|:-------------:|---------:| |bot|[InputUser](../types/InputUser.md) | Required| |peer|[InputPeer](../types/InputPeer.md) | Required| -|random\_id|[long](../types/long.md) | Required| |start\_param|[string](../types/string.md) | Required| @@ -36,5 +35,5 @@ if (isset($number)) { $MadelineProto->complete_phone_login($code); } -$Updates = $MadelineProto->messages->startBot(['bot' => InputUser, 'peer' => InputPeer, 'random_id' => long, 'start_param' => string, ]); +$Updates = $MadelineProto->messages->startBot(['bot' => InputUser, 'peer' => InputPeer, 'start_param' => string, ]); ``` \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 7ad2ccc5..94855b1e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -204,7 +204,7 @@ $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' var_dump($sentMessage); ``` -The API class also provides some wrapper methods for logging in as a bot or as a normal user: +The API class also provides some wrapper methods for logging in as a bot or as a normal user, and for getting inputPeer constructors to use in sendMessage and other methods: ``` $sentCode = $MadelineProto->phone_login($number); // Send code @@ -217,6 +217,19 @@ for ($x = 0; $x < $sentCode['type']['length']; $x++) { $authorization = $MadelineProto->complete_phone_login($code); // Complete authorization var_dump($authorization); +var_dump($MadelineProto->API->resolve_username('@Palmas2012')); // Always use this method to resolve usernames, but you won't need to call this to get info about peers, as get_peer and get_input_peer will call it for you if needed + +$mention = $MadelineProto->API->get_peer('@veetaw'); // Returns an object of type User or Chat +$mention = $MadelineProto->API->constructor2inputpeer($mention); // Converts an object of type User or Chat to an object of type inputPeer + +$message = "I've installed MadelineProto!"; +foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { + $peer = $MadelineProto->API->get_input_peer($peer); + $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); + var_dump($sentMessage); +} + +// The above works with bots too $authorization = $MadelineProto->bot_login($token); // Note that every time you login as a bot or as a user MadelineProto will logout first, so now MadelineProto is logged in as the bot with token $token, not as the user with number $number var_dump($authorization); ``` diff --git a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php index caa1ecb0..eb75fdd5 100644 --- a/src/danog/MadelineProto/MTProtoTools/PeerHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/PeerHandler.php @@ -18,15 +18,90 @@ namespace danog\MadelineProto\MTProtoTools; trait PeerHandler { public $chats = []; - public $users = []; public function add_users($users) { - var_dump($users); + foreach ($users as $key => $user) { + switch ($user['_']) { + case 'user': + $this->chats[$user['id']] = $user; + case 'userEmpty': + break; + default: + throw new \danog\MadelineProto\Exception('Invalid user provided at key '.$key.': '.var_export($user, true)); + break; + } + } } public function add_chats($chats) { - var_dump($chats); + foreach ($chats as $key => $chat) { + switch ($chat['_']) { + case 'chat': + case 'chatEmpty': + $this->chats[-$chat['id']] = $chat; + case 'chatForbidden': + case 'channelEmpty': + break; + case 'channel': + $this->chats[(int)('-100'.$chat['id'])] = $chat; + break; + default: + throw new \danog\MadelineProto\Exception('Invalid chat provided at key '.$key.': '.var_export($chat, true)); + break; + } + } } + + public function get_peer($id, $recursive = true) { + if (is_numeric($id)) { + if (isset($this->chats[$id])) { + return $this->chats[$id]; + } +// if ($recursive) { +// } + throw new \danog\MadelineProto\Exception("Couldn't find peer by provided chat id ".$id); + } + $id = str_replace('@', '', $id); + foreach ($this->chats as $chat) { + if (isset($chat['username']) && $chat['username'] == $id) { + return $chat; + } + } + if ($recursive) { + $this->resolve_username($id); + return $this->get_peer($id, false); + } + throw new \danog\MadelineProto\Exception("Couldn't find peer by provided username ".$id); + } + + public function get_input_peer($id) { + return $this->constructor2inputpeer($this->get_peer($id)); + } + + public function constructor2inputpeer($peer) { + switch ($peer['_']) { + case 'user': + return $peer['self'] ? ['_' => 'inputPeerSelf'] : ['_' => 'inputPeerUser', 'user_id' => $peer['id'], 'access_hash' => $peer['access_hash']]; + case 'chat': + case 'chatEmpty': + return ['_' => 'inputPeerChat', 'chat_id' => $peer['id']]; + case 'channel': + return ['_' => 'inputPeerChannel', 'channel_id' => $peer['id'], 'access_hash' => $peer['access_hash']]; + default: + throw new \danog\MadelineProto\Exception('Invalid constructor given'); + } + } + + public function resolve_username($username) { + $res = $this->method_call('contacts.resolveUsername', ['username' => str_replace('@', '', $username)]); + if ($res['_'] == 'contacts.resolvedPeer') { + $this->add_users($res['users']); + $this->add_chats($res['chats']); + return $res; + } + throw new \danog\MadelineProto\Exception('resolve_username returned an unexpected constructor: '.var_export($username, true)); + } + } diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index a9ac8420..72fc4b82 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -207,6 +207,23 @@ class TL extends \danog\MadelineProto\Tools //\danog\MadelineProto\Logger::log('Skipping '.$current_argument['name'].' of type '.$current_argument['type'].'/'.$current_argument['subtype']); continue; } + if ($current_argument['name'] == 'random_id') { + switch ($current_argument['type']) { + case 'long': + $serialized .= \phpseclib\Crypt\Random::string(8); + continue 2; + case 'long': + $serialized .= \phpseclib\Crypt\Random::string(4); + continue 2; + case 'Vector t': + if ($method == 'messages.forwardMessages') { + $serialized .= \danog\PHP\Struct::pack('constructors->find_by_predicate('vector')['id']); + $serialized .= \danog\PHP\Struct::pack('contacts->resolveUsername(['username' => $user]); - var_dump($username); - $peers[$user] = ['_' => 'inputPeerChannel', 'channel_id' => $username['peer']['channel_id'], 'access_hash' => $username['chats'][0]['access_hash']]; -} +$message = (getenv('TRAVIS_COMMIT') == '') ? 'Message entities can be sent too (yay)' : ('Travis ci tests in progress: commit '.getenv('TRAVIS_COMMIT').', job '.getenv('TRAVIS_JOB_NUMBER').', PHP version: '.getenv('TRAVIS_PHP_VERSION')); -foreach ($peers as $peer) { - $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'random_id' => \danog\PHP\Struct::unpack('API->resolve_username('@Palmas2012')); // Always use this method to resolve usernames, but you won't need to call this to get info about peers, as get_peer and get_input_peer will call it for you if needed + +$mention = $MadelineProto->API->get_peer('@veetaw'); // Returns an object of type User or Chat +$mention = $MadelineProto->API->constructor2inputpeer($mention); // Converts an object of type User or Chat to an object of type inputPeer + +foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) { + $peer = $MadelineProto->API->get_input_peer($peer); // Returns directly an inputPeer object, basically does the same thing I've done manually above + $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message.' & pony', 'entities' => [['_' => 'messageEntityUrl', 'offset' => strlen($message)+1, 'length' => 6, 'url' => $flutter], ['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); var_dump($sentMessage); } @@ -58,9 +60,8 @@ if (file_exists('token.php')) { $authorization = $MadelineProto->bot_login($token); var_dump($authorization); } -/* -foreach ($peers as $peer) { - $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'random_id' => \danog\PHP\Struct::unpack('API->get_input_peer($peer); + $sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message.' & pony', 'entities' => [['_' => 'messageEntityUrl', 'offset' => strlen($message)+1, 'length' => 6, 'url' => $flutter], ['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]); var_dump($sentMessage); } -*/