Skip to content

Commit 95f8b72

Browse files
authored
Merge pull request #4 from WizardLoop/dev
[v1.0.3]
2 parents a42ab8b + 0f56343 commit 95f8b72

4 files changed

Lines changed: 85 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ All notable changes to the **GetAnyMessage** project will be documented in this
2525
- Update statUsers in: **src/Handlers/AdminPanel.php**
2626

2727
---
28+
29+
## [v1.0.3] - 2026-03-04
30+
31+
### Added:
32+
- MySQL session support for storing bot sessions on servers with ephemeral file systems.
33+
- Restart plugin to allow programmatic bot restarts.
34+
35+
---

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,24 @@ Copy `.env.example` to `.env` and fill in:
7272
cp .env.example .env
7373
```
7474

75-
- `API_ID`
76-
- `API_HASH`
77-
- `BOT_TOKEN`
78-
- `ADMIN_ID`
75+
**Telegram Bot Settings:**
76+
77+
- `API_ID` – Your API ID from [my.telegram.org](https://my.telegram.org)
78+
- `API_HASH` – Your API hash from [my.telegram.org](https://my.telegram.org)
79+
- `BOT_TOKEN` – Your bot token from [@BotFather](https://t.me/BotFather)
80+
- `ADMIN` – Your username or user ID. Multiple admins supported, comma-separated (e.g., `1234,12345`)
81+
- `BOT_NAME` – Your bot's name (default: `GetAnyMessage`)
82+
83+
**Database Settings (Optional, for MySQL session storage):**
84+
85+
- `DB_FLAG` – Enable MySQL session storage (`yes`) or disable (`no` / leave empty)
86+
- `DB_HOST` – MySQL host (default: `localhost`)
87+
- `DB_PORT` – MySQL port (default: `3306`)
88+
- `DB_USER` – MySQL username
89+
- `DB_PASS` – MySQL password
90+
- `DB_NAME` – MySQL database name
91+
92+
> ⚠️ If `DB_FLAG` is `no` or empty, all DB_* variables are ignored and the bot will use file-based session storage.
7993
8094
---
8195

src/.env.example

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
API_ID=your_api_id # [api id from my.telegram.org]
22
API_HASH=your_api_hash # [api hash from my.telegram.org]
3-
BOT_TOKEN=your_bot_token # [for the main bot]
3+
BOT_TOKEN=your_bot_token
44
ADMIN=your_username_or_id # [can be multiple admins just add: , ex: 1234, 12345]
5+
BOT_NAME=GetAnyMessage
6+
DB_FLAG=yes # can be set no or null
7+
DB_HOST=localhost # can be null if DB_FLAG=no
8+
DB_PORT=3306 # can be null if DB_FLAG=no
9+
DB_USER=mpuser # can be null if DB_FLAG=no
10+
DB_PASS=mp_pass # can be null if DB_FLAG=no
11+
DB_NAME=madelineproto # can be null if DB_FLAG=no

src/bot.php

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -493,27 +493,27 @@ public function send_broadcast(callbackQuery $query): void {
493493
);
494494

495495
$sentMessage2 = $this->extractMessageId($sentMessage);
496-
Amp\File\write(__DIR__."/data/messagetoeditbroadcast1.txt", "$sentMessage2");
497-
Amp\File\write(__DIR__."/data/messagetoeditbroadcast2.txt", "$senderId");
496+
\Amp\File\write(__DIR__."/data/messagetoeditbroadcast1.txt", "$sentMessage2");
497+
\Amp\File\write(__DIR__."/data/messagetoeditbroadcast2.txt", "$senderId");
498498

499499
if (file_exists(__DIR__."/data/$senderId/txt.txt")) {
500-
$filexmsgidtxt = Amp\File\read(__DIR__."/data/$senderId/txt.txt");
500+
$filexmsgidtxt = \Amp\File\read(__DIR__."/data/$senderId/txt.txt");
501501
}else{
502502
$filexmsgidtxt = null;
503503
}
504504
if (file_exists(__DIR__."/data/$senderId/ent.txt")) {
505-
$filexmsgident = json_decode(Amp\File\read(__DIR__."/data/$senderId/ent.txt"),true);
505+
$filexmsgident = json_decode(\Amp\File\read(__DIR__."/data/$senderId/ent.txt"),true);
506506
}else{
507507
$filexmsgident = null;
508508
}
509509
if (file_exists(__DIR__."/data/$senderId/media.txt")) {
510-
$filexmsgidmedia = Amp\File\read(__DIR__."/data/$senderId/media.txt");
510+
$filexmsgidmedia = \Amp\File\read(__DIR__."/data/$senderId/media.txt");
511511
}else{
512512
$filexmsgidmedia = null;
513513
}
514514

515515
if (file_exists(__DIR__."/data/broadcastsend.txt")) {
516-
$check2 = Amp\File\read(__DIR__."/data/broadcastsend.txt");
516+
$check2 = \Amp\File\read(__DIR__."/data/broadcastsend.txt");
517517
if($check2 == "USERS"){
518518

519519

@@ -811,9 +811,9 @@ public function handleBroadcastProgress(Progress $progress): void {
811811
if (time() - $this->lastLog > 5 || $progress->status === Status::GATHERING_PEERS) {
812812
$this->lastLog = time();
813813
if (file_exists(__DIR__."/data/messagetoeditbroadcast2.txt")) {
814-
$filexmsgid1 = Amp\File\read(__DIR__."/data/messagetoeditbroadcast2.txt");
814+
$filexmsgid1 = \Amp\File\read(__DIR__."/data/messagetoeditbroadcast2.txt");
815815
if (file_exists(__DIR__."/data/messagetoeditbroadcast1.txt")) {
816-
$filexmsgid2 = Amp\File\read(__DIR__."/data/messagetoeditbroadcast1.txt");
816+
$filexmsgid2 = \Amp\File\read(__DIR__."/data/messagetoeditbroadcast1.txt");
817817
try {
818818
$this->messages->editMessage(peer: $filexmsgid1, id: $filexmsgid2, message: "$progressStr", reply_markup: null);
819819
} catch (Throwable $e) {}
@@ -824,7 +824,7 @@ public function handleBroadcastProgress(Progress $progress): void {
824824
if (time() - $this->lastLog > 5 || $progress->status === Status::FINISHED) {
825825
$this->lastLog = time();
826826
if (file_exists(__DIR__."/data/broadcastsend.txt")) {
827-
$broadcast_send = Amp\File\read(__DIR__."/data/broadcastsend.txt");
827+
$broadcast_send = \Amp\File\read(__DIR__."/data/broadcastsend.txt");
828828
}
829829
if (!file_exists(__DIR__."/data/broadcastsend.txt")) {
830830
$broadcast_send = "ALL";
@@ -835,10 +835,10 @@ public function handleBroadcastProgress(Progress $progress): void {
835835
$sucessCount2 = $progress->failCount;
836836

837837
if (file_exists(__DIR__."/data/messagetoeditbroadcast2.txt")) {
838-
$filexmsgid1 = Amp\File\read(__DIR__."/data/messagetoeditbroadcast2.txt");
838+
$filexmsgid1 = \Amp\File\read(__DIR__."/data/messagetoeditbroadcast2.txt");
839839

840840
if (file_exists(__DIR__."/data/messagetoeditbroadcast1.txt")) {
841-
$filexmsgid2 = Amp\File\read(__DIR__."/data/messagetoeditbroadcast1.txt");
841+
$filexmsgid2 = \Amp\File\read(__DIR__."/data/messagetoeditbroadcast1.txt");
842842

843843
$bot_API_markup = ['inline_keyboard' => [[['text'=>"🔙",'callback_data'=>"admin_back"]]]];
844844

@@ -864,31 +864,61 @@ public function handleBroadcastProgress(Progress $progress): void {
864864
} catch (Throwable $e) {}
865865
}
866866

867-
867+
public static function getPlugins(): array {
868+
return [\danog\MadelineProto\EventHandler\Plugin\RestartPlugin::class];
869+
}
870+
public static function getPluginPaths(): string|array|null {
871+
return null;
872+
}
868873
}
869874

870875
function RunBot(): void {
871876
try {
872-
873-
$env = parse_ini_file(__DIR__ . '/.env');
874-
877+
$env = parse_ini_file(__DIR__."/".'.env');
875878
if (!isset($env['API_ID'], $env['API_HASH'], $env['BOT_TOKEN'])) {
876879
die("Missing environment variables in .env\n");
877880
}
878881

879-
$API_ID = $env['API_ID'];
880-
$API_HASH = $env['API_HASH'];
882+
$API_ID = $env['API_ID'];
883+
$API_HASH = $env['API_HASH'];
881884
$BOT_TOKEN = $env['BOT_TOKEN'];
885+
$DB_FLAG = $env['DB_FLAG'] ?? 'no';
886+
$BOT_NAME = $env['BOT_NAME'] ?? 'GetAnyMessage';
887+
$dbHost = $env['DB_HOST'] ?? 'localhost';
888+
$dbPort = $env['DB_PORT'] ?? 3306;
889+
$dbUser = $env['DB_USER'] ?? 'mpuser';
890+
$dbPass = $env['DB_PASS'] ?? 'mp_pass';
891+
$dbName = $env['DB_NAME'] ?? 'madelineproto';
882892

883893
$settings = new \danog\MadelineProto\Settings;
884-
885894
$settings->setAppInfo((new \danog\MadelineProto\Settings\AppInfo)->setApiId((int)$API_ID)->setApiHash($API_HASH));
886895

887-
GetAnyMessage::startAndLoopBot(__DIR__.'/bot.madeline', $BOT_TOKEN, $settings);
896+
$connection = (new \danog\MadelineProto\Settings\Connection())->setTimeout(600.0)->setRetry(true)->setMaxMediaSocketCount(1000);
897+
$settings->setConnection($connection);
898+
899+
$files = (new \danog\MadelineProto\Settings\Files())->setUploadParallelChunks(7)->setDownloadParallelChunks(12);
900+
$settings->setFiles($files);
901+
902+
$logger = (new \danog\MadelineProto\Settings\Logger)->setLevel(\danog\MadelineProto\Logger::ERROR);
903+
$settings->setLogger($logger);
904+
905+
if($DB_FLAG != 'no'){
906+
$db = (new \danog\MadelineProto\Settings\Database\Mysql())
907+
->setUri("tcp://$dbHost:$dbPort")
908+
->setUsername($dbUser)
909+
->setPassword($dbPass)
910+
->setDatabase($dbName)
911+
->setEphemeralFilesystemPrefix("Session_{$BOT_NAME}");
912+
$settings->setDb($db);
913+
}
914+
915+
GetAnyMessage::startAndLoopBot(__DIR__."/bot_{$BOT_NAME}.madeline", $BOT_TOKEN, $settings);
888916

889917
} catch (\Throwable $e) {
890-
// if ($e instanceof \Amp\TimeoutException) {
891-
echo "\n" . $e->getMessage() . "\n";
918+
919+
if (strpos($e->getMessage(), 'bad_msg_notification') !== false) exit(1);
920+
if ($e instanceof \Amp\TimeoutException || $e instanceof \Amp\CancelledException) exit(1);
921+
892922
}
893923
}
894924
RunBot();

0 commit comments

Comments
 (0)