Skip to content

Commit

Permalink
Cleanup examples
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jul 8, 2023
1 parent 30890ae commit 9f4a1d5
Show file tree
Hide file tree
Showing 7 changed files with 6,124 additions and 275 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MadelineProto was updated (8.0.0-beta100)!

Features:
- Thanks to the many translation contributors @ https://weblate.madelineproto.xyz/, MadelineProto is now fully localized in Hebrew, Persian, Kurdish, Uzbek and Italian, with WIP translations in Russian and French!
- You can now use callFork to fork a new green thread!
- The `waveform` attribute of `Voice` objects is now automatically encoded and decoded to an array of 100 integer values!
- Added a custom PeerNotInDbException class for "This peer is not present in the internal peer database" errors
- Added a `label` property to the Button class, directly indicating the button label (instead of manually fetching it as an array key).
- Added `isForum` method to check whether a given supergroup is a forum
- Added `entitiesToHtml` method to convert a message and a set of Telegram entities to an HTML string!
- Added `wrapUpdate`, `wrapMessage`, `wrapMedia`
- You can now use `reportMemoryProfile()` to generate and send a `pprof` memory profile to all report peers to debug the causes of high memory usage.


Fixes:
- Fixed file uploads with uv
- Many performance improvements and bugfixes!
- Improve background broadcasting with the broadcast API using a pre-defined list of `whitelist` IDs!
- Broadcast IDs are now unique across multiple broadcasts, even if previous broadcasts already completed their ID will never be re-used.
- Now uploadMedia, sendMedia and upload directly accept PHP memory stream resources, to upload files from buffers.
56 changes: 0 additions & 56 deletions examples/database-config-examples/memory.php

This file was deleted.

57 changes: 0 additions & 57 deletions examples/database-config-examples/mysql.php

This file was deleted.

57 changes: 0 additions & 57 deletions examples/database-config-examples/postgres.php

This file was deleted.

56 changes: 0 additions & 56 deletions examples/database-config-examples/redis.php

This file was deleted.

71 changes: 22 additions & 49 deletions examples/downloadRenameBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,25 @@ public function getReportPeers()
* @var array
*/
private $states = [];
/**
* Constructor.
*
* @param ?API $API API
*/
public function __construct(?APIWrapper $API)
{
$this->UPLOAD = class_exists(HttpServer::class);
parent::__construct($API);
}
public function onStart()
{
$this->adminId = yield $this->getInfo(self::ADMIN)['bot_api_id'];
$this->adminId = $this->getInfo(self::ADMIN)['bot_api_id'];
}
/**
* Handle updates from channels and supergroups.
*
* @param array $update Update
*/
public function onUpdateNewChannelMessage(array $update): Generator
public function onUpdateNewChannelMessage(array $update)
{
//yield $this->onUpdateNewMessage($update);
//$this->onUpdateNewMessage($update);
}
/**
* Handle updates from users.
*
* @param array $update Update
*/
public function onUpdateNewMessage(array $update): Generator
public function onUpdateNewMessage(array $update)
{
if ($update['message']['out'] ?? false) {
return;
Expand All @@ -117,34 +107,19 @@ public function onUpdateNewMessage(array $update): Generator
}

try {
$peer = yield $this->getInfo($update);
$peer = $this->getInfo($update);
$peerId = $peer['bot_api_id'];
$messageId = $update['message']['id'];

if ($this->UPLOAD && $update['message']['message'] === '/getUrl') {
yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a file: ', 'reply_to_msg_id' => $messageId]);
$this->states[$peerId] = $this->UPLOAD;
return;
}
if ($update['message']['message'] === '/start') {
return $this->messages->sendMessage(['peer' => $peerId, 'message' => self::START, 'parse_mode' => 'Markdown', 'reply_to_msg_id' => $messageId]);
}
if ($update['message']['message'] === '/report' && $peerId === $this->adminId) {
memprof_dump_callgrind($stm = fopen("php://memory", "w"));
fseek($stm, 0);
yield $this->messages->sendMedia(['peer' => $peerId, 'media' => ['_' => 'inputMediaUploadedDocument', 'file' => $stm, 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'callgrind.out']]]]);
fclose($stm);
$this->reportMemoryProfile();
return;
}
if (isset($update['message']['media']['_']) && $update['message']['media']['_'] !== 'messageMediaWebPage') {
if ($this->UPLOAD && ($this->states[$peerId] ?? false) === $this->UPLOAD) {
unset($this->states[$peerId]);
$update = Files::extractBotAPIFile(yield $this->MTProtoToBotAPI($update));
$file = [$update['file_size'], $update['mime_type']];
var_dump($update['file_id'].'.'.Tools::base64urlEncode(json_encode($file))."/".$update['file_name']);
return;
}
yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a new name for this file: ', 'reply_to_msg_id' => $messageId]);
$this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a new name for this file: ', 'reply_to_msg_id' => $messageId]);
$this->states[$peerId] = $update['message']['media'];

return;
Expand All @@ -164,7 +139,7 @@ public function onUpdateNewMessage(array $update): Generator
$url = "http://$url";
}
}
$id = yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Preparing...', 'reply_to_msg_id' => $messageId]);
$id = $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Preparing...', 'reply_to_msg_id' => $messageId]);
if (!isset($id['id'])) {
$this->report(json_encode($id));
foreach ($id['updates'] as $updat) {
Expand All @@ -189,31 +164,29 @@ function ($progress, $speed, $time) use ($peerId, $id) {
}
$prev = $now;
try {
yield $this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => "Upload progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"], ['FloodWaitLimit' => 0]);
$this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => "Upload progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"], ['FloodWaitLimit' => 0]);
} catch (RPCErrorException $e) {
}
},
);
yield $this->messages->sendMedia(
[
'peer' => $peerId,
'reply_to_msg_id' => $messageId,
'media' => [
'_' => 'inputMediaUploadedDocument',
'file' => $url,
'attributes' => [
['_' => 'documentAttributeFilename', 'file_name' => $name],
],
$this->messages->sendMedia(
peer: $peerId,
reply_to_msg_id: $messageId,
media: [
'_' => 'inputMediaUploadedDocument',
'file' => $url,
'attributes' => [
['_' => 'documentAttributeFilename', 'file_name' => $name],
],
'message' => 'Powered by @MadelineProto!',
'parse_mode' => 'Markdown',
],
message: 'Powered by @MadelineProto!',
parse_mode: 'Markdown',
);

if (in_array($peer['type'], ['channel', 'supergroup'])) {
yield $this->channels->deleteMessages(['channel' => $peerId, 'id' => [$id]]);
$this->channels->deleteMessages(['channel' => $peerId, 'id' => [$id]]);
} else {
yield $this->messages->deleteMessages(['revoke' => true, 'id' => [$id]]);
$this->messages->deleteMessages(['revoke' => true, 'id' => [$id]]);
}
} catch (Throwable $e) {
if (strpos($e->getMessage(), 'Could not connect to URI') === false && !($e instanceof UriException) && strpos($e->getMessage(), 'URI') === false) {
Expand All @@ -224,7 +197,7 @@ function ($progress, $speed, $time) use ($peerId, $id) {
$this->report(json_encode($url));
}
try {
yield $this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => 'Error: '.$e->getMessage()]);
$this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => 'Error: '.$e->getMessage()]);
} catch (Throwable $e) {
$this->logger((string) $e, Logger::FATAL_ERROR);
}
Expand Down
Loading

0 comments on commit 9f4a1d5

Please sign in to comment.