Added file upload/download wrappers, decided to store full chat/user/channel constructors in $API->chats, fixed bugs

This commit is contained in:
Daniil Gentili 2017-01-07 12:39:11 +01:00
parent f54fb0b6b3
commit 20d9b0fa6d
17 changed files with 797 additions and 49 deletions

BIN
60 Normal file

Binary file not shown.

View File

@ -199,7 +199,7 @@ $MadelineProto->update_settings($settings);
### Handling updates
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:
This method accepts an array of options as the first parameter, and returns an array of updates (an array containing the update id and an object of type [Update](https://daniil.it/MadelineProto/API_docs/types/Updates.html)). Example:
```
$MadelineProto = new \danog\MadelineProto\API();
@ -314,6 +314,36 @@ array(3) {
To specify a custom callback change the correct value in the settings. The specified callable must accept one parameter for the update.
### Uploading and downloading files
MadelineProto provides wrapper methods to upload and download files.
Every method described in this section accepts a last optional paramater with a callable function that will be called during the upload/download using the first parameter to pass a floating point number indicating the upload/download status in percentage.
The upload method returns an [InputFile](https://daniil.it/MadelineProto/API_docs/types/InputFile.html) object that must be used to generate an [InputMedia](https://daniil.it/MadelineProto/API_docs/types/InputMedia.html) object, that can be later sent using the [sendmedia method](https://daniil.it/MadelineProto/API_docs/methods/messages_sendMedia.html).
```
$inputFile = $MadelineProto->upload('file', 'optional new file name.ext');
// Generate an inputMedia object and store it in $inputMedia, see testing.php
$MadelineProto->messages->sendMedia(['peer' => '@pwrtelegramgroup', 'media' => $inputMedia]);
```
See testing.php for more examples.
There are multiple download methods that allow you to download a file to a directory, to a file or to a stream.
The first parameter of these functions must always be a [messageMediaPhoto](https://daniil.it/MadelineProto/API_docs/constructors/messageMediaPhoto.html) or a [messageMediaDocument](https://daniil.it/MadelineProto/API_docs/constructors/messageMediaDocument.html) object. These objects are usually received in updates, see `bot.php` for examples
```
$output_file_name = $MadelineProto->download_to_dir($message_media, '/tmp/dldir');
$custom_output_file_name = $MadelineProto->download_to_file($message_media, '/tmp/dldir/customname.ext');
$stream = fopen('php://output', 'w'); // Stream to browser like with echo
$MadelineProto->download_to_stream($message_media, $stream);
```
### Calling mtproto methods and available wrappers

13
bot.php
View File

@ -19,8 +19,17 @@ while (true) {
case 'updateNewMessage':
if ($update['update']['message']['out']) continue;
$res = json_encode($update, JSON_PRETTY_PRINT);
if ($res == '') $res =var_export($update, true);
try { $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json', ]]]); } catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); }
if ($res == '') $res = var_export($update, true);
try {
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => $res, 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json', ]]]);
} catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); }
try {
if (isset($update['update']['message']['media'])) {
$time = time();
$file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp');
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json', ]]]);
}
} catch (\danog\MadelineProto\RPCErrorException $e) { $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); }
}
}
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('bot.madeline', $MadelineProto).' bytes'.PHP_EOL;

9
composer.lock generated
View File

@ -4,7 +4,6 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "fc800fb5f8bf5490499819d3b4b2330d",
"content-hash": "3a10bc147a48cd4573bcc9654d3be153",
"packages": [
{
@ -58,7 +57,7 @@
"struct",
"unpack"
],
"time": "2016-11-14 15:09:50"
"time": "2016-11-14T15:09:50+00:00"
},
{
"name": "paragonie/constant_time_encoding",
@ -119,7 +118,7 @@
"hex2bin",
"rfc4648"
],
"time": "2016-07-11 20:32:06"
"time": "2016-07-11T20:32:06+00:00"
},
{
"name": "paragonie/random_compat",
@ -167,7 +166,7 @@
"pseudorandom",
"random"
],
"time": "2016-11-07 23:38:38"
"time": "2016-11-07T23:38:38+00:00"
},
{
"name": "phpseclib/phpseclib",
@ -259,7 +258,7 @@
"x.509",
"x509"
],
"time": "2016-10-04 00:57:04"
"time": "2016-10-04T00:57:04+00:00"
}
],
"packages-dev": [],

View File

@ -203,7 +203,7 @@ $MadelineProto->update_settings($settings);
### Handling updates
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:
This method accepts an array of options as the first parameter, and returns an array of updates (an array containing the update id and an object of type [Update](https://daniil.it/MadelineProto/API_docs/types/Updates.html)). Example:
```
$MadelineProto = new \danog\MadelineProto\API();
@ -318,6 +318,36 @@ array(3) {
To specify a custom callback change the correct value in the settings. The specified callable must accept one parameter for the update.
### Uploading and downloading files
MadelineProto provides wrapper methods to upload and download files.
Every method described in this section accepts a last optional paramater with a callable function that will be called during the upload/download using the first parameter to pass a floating point number indicating the upload/download status in percentage.
The upload method returns an [InputFile](https://daniil.it/MadelineProto/API_docs/types/InputFile.html) object that must be used to generate an [InputMedia](https://daniil.it/MadelineProto/API_docs/types/InputMedia.html) object, that can be later sent using the [sendmedia method](https://daniil.it/MadelineProto/API_docs/methods/messages_sendMedia.html).
```
$inputFile = $MadelineProto->upload('file', 'optional new file name.ext');
// Generate an inputMedia object and store it in $inputMedia, see testing.php
$MadelineProto->messages->sendMedia(['peer' => '@pwrtelegramgroup', 'media' => $inputMedia]);
```
See testing.php for more examples.
There are multiple download methods that allow you to download a file to a directory, to a file or to a stream.
The first parameter of these functions must always be a [messageMediaPhoto](https://daniil.it/MadelineProto/API_docs/constructors/messageMediaPhoto.html) or a [messageMediaDocument](https://daniil.it/MadelineProto/API_docs/constructors/messageMediaDocument.html) object. These objects are usually received in updates, see `bot.php` for examples
```
$output_file_name = $MadelineProto->download_to_dir($message_media, '/tmp/dldir');
$custom_output_file_name = $MadelineProto->download_to_file($message_media, '/tmp/dldir/customname.ext');
$stream = fopen('php://output', 'w'); // Stream to browser like with echo
$MadelineProto->download_to_stream($message_media, $stream);
```
### Calling mtproto methods and available wrappers

BIN
faust.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

BIN
lel.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
mosconi.mp3 Normal file

Binary file not shown.

BIN
pony.mp4 Normal file

Binary file not shown.

View File

@ -16,6 +16,7 @@ class API extends APIFactory
{
use \danog\MadelineProto\Wrappers\Login;
use \danog\MadelineProto\Wrappers\PeerHandler;
use \danog\MadelineProto\Wrappers\FilesHandler;
use \danog\MadelineProto\Wrappers\SettingsManager;
public $API;

View File

@ -73,11 +73,13 @@ trait CallHandler
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
break;
}
/*
case 420:
$seconds = preg_replace('/[^0-9]+/', '', $server_answer['error_message']);
\danog\MadelineProto\Logger::log('Flood, waiting '.$seconds.' seconds...');
sleep($seconds);
throw new \danog\MadelineProto\Exception('Re-executing query...');
*/
default:
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
break;

View File

@ -24,8 +24,9 @@ trait PeerHandler
foreach ($users as $key => $user) {
switch ($user['_']) {
case 'user':
if (!isset($this->chats[$user['id']]) || $this->chats[$user['id']] !== $user) {
$this->chats[$user['id']] = $user;
if (!isset($this->chats[$user['id']]) || $this->chats[$user['id']]['user'] !== $user) {
//$this->method_call('users.getFullUser', ['id' => $user]);
$this->chats[$user['id']] = ['_' => 'userFull', 'user' => $user];
$this->should_serialize = true;
}
case 'userEmpty':
@ -43,18 +44,21 @@ trait PeerHandler
switch ($chat['_']) {
case 'chat':
case 'chatEmpty':
if (!isset($this->chats[-$chat['id']]) || $this->chats[-$chat['id']] !== $chat) {
if (!isset($this->chats[-$chat['id']]) || $this->chats[-$chat['id']]['chat'] !== $chat) {
//$this->method_call('messages.getFullChat', ['chat_id' => $chat['id']]);
$this->chats[-$chat['id']] = ['_' => 'chatFull', 'chat' => $chat];
$this->should_serialize = true;
$this->chats[-$chat['id']] = $chat;
}
case 'chatForbidden':
case 'channelEmpty':
break;
case 'channel':
if (!isset($this->chats[(int) ('-100'.$chat['id'])]) || $this->chats[(int) ('-100'.$chat['id'])] !== $chat) {
if (!isset($this->chats[(int) ('-100'.$chat['id'])]) || $this->chats[(int) ('-100'.$chat['id'])]['channel'] !== $chat) {
$this->chats[(int)('-100'.$chat['id'])] = ['_' => 'channelFull', 'channel' => $chat];
$this->should_serialize = true;
$this->chats[(int) ('-100'.$chat['id'])] = $chat;
//$this->method_call('channels.getFullChannel', ['channel' => $chat]);
}
break;
default:
@ -62,7 +66,6 @@ trait PeerHandler
break;
}
}
$this->should_serialize = true;
}
public function peer_isset($id)
@ -111,13 +114,16 @@ trait PeerHandler
{
if (is_array($id)) {
switch ($id['_']) {
case 'inputPeerSelf':
case 'inputUserSelf':
case 'inputPeerSelf':
$id = $this->datacenter->authorization['user']['id'];
break;
case 'user':
$id = $id['id'];
break;
case 'userFull':
$id = $id['user']['id'];
break;
case 'inputPeerUser':
case 'inputUser':
case 'peerUser':
@ -127,6 +133,9 @@ trait PeerHandler
case 'chat':
$id = -$id['id'];
break;
case 'chatFull':
$id = -$id['chat']['id'];
break;
case 'inputPeerChat':
case 'peerChat':
$id = -$id['chat_id'];
@ -135,6 +144,10 @@ trait PeerHandler
case 'channel':
$id = '-100'.$id['id'];
break;
case 'channelFull':
$id = '-100'.$id['channel']['id'];
break;
case 'inputPeerChannel':
case 'inputChannel':
case 'peerChannel':
@ -161,14 +174,14 @@ trait PeerHandler
if (isset($this->chats[$id])) {
return $this->gen_all($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']) && strtolower($chat['username']) == strtolower($id)) {
return $this->gen_all($chat);
foreach (['user', 'chat', 'channel'] as $wut) {
if (isset($chat[$wut]['username']) && strtolower($chat[$wut]['username']) == strtolower($id)) {
return $this->gen_all($chat);
}
}
}
if ($recursive) {
@ -183,32 +196,35 @@ trait PeerHandler
{
$res = [$this->constructors->find_by_predicate($constructor['_'])['type'] => $constructor];
switch ($constructor['_']) {
case 'user':
if ($constructor['self']) {
case 'userFull':
$res['User'] = $constructor['user'];
if ($constructor['user']['self']) {
$res['InputPeer'] = ['_' => 'inputPeerSelf'];
$res['InputUser'] = ['_' => 'inputUserSelf'];
} elseif (isset($constructor['access_hash'])) {
$res['InputPeer'] = ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$res['InputUser'] = ['_' => 'inputUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
} elseif (isset($constructor['user']['access_hash'])) {
$res['InputPeer'] = ['_' => 'inputPeerUser', 'user_id' => $constructor['user']['id'], 'access_hash' => $constructor['user']['access_hash']];
$res['InputUser'] = ['_' => 'inputUser', 'user_id' => $constructor['user']['id'], 'access_hash' => $constructor['user']['access_hash']];
}
$res['Peer'] = ['_' => 'peerUser', 'user_id' => $constructor['id']];
$res['user_id'] = $constructor['id'];
$res['bot_api_id'] = $constructor['id'];
$res['Peer'] = ['_' => 'peerUser', 'user_id' => $constructor['user']['id']];
$res['user_id'] = $constructor['user']['id'];
$res['bot_api_id'] = $constructor['user']['id'];
break;
case 'chat':
$res['InputPeer'] = ['_' => 'inputPeerChat', 'chat_id' => $constructor['id']];
$res['Peer'] = ['_' => 'peerChat', 'chat_id' => $constructor['id']];
$res['chat_id'] = $constructor['id'];
$res['bot_api_id'] = -$constructor['id'];
case 'chatFull':
$res['Chat'] = $constructor['chat'];
$res['InputPeer'] = ['_' => 'inputPeerChat', 'chat_id' => $constructor['chat']['id']];
$res['Peer'] = ['_' => 'peerChat', 'chat_id' => $constructor['chat']['id']];
$res['chat_id'] = $constructor['chat']['id'];
$res['bot_api_id'] = -$constructor['chat']['id'];
break;
case 'channel':
if (isset($constructor['access_hash'])) {
$res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
case 'channelFull':
$res['Channel'] = $constructor['channel'];
if (isset($constructor['channel']['access_hash'])) {
$res['InputPeer'] = ['_' => 'inputPeerChannel', 'channel_id' => $constructor['channel']['id'], 'access_hash' => $constructor['channel']['access_hash']];
$res['InputChannel'] = ['_' => 'inputChannel', 'channel_id' => $constructor['channel']['id'], 'access_hash' => $constructor['channel']['access_hash']];
}
$res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['id']];
$res['channel_id'] = $constructor['id'];
$res['bot_api_id'] = (int) ('-100'.$constructor['id']);
$res['Peer'] = ['_' => 'peerChannel', 'channel_id' => $constructor['channel']['id']];
$res['channel_id'] = $constructor['channel']['id'];
$res['bot_api_id'] = (int) ('-100'.$constructor['channel']['id']);
break;
default:
throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($constructor, true));

View File

@ -82,8 +82,30 @@ trait ResponseHandler
if (isset($response['result']['users'])) {
$this->add_users($response['result']['users']);
}
if (isset($response['result']['_']) && $this->constructors->find_by_predicate($response['result']['_'])['type'] == 'Update') {
$this->handle_update($response['result']);
if (isset($response['result']['chats'])) {
$this->add_chats($response['result']['chats']);
}
if (isset($response['result']['_'])) {
switch ($this->constructors->find_by_predicate($response['result']['_'])['type']) {
case 'Update':
$this->handle_update($response['result']);
break;
case 'userFull':
$this->chats[$response['result']['user']['id']] = $response['result'];
$this->should_serialize = true;
break;
case 'chatFull':
$this->chats[-$response['result']['chat']['id']] = $response['result'];
$this->should_serialize = true;
break;
case 'channelFull':
$this->chats[(int)('-100'.$response['result']['channel']['id'])] = $response['result'];
$this->should_serialize = true;
break;
}
}
switch ($response['_']) {
case 'msgs_ack':

View File

@ -83,7 +83,6 @@ trait UpdateHandler
}
$difference = $this->method_call('updates.getChannelDifference', ['channel' => $input, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]);
\danog\MadelineProto\Logger::log('Got '.$difference['_']);
$this->get_channel_state($channel)['sync_loading'] = false;
switch ($difference['_']) {
case 'updates.channelDifferenceEmpty':
$this->set_channel_state($channel, $difference);
@ -105,6 +104,7 @@ trait UpdateHandler
throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true));
break;
}
$this->get_channel_state($channel)['sync_loading'] = false;
}
public function set_update_state($data)
@ -138,7 +138,6 @@ trait UpdateHandler
$difference = $this->method_call('updates.getDifference', ['pts' => $this->get_update_state()['pts'], 'date' => $this->get_update_state()['date'], 'qts' => -1]);
\danog\MadelineProto\Logger::log('Got '.$difference['_']);
$this->get_update_state()['sync_loading'] = false;
switch ($difference['_']) {
case 'updates.differenceEmpty':
$this->set_update_state($difference);
@ -158,6 +157,7 @@ trait UpdateHandler
throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true));
break;
}
$this->get_update_state()['sync_loading'] = false;
}
public function get_updates_state()
@ -203,13 +203,13 @@ trait UpdateHandler
} else {
$cur_state = &$this->get_channel_state($channel_id, (isset($update['pts']) ? $update['pts'] : 0) - (isset($update['pts_count']) ? $update['pts_count'] : 0));
}
/*
if ($cur_state['sync_loading']) {
\danog\MadelineProto\Logger::log('Sync loading, not handling update');
return false;
}
*/
switch ($update['_']) {
case 'updateChannelTooLong':
$this->get_channel_difference($channel_id);
@ -235,7 +235,7 @@ trait UpdateHandler
return false;
}
if ($message['from_id'] == $this->datacenter->authorization['user']['id']) { $message['out'] = true; }
if (isset($message['from_id']) && $message['from_id'] == $this->datacenter->authorization['user']['id']) { $message['out'] = true; }
break;
default:
if ($channel_id !== false && !$this->peer_isset('channel#'.$channel_id)) {
@ -266,7 +266,7 @@ trait UpdateHandler
if ($update['pts'] > $cur_state['pts']) {
$cur_state['pts'] = $update['pts'];
$pop_pts = true;
} elseif (isset($update['pts_count']) && $update['pts_count']) {
} elseif (isset($update['pts_count'])) {
\danog\MadelineProto\Logger::log('Duplicate update. current pts: '.$cur_state['pts'].' + pts count: '.(isset($update['pts_count']) ? $update['pts_count'] : 0).' = new pts: '.$new_pts.'. update pts: '.$update['pts'].' <= current pts '.$cur_state['pts'].', channel id: '.$channel_id, $update);
return false;

View File

@ -0,0 +1,600 @@
<?php
/*
Copyright 2016 Daniil Gentili
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
namespace danog\MadelineProto\Wrappers;
/**
* Manages file upload and download
*/
trait FilesHandler
{
public $all_mimes = array (
'png' =>
array (
0 => 'image/png',
1 => 'image/x-png',
),
'bmp' =>
array (
0 => 'image/bmp',
1 => 'image/x-bmp',
2 => 'image/x-bitmap',
3 => 'image/x-xbitmap',
4 => 'image/x-win-bitmap',
5 => 'image/x-windows-bmp',
6 => 'image/ms-bmp',
7 => 'image/x-ms-bmp',
8 => 'application/bmp',
9 => 'application/x-bmp',
10 => 'application/x-win-bitmap',
),
'gif' =>
array (
0 => 'image/gif',
),
'jpeg' =>
array (
0 => 'image/jpeg',
1 => 'image/pjpeg',
),
'xspf' =>
array (
0 => 'application/xspf+xml',
),
'vlc' =>
array (
0 => 'application/videolan',
),
'wmv' =>
array (
0 => 'video/x-ms-wmv',
1 => 'video/x-ms-asf',
),
'au' =>
array (
0 => 'audio/x-au',
),
'ac3' =>
array (
0 => 'audio/ac3',
),
'flac' =>
array (
0 => 'audio/x-flac',
),
'ogg' =>
array (
0 => 'audio/ogg',
1 => 'video/ogg',
2 => 'application/ogg',
),
'kmz' =>
array (
0 => 'application/vnd.google-earth.kmz',
),
'kml' =>
array (
0 => 'application/vnd.google-earth.kml+xml',
),
'rtx' =>
array (
0 => 'text/richtext',
),
'rtf' =>
array (
0 => 'text/rtf',
),
'jar' =>
array (
0 => 'application/java-archive',
1 => 'application/x-java-application',
2 => 'application/x-jar',
),
'zip' =>
array (
0 => 'application/x-zip',
1 => 'application/zip',
2 => 'application/x-zip-compressed',
3 => 'application/s-compressed',
4 => 'multipart/x-zip',
),
'7zip' =>
array (
0 => 'application/x-compressed',
),
'xml' =>
array (
0 => 'application/xml',
1 => 'text/xml',
),
'svg' =>
array (
0 => 'image/svg+xml',
),
'3g2' =>
array (
0 => 'video/3gpp2',
),
'3gp' =>
array (
0 => 'video/3gp',
1 => 'video/3gpp',
),
'mp4' =>
array (
0 => 'video/mp4',
),
'm4a' =>
array (
0 => 'audio/x-m4a',
),
'f4v' =>
array (
0 => 'video/x-f4v',
),
'flv' =>
array (
0 => 'video/x-flv',
),
'webm' =>
array (
0 => 'video/webm',
),
'aac' =>
array (
0 => 'audio/x-acc',
),
'm4u' =>
array (
0 => 'application/vnd.mpegurl',
),
'pdf' =>
array (
0 => 'application/pdf',
1 => 'application/octet-stream',
),
'pptx' =>
array (
0 => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
),
'ppt' =>
array (
0 => 'application/powerpoint',
1 => 'application/vnd.ms-powerpoint',
2 => 'application/vnd.ms-office',
3 => 'application/msword',
),
'docx' =>
array (
0 => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
),
'xlsx' =>
array (
0 => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
1 => 'application/vnd.ms-excel',
),
'xl' =>
array (
0 => 'application/excel',
),
'xls' =>
array (
0 => 'application/msexcel',
1 => 'application/x-msexcel',
2 => 'application/x-ms-excel',
3 => 'application/x-excel',
4 => 'application/x-dos_ms_excel',
5 => 'application/xls',
6 => 'application/x-xls',
),
'xsl' =>
array (
0 => 'text/xsl',
),
'mpeg' =>
array (
0 => 'video/mpeg',
),
'mov' =>
array (
0 => 'video/quicktime',
),
'avi' =>
array (
0 => 'video/x-msvideo',
1 => 'video/msvideo',
2 => 'video/avi',
3 => 'application/x-troff-msvideo',
),
'movie' =>
array (
0 => 'video/x-sgi-movie',
),
'log' =>
array (
0 => 'text/x-log',
),
'txt' =>
array (
0 => 'text/plain',
),
'css' =>
array (
0 => 'text/css',
),
'html' =>
array (
0 => 'text/html',
),
'wav' =>
array (
0 => 'audio/x-wav',
1 => 'audio/wave',
2 => 'audio/wav',
),
'xhtml' =>
array (
0 => 'application/xhtml+xml',
),
'tar' =>
array (
0 => 'application/x-tar',
),
'tgz' =>
array (
0 => 'application/x-gzip-compressed',
),
'psd' =>
array (
0 => 'application/x-photoshop',
1 => 'image/vnd.adobe.photoshop',
),
'exe' =>
array (
0 => 'application/x-msdownload',
),
'js' =>
array (
0 => 'application/x-javascript',
),
'mp3' =>
array (
0 => 'audio/mpeg',
1 => 'audio/mpg',
2 => 'audio/mpeg3',
3 => 'audio/mp3',
),
'rar' =>
array (
0 => 'application/x-rar',
1 => 'application/rar',
2 => 'application/x-rar-compressed',
),
'gzip' =>
array (
0 => 'application/x-gzip',
),
'hqx' =>
array (
0 => 'application/mac-binhex40',
1 => 'application/mac-binhex',
2 => 'application/x-binhex40',
3 => 'application/x-mac-binhex40',
),
'cpt' =>
array (
0 => 'application/mac-compactpro',
),
'bin' =>
array (
0 => 'application/macbinary',
1 => 'application/mac-binary',
2 => 'application/x-binary',
3 => 'application/x-macbinary',
),
'oda' =>
array (
0 => 'application/oda',
),
'ai' =>
array (
0 => 'application/postscript',
),
'smil' =>
array (
0 => 'application/smil',
),
'mif' =>
array (
0 => 'application/vnd.mif',
),
'wbxml' =>
array (
0 => 'application/wbxml',
),
'wmlc' =>
array (
0 => 'application/wmlc',
),
'dcr' =>
array (
0 => 'application/x-director',
),
'dvi' =>
array (
0 => 'application/x-dvi',
),
'gtar' =>
array (
0 => 'application/x-gtar',
),
'php' =>
array (
0 => 'application/x-httpd-php',
1 => 'application/php',
2 => 'application/x-php',
3 => 'text/php',
4 => 'text/x-php',
5 => 'application/x-httpd-php-source',
),
'swf' =>
array (
0 => 'application/x-shockwave-flash',
),
'sit' =>
array (
0 => 'application/x-stuffit',
),
'z' =>
array (
0 => 'application/x-compress',
),
'mid' =>
array (
0 => 'audio/midi',
),
'aif' =>
array (
0 => 'audio/x-aiff',
1 => 'audio/aiff',
),
'ram' =>
array (
0 => 'audio/x-pn-realaudio',
),
'rpm' =>
array (
0 => 'audio/x-pn-realaudio-plugin',
),
'ra' =>
array (
0 => 'audio/x-realaudio',
),
'rv' =>
array (
0 => 'video/vnd.rn-realvideo',
),
'jp2' =>
array (
0 => 'image/jp2',
1 => 'video/mj2',
2 => 'image/jpx',
3 => 'image/jpm',
),
'tiff' =>
array (
0 => 'image/tiff',
),
'eml' =>
array (
0 => 'message/rfc822',
),
'pem' =>
array (
0 => 'application/x-x509-user-cert',
1 => 'application/x-pem-file',
),
'p10' =>
array (
0 => 'application/x-pkcs10',
1 => 'application/pkcs10',
),
'p12' =>
array (
0 => 'application/x-pkcs12',
),
'p7a' =>
array (
0 => 'application/x-pkcs7-signature',
),
'p7c' =>
array (
0 => 'application/pkcs7-mime',
1 => 'application/x-pkcs7-mime',
),
'p7r' =>
array (
0 => 'application/x-pkcs7-certreqresp',
),
'p7s' =>
array (
0 => 'application/pkcs7-signature',
),
'crt' =>
array (
0 => 'application/x-x509-ca-cert',
1 => 'application/pkix-cert',
),
'crl' =>
array (
0 => 'application/pkix-crl',
1 => 'application/pkcs-crl',
),
'pgp' =>
array (
0 => 'application/pgp',
),
'gpg' =>
array (
0 => 'application/gpg-keys',
),
'rsa' =>
array (
0 => 'application/x-pkcs7',
),
'ics' =>
array (
0 => 'text/calendar',
),
'zsh' =>
array (
0 => 'text/x-scriptzsh',
),
'cdr' =>
array (
0 => 'application/cdr',
1 => 'application/coreldraw',
2 => 'application/x-cdr',
3 => 'application/x-coreldraw',
4 => 'image/cdr',
5 => 'image/x-cdr',
6 => 'zz-application/zz-winassoc-cdr',
),
'wma' =>
array (
0 => 'audio/x-ms-wma',
),
'vcf' =>
array (
0 => 'text/x-vcard',
),
'srt' =>
array (
0 => 'text/srt',
),
'vtt' =>
array (
0 => 'text/vtt',
),
'ico' =>
array (
0 => 'image/x-icon',
1 => 'image/x-ico',
2 => 'image/vnd.microsoft.icon',
),
'csv' =>
array (
0 => 'text/x-comma-separated-values',
1 => 'text/comma-separated-values',
2 => 'application/vnd.msexcel',
),
'json' =>
array (
0 => 'application/json',
1 => 'text/json',
),
);
public function upload($file, $file_name = '', $cb = null) {
if (!file_exists($file)) throw new \danog\MadelineProto\Exception('Given file does not exist!');
if (empty($file_name)) $file_name = basename($file);
$file_size = filesize($file);
if ($file_size > 1500 * 1024 * 1024) throw new \danog\MadelineProto\Exception('Given file is too big!');
if ($cb === null) $cb = function ($percent) { \danog\MadelineProto\Logger::log('Upload status: '.$percent.'%'); };
$part_size = 512 * 1024;
$part_total_num = (int)ceil($file_size/$part_size);
$part_num = 0;
$method = $file_size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart';
$constructor = $file_size > 10 * 1024 * 1024 ? 'inputFileBig' : 'inputFile';
$file_id = \danog\PHP\Struct::unpack('<q', \phpseclib\Crypt\Random::string(8))[0];
$f = fopen($file, 'r');
fseek($f, 0);
while (ftell($f) !== $file_size) {
if (!$this->API->method_call($method, ['file_id' => $file_id, 'file_part' => $part_num++, 'file_total_parts' => $part_total_num, 'bytes' => stream_get_contents($f, $part_size)])) throw new \danog\MadelineProto\Exception('An error occurred while uploading file part '.$part_num);
$cb(ftell($f) * 100 / $file_size);
}
fclose($f);
return ['_' => $constructor, 'id' => $file_id, 'parts' => $part_total_num, 'name' => $file_name, 'md5_checksum' => md5_file($file)];
}
public function get_extension_from_mime($mime) {
foreach ($this->all_mimes as $key => $value) {
if(array_search($mime,$value) !== false) return '.'.$key;
}
return '';
}
public function get_download_info($message_media) {
$res = [];
switch ($message_media['_']) {
case 'messageMediaPhoto':
$res['caption'] = $message_media['caption'];
$res['ext'] = '.jpg';
$photo = end($message_media['photo']['sizes']);
$res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id'];
$res['size'] = $photo['size'];
$res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret']];
return $res;
case 'messageMediaDocument':
$res['caption'] = $message_media['caption'];
foreach ($message_media['document']['attributes'] as $attribute) {
switch ($attribute['_']) {
case 'documentAttributeFilename':
$pathinfo = pathinfo($attribute['file_name']);
$res['ext'] = '.'.$pathinfo['extension'];
$res['name'] = $pathinfo['filename'];
break;
case 'documentAttributeAudio':
$audio = $attribute;
break;
}
}
if (isset($audio) && isset($audio['title']) && !isset($res['name'])) {
$res['name'] = $audio['title'];
if (isset($audio['performer'])) $res['name'] .= ' - '.$audio['performer'];
}
if (!isset($res['ext'])) $res['ext'] = $this->get_extension_from_mime($message_media['document']['mime_type']);
if (!isset($res['name'])) $res['name'] = $message_media['document']['id'];
$res['name'] .= '_'.$message_media['document']['id'];
$res['size'] = $message_media['document']['size'];
$res['InputFileLocation'] = ['_' => 'inputDocumentFileLocation', 'id' => $message_media['document']['id'], 'access_hash' => $message_media['document']['access_hash'], 'version' => $message_media['document']['version']];
return $res;
default:
throw \danog\MadelineProto\Exception('Invalid constructor provided: '.$message_media['_']);
}
}
public function download_to_dir($message_media, $dir, $cb = null) {
$info = $this->get_download_info($message_media);
return $this->download_to_file($message_media, $dir.'/'.$info['name'].$info['ext'], $cb);
}
public function download_to_file($message_media, $file, $cb = null) {
if (!file_exists($file)) touch($file);
$stream = fopen($file, 'w');
fseek($stream, filesize($file));
$this->download_to_stream($message_media, $stream, $cb);
return $file;
}
public function download_to_stream($message_media, $stream, $cb = null) {
if ($cb === null) $cb = function ($percent) { \danog\MadelineProto\Logger::log('Download status: '.$percent.'%'); };
$info = $this->get_download_info($message_media);
$offset = ftell($stream);
$part_size = 512 * 1024;
while (fwrite($stream, $this->API->method_call('upload.getFile', ['location' => $info['InputFileLocation'], 'offset' => $offset += $part_size, 'limit' => $part_size])['bytes']) != false) {
$cb(ftell($stream) * 100 / $info['size']);
}
return true;
}
}

BIN
swing.mp4 Normal file

Binary file not shown.

View File

@ -46,9 +46,48 @@ $flutter = 'https://storage.pwrtelegram.xyz/pwrtelegrambot/document/file_6570.mp
$mention = $MadelineProto->get_info('@danogentili'); // Returns an array with all of the constructors that can be extracted from a username or an id
$mention = $mention['user_id']; // Selects only the numeric user id
$media = [];
// Photo uploaded as document
$inputFile = $MadelineProto->upload('faust.jpg', 'fausticorn.jpg'); // This gets an inputFile object with file name magic
$media['document_photo'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('faust.jpg'), 'caption' => 'This file was uploaded using MadelineProto', 'attributes' => [['_' => 'documentAttributeImageSize', 'w' => 1280, 'h' => 914]]];
// Photo
$media['photo'] = ['_' => 'inputMediaUploadedPhoto', 'file' => $inputFile, 'mime_type' => mime_content_type('faust.jpg'), 'caption' => 'This photo was uploaded using MadelineProto'];
// GIF
$inputFile = $MadelineProto->upload('pony.mp4');
$media['gif'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('pony.mp4'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeAnimated']]];
// Sticker
$inputFile = $MadelineProto->upload('lel.webp');
$media['sticker'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('lel.webp'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]];
// Video
$inputFile = $MadelineProto->upload('swing.mp4');
$media['video'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('swing.mp4'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeVideo', 'duration' => 5, 'w' => 1280, 'h' => 720]]];
// audio
$inputFile = $MadelineProto->upload('mosconi.mp3');
$media['audio'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('mosconi.mp3'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => false, 'duration' => 1, 'title' => 'AH NON LO SO', 'performer' => 'IL DIO GERARDO MOSCONI']]];
// voice
$media['voice'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('mosconi.mp3'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeAudio', 'voice' => true, 'duration' => 1, 'title' => 'AH NON LO SO', 'performer' => 'IL DIO GERARDO MOSCONI']]];
// Document
$time = time();
$inputFile = $MadelineProto->upload('60', 'magic'); // This gets an inputFile object with file name magic
var_dump(time() - $time);
$media['document'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => 'magic/magic', 'caption' => 'This file was uploaded using MadelineProto', 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'magic.magic']]];
foreach (['@pwrtelegramgroup', '@pwrtelegramgroupita'] as $peer) {
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => strlen($message), 'user_id' => $mention]]]);
\danog\MadelineProto\Logger::log($sentMessage);
foreach ($media as $type => $inputMedia) {
\danog\MadelineProto\Logger::log($MadelineProto->messages->sendMedia(['peer' => $peer, 'media' => $inputMedia]));
}
}
sleep(5);