The MadelineProto API documentation (mtproto tl scheme) can be found [here](https://daniil.it/MadelineProto/MTProto_docs/).
The MadelineProto API documentations (old layers) can be found [here (layer 46)](https://daniil.it/MadelineProto/API_docs_46/), [here (layer 55)](https://daniil.it/MadelineProto/API_docs_55/).
This project depends on [PHPStruct](https://github.com/danog/PHPStruct), [phpseclib](https://github.com/phpseclib/phpseclib), https://packagist.org/packages/paragonie/constant_time_encoding and https://packagist.org/packages/paragonie/random_compat
To install dependencies install composer and run:
```
composer update
```
In the cloned repo.
### Instantiation
```
$MadelineProto = new \danog\MadelineProto\API();
```
### Settings
The constructor accepts an optional parameter, which is the settings array. This array contains some other arrays, which are the settings for a specific MadelineProto function.
Here you can see the default values for the settings\ arrays and explanations for every setting:
'all' => [ // Connection settings will be applied on datacenter ids matching the key of these settings subarrays, if the key is equal to all like in this case that will match all datacenters that haven't a custom settings subarray...
'protocol' => 'tcp_full', // can be tcp_full, tcp_abridged, tcp_intermediate, http (unsupported), https (unsupported), udp (unsupported)
'test_mode' => false, // decides whether to connect to the main telegram servers or to the testing servers (deep telegram)
'ipv6' => $this->ipv6, // decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean
'timeout' => 10 // timeout for sockets
],
],
'app_info' => [ // obtained in https://my.telegram.org
'updates_array_limit' => 1000, // How big should be the array containing the updates processed with the default example_update_handler callback
'callback' => [$this, 'get_updates_update_handler'], // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter
You can provide part of any subsetting array, that way the remaining arrays will be automagically set to default and undefined values of specified subsetting arrays will be set to the default values.
When an update is received, the update callback function (see settings) is called. By default, the get_updates_update_handler MadelineProto method is called. This method stores all incoming updates into an array (its size limit is specified by the updates\_array\_limit parameter in the settings) and can be fetched by running the `get_updates` method.
This method accepts an array of options as the first parameter, and returns an array of updates. Example:
```
$MadelineProto = new \danog\MadelineProto\API();
// Login or deserialize
$offset = 0;
while (true) {
$updates = $MadelineProto->API->get_updates(['offset' => $offset, 'limit' => 50, 'timeout' => 1]); // Just like in the bot API, you can specify an offset, a limit and a timeout
foreach ($updates as $update) {
$offset = $update['update_id'] // Just like in the bot API, the offset must be set to the last update_id
// Parse $update['update'], that is an object of type Update
}
var_dump($update);
}
array(3) {
[0]=>
array(2) {
["update_id"]=>
int(0)
["update"]=>
array(5) {
["_"]=>
string(22) "updateNewAuthorization"
["auth_key_id"]=>
int(-8182897590766478746)
["date"]=>
int(1483110797)
["device"]=>
string(3) "Web"
["location"]=>
string(25) "IT, 05 (IP = 79.2.51.203)"
}
}
[1]=>
array(2) {
["update_id"]=>
int(1)
["update"]=>
array(3) {
["_"]=>
string(23) "updateReadChannelOutbox"
["channel_id"]=>
int(1049295266)
["max_id"]=>
int(8288)
}
}
[2]=>
array(2) {
["update_id"]=>
int(2)
["update"]=>
array(4) {
["_"]=>
string(23) "updateNewChannelMessage"
["message"]=>
array(12) {
["_"]=>
string(7) "message"
["out"]=>
bool(false)
["mentioned"]=>
bool(false)
["media_unread"]=>
bool(false)
["silent"]=>
bool(false)
["post"]=>
bool(false)
["id"]=>
int(11521)
["from_id"]=>
int(262946027)
["to_id"]=>
array(2) {
["_"]=>
string(11) "peerChannel"
["channel_id"]=>
int(1066910227)
}
["date"]=>
int(1483110798)
["message"]=>
string(3) "yay"
["entities"]=>
array(1) {
[0]=>
array(4) {
["_"]=>
string(24) "messageEntityMentionName"
["offset"]=>
int(0)
["length"]=>
int(3)
["user_id"]=>
int(101374607)
}
}
}
["pts"]=>
int(13010)
["pts_count"]=>
int(1)
}
}
}
```
To specify a custom callback change the correct value in the settings. The specified callable must accept one parameter for the update.
To call an MTProto method simply call it as if it is a method of the API class, substitute namespace sepators (.) with -> if needed.
Also, an object of type User, InputUser, Chat, InputChannel, Peer or InputPeer must be provided as a parameter to a method, you can substitute it with the user/group/channel's username or bot API id.
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:
$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
An istance of MadelineProto can be safely serialized or unserialized. To serialize MadelineProto to a file, usage of the `\danog\MadelineProto\Serialization` class is recommended:
That class serializes only if the `$MadelineProto->API->should_serialize` boolean is set to true.
The same operation should be done when serializing to another destination manually, to avoid conflicts with other PHP scripts that are trying to serialize another instance of the class.
AckHandler - Handles acknowledgement of incoming and outgoing mtproto messages
AuthKeyHandler - Handles generation of the temporary and permanent authorization keys
CallHandler - Handles synchronous calls to mtproto methods or objects, also basic response management (waits until the socket receives a response)
Crypt - Handles ige and aes encryption
MessageHandler - Handles sending and receiving of mtproto messages (packs TL serialized data with message id, auth key id and encrypts it with Crypt if needed, adds them to the arrays of incoming and outgoing messages)
MsgIdHandler - Handles message ids (checks if they are valid, adds them to the arrays of incoming and outgoing messages)
ResponseHandler - Handles the content of responses received, service messages, rpc results, errors, and stores them into the response section of the outgoing messages array)
API - Wrapper class that instantiates the MTProto class, sets the error handler, provides a wrapper for calling mtproto methods directly as class submethods, and uses the simplified wrappers from Wrappers/
APIFactory - Provides a wrapper for calling namespaced mtproto methods directly as class submethods
Connection - Handles tcp/udp/http connections and wrapping payloads generated by MTProtoTools/MessageHandler into the right message according to the protocol, stores authorization keys, session id and sequence number
DataCenter - Handles mtproto datacenters (is a wrapper for Connection classes)