Skip to content

Commit 24b5af9

Browse files
authored
Merge pull request #1 from devrabie/remove-database-feature
المهام المنجزة:
2 parents 96bf360 + 3ef80ed commit 24b5af9

36 files changed

+148
-2796
lines changed

README.md

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ The Bot can:
8686
- Manage Channel from the bot admin interface.
8787
- Full support for **inline bots**.
8888
- Inline keyboard.
89-
- Messages, InlineQuery and ChosenInlineQuery are stored in the Database.
90-
- Conversation feature.
89+
- Conversation feature (Currently disabled due to database removal).
9190

9291
---
9392

@@ -201,7 +200,7 @@ The bot can handle updates with [**Webhook**](#webhook-installation) or [**getUp
201200
| ---- | :----: | :----: |
202201
| Description | Telegram sends the updates directly to your host | You have to fetch Telegram updates manually |
203202
| Host with https | Required | Not required |
204-
| MySQL | Not required | ([Not](#getupdates-without-database)) Required |
203+
| MySQL | Not required | Not Required |
205204

206205
## Using a custom Bot API server
207206

@@ -290,8 +289,6 @@ Edit *[unset.php]* with your bot credentials and execute it.
290289

291290
## getUpdates installation
292291

293-
For best performance, the MySQL database should be enabled for the `getUpdates` method!
294-
295292
Create *[getUpdatesCLI.php]* with the following contents:
296293
```php
297294
#!/usr/bin/env php
@@ -301,21 +298,10 @@ require __DIR__ . '/vendor/autoload.php';
301298
$bot_api_key = 'your:bot_api_key';
302299
$bot_username = 'username_bot';
303300

304-
$mysql_credentials = [
305-
'host' => 'localhost',
306-
'port' => 3306, // optional
307-
'user' => 'dbuser',
308-
'password' => 'dbpass',
309-
'database' => 'dbname',
310-
];
311-
312301
try {
313302
// Create Telegram API object
314303
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
315304

316-
// Enable MySQL
317-
$telegram->enableMySql($mysql_credentials);
318-
319305
// Handle telegram getUpdates request
320306
$telegram->handleGetUpdates();
321307
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
@@ -334,13 +320,6 @@ Lastly, run it!
334320
$ ./getUpdatesCLI.php
335321
```
336322

337-
### getUpdates without database
338-
339-
If you choose to / or are obliged to use the `getUpdates` method without a database, you can replace the `$telegram->enableMySql(...);` line above with:
340-
```php
341-
$telegram->useGetUpdatesWithoutDatabase();
342-
```
343-
344323
## Filter Update
345324

346325
:exclamation: Note that by default, Telegram will send any new update types that may be added in the future. This may cause commands that don't take this into account to break!
@@ -463,59 +442,14 @@ Get the file path and download it. (see *[WhoamiCommand.php]* for a full example
463442

464443
#### Send message to all active chats
465444

466-
To do this you have to enable the MySQL connection.
467-
Here's an example of use (check [`DB::selectChats()`][DB::selectChats] for parameter usage):
468-
469-
```php
470-
$results = Request::sendToActiveChats(
471-
'sendMessage', // Callback function to execute (see Request.php methods)
472-
['text' => 'Hey! Check out the new features!!'], // Param to evaluate the request
473-
[
474-
'groups' => true,
475-
'supergroups' => true,
476-
'channels' => false,
477-
'users' => true,
478-
]
479-
);
480-
```
481-
482-
You can also broadcast a message to users, from the private chat with your bot. Take a look at the [admin commands](#admin-commands) below.
445+
This feature is currently disabled due to the removal of database functionality.
483446

484447
## Utils
485448

486449
### MySQL storage (Recommended)
487450

488-
If you want to save messages/users/chats for further usage in commands, create a new database (`utf8mb4_unicode_520_ci`), import *[structure.sql]* and enable MySQL support BEFORE `handle()` method:
451+
Database storage functionality has been removed from this library.
489452

490-
```php
491-
$mysql_credentials = [
492-
'host' => 'localhost',
493-
'port' => 3306, // optional
494-
'user' => 'dbuser',
495-
'password' => 'dbpass',
496-
'database' => 'dbname',
497-
];
498-
499-
$telegram->enableMySql($mysql_credentials);
500-
```
501-
502-
You can set a custom prefix to all the tables while you are enabling MySQL:
503-
504-
```php
505-
$telegram->enableMySql($mysql_credentials, $bot_username . '_');
506-
```
507-
508-
You can also store inline query and chosen inline query data in the database.
509-
510-
#### External Database connection
511-
512-
It is possible to provide the library with an external MySQL PDO connection.
513-
Here's how to configure it:
514-
515-
```php
516-
$telegram->enableExternalMySql($external_pdo_connection);
517-
//$telegram->enableExternalMySql($external_pdo_connection, $table_prefix)
518-
```
519453
### Channels Support
520454

521455
All methods implemented can be used to manage channels.
@@ -566,13 +500,13 @@ $telegram->setCommandConfig('weather', [
566500

567501
### Admin Commands
568502

569-
Enabling this feature, the bot admin can perform some super user commands like:
570-
- List all the chats started with the bot */chats*
571-
- Clean up old database entries */cleanup*
503+
Some admin commands that relied on database functionality have been disabled or removed:
504+
- List all the chats started with the bot */chats* (Disabled)
505+
- Clean up old database entries */cleanup* (Disabled)
572506
- Show debug information about the bot */debug*
573507
- Send message to all chats */sendtoall*
574508
- Post any content to your channels */sendtochannel*
575-
- Inspect a user or a chat with */whois*
509+
- Inspect a user or a chat with */whois* (Removed)
576510

577511
Take a look at all default admin commands stored in the *[src/Commands/AdminCommands/][AdminCommands-folder]* folder.
578512

@@ -711,8 +645,8 @@ Credit list in [CREDITS](CREDITS)
711645
[WhoamiCommand.php]: https://github.com/php-telegram-bot/example-bot/blob/master/Commands/WhoamiCommand.php "example /whoami command"
712646
[HelpCommand.php]: https://github.com/php-telegram-bot/example-bot/blob/master/Commands/HelpCommand.php "example /help command"
713647
[SendtochannelCommand.php]: https://github.com/php-telegram-bot/core/blob/master/src/Commands/AdminCommands/SendtochannelCommand.php "/sendtochannel admin command"
714-
[DB::selectChats]: https://github.com/php-telegram-bot/core/blob/0.70.0/src/DB.php#L1148 "DB::selectChats() parameters"
715-
[structure.sql]: https://github.com/php-telegram-bot/core/blob/master/structure.sql "DB structure for importing"
648+
[DB::selectChats]: # "DB::selectChats() parameters (Removed)"
649+
[structure.sql]: # "DB structure for importing (Removed)"
716650
[Wiki]: https://github.com/php-telegram-bot/core/wiki "PHP Telegram Bot Wiki"
717651
[wiki-create-your-own-commands]: https://github.com/php-telegram-bot/core/wiki/Create-your-own-commands "Create your own commands"
718652
[issues]: https://github.com/php-telegram-bot/core/issues "PHP Telegram Bot Issues"

src/Commands/AdminCommands/ChatsCommand.php

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Longman\TelegramBot\Commands\AdminCommand;
1515
use Longman\TelegramBot\DB;
16-
use Longman\TelegramBot\Entities\Chat;
1716
use Longman\TelegramBot\Entities\ServerResponse;
1817
use Longman\TelegramBot\Exception\TelegramException;
1918
use Longman\TelegramBot\Request;
@@ -53,91 +52,8 @@ class ChatsCommand extends AdminCommand
5352
*/
5453
public function execute(): ServerResponse
5554
{
56-
$message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost();
55+
$usage = $this->getUsage();
5756

58-
$chat_id = $message->getChat()->getId();
59-
$text = trim($message->getText(true));
60-
61-
$results = DB::selectChats([
62-
'groups' => true,
63-
'supergroups' => true,
64-
'channels' => true,
65-
'users' => true,
66-
'text' => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
67-
]);
68-
69-
$user_chats = 0;
70-
$group_chats = 0;
71-
$supergroup_chats = 0;
72-
$channel_chats = 0;
73-
74-
if ($text === '') {
75-
$text_back = '';
76-
} elseif ($text === '*') {
77-
$text_back = 'List of all bot chats:' . PHP_EOL;
78-
} else {
79-
$text_back = 'Chat search results:' . PHP_EOL;
80-
}
81-
82-
if (is_array($results)) {
83-
foreach ($results as $result) {
84-
//Initialize a chat object
85-
$result['id'] = $result['chat_id'];
86-
$chat = new Chat($result);
87-
88-
$whois = $chat->getId();
89-
if ($this->telegram->getCommandObject('whois')) {
90-
// We can't use '-' in command because part of it will become unclickable
91-
$whois = '/whois' . str_replace('-', 'g', $chat->getId());
92-
}
93-
94-
if ($chat->isPrivateChat()) {
95-
if ($text !== '') {
96-
$text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
97-
}
98-
99-
++$user_chats;
100-
} elseif ($chat->isSuperGroup()) {
101-
if ($text !== '') {
102-
$text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
103-
}
104-
105-
++$supergroup_chats;
106-
} elseif ($chat->isGroupChat()) {
107-
if ($text !== '') {
108-
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
109-
}
110-
111-
++$group_chats;
112-
} elseif ($chat->isChannel()) {
113-
if ($text !== '') {
114-
$text_back .= '- C ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
115-
}
116-
117-
++$channel_chats;
118-
}
119-
}
120-
}
121-
122-
if (($user_chats + $group_chats + $supergroup_chats) === 0) {
123-
$text_back = 'No chats found..';
124-
} else {
125-
$text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
126-
$text_back .= PHP_EOL . 'Groups: ' . $group_chats;
127-
$text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats;
128-
$text_back .= PHP_EOL . 'Channels: ' . $channel_chats;
129-
$text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats);
130-
131-
if ($text === '') {
132-
$text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
133-
}
134-
}
135-
136-
$data = [
137-
'chat_id' => $chat_id,
138-
'text' => $text_back,
139-
];
140-
141-
return Request::sendMessage($data);
57+
return $this->replyToChat(sprintf('This command is not available because the database feature has been removed.%sUsage: %s', PHP_EOL, $usage));
14258
}
14359
}

src/Commands/AdminCommands/CleanupCommand.php

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Longman\TelegramBot\Commands\AdminCommands;
1313

1414
use Longman\TelegramBot\Commands\AdminCommand;
15-
use Longman\TelegramBot\DB;
1615
use Longman\TelegramBot\Entities\ServerResponse;
1716
use Longman\TelegramBot\Exception\TelegramException;
1817
use Longman\TelegramBot\Request;
@@ -365,71 +364,6 @@ public function executeNoDb(): ServerResponse
365364
*/
366365
public function execute(): ServerResponse
367366
{
368-
$message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost();
369-
$text = $message->getText(true);
370-
371-
// Dry run?
372-
$dry_run = strpos($text, 'dry') !== false;
373-
$text = trim(str_replace('dry', '', $text));
374-
375-
$settings = $this->getSettings($text);
376-
$queries = $this->getQueries($settings);
377-
378-
if ($dry_run) {
379-
return $this->replyToUser('Queries:' . PHP_EOL . implode(PHP_EOL, $queries));
380-
}
381-
382-
$infos = [];
383-
foreach ($settings['tables_to_clean'] as $table) {
384-
$info = "*{$table}*";
385-
386-
if (isset($settings['clean_older_than'][$table])) {
387-
$info .= " ({$settings['clean_older_than'][$table]})";
388-
}
389-
390-
$infos[] = $info;
391-
}
392-
393-
$data = [
394-
'chat_id' => $message->getFrom()->getId(),
395-
'parse_mode' => 'Markdown',
396-
];
397-
398-
$data['text'] = 'Cleaning up tables:' . PHP_EOL . implode(PHP_EOL, $infos);
399-
Request::sendMessage($data);
400-
401-
$rows = 0;
402-
$pdo = DB::getPdo();
403-
try {
404-
$pdo->beginTransaction();
405-
406-
foreach ($queries as $query) {
407-
// Delete in chunks to not block / improve speed on big tables.
408-
$query .= ' LIMIT 10000';
409-
while ($dbq = $pdo->query($query)) {
410-
if ($dbq->rowCount() === 0) {
411-
continue 2;
412-
}
413-
$rows += $dbq->rowCount();
414-
}
415-
416-
TelegramLog::error('Error while executing query: ' . $query);
417-
}
418-
419-
// commit changes to the database and end transaction
420-
$pdo->commit();
421-
422-
$data['text'] = "*Database cleanup done!* _(removed {$rows} rows)_";
423-
} catch (PDOException $e) {
424-
$data['text'] = '*Database cleanup failed!* _(check your error logs)_';
425-
426-
// rollback changes on exception
427-
// useful if you want to track down error you can't replicate it when some of the data is already deleted
428-
$pdo->rollBack();
429-
430-
TelegramLog::error($e->getMessage());
431-
}
432-
433-
return Request::sendMessage($data);
367+
return $this->replyToChat('This command is not available because the database feature has been removed.' . PHP_EOL . 'Type /debug to see all available information.');
434368
}
435369
}

src/Commands/AdminCommands/DebugCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Exception;
1515
use Longman\TelegramBot\Commands\AdminCommand;
16-
use Longman\TelegramBot\DB;
1716
use Longman\TelegramBot\Entities\ServerResponse;
1817
use Longman\TelegramBot\Exception\TelegramException;
1918
use Longman\TelegramBot\Request;
@@ -51,7 +50,6 @@ class DebugCommand extends AdminCommand
5150
*/
5251
public function execute(): ServerResponse
5352
{
54-
$pdo = DB::getPdo();
5553
$message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost();
5654
$chat = $message->getChat();
5755
$text = strtolower($message->getText(true));
@@ -83,8 +81,7 @@ public function execute(): ServerResponse
8381
$debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS);
8482
$debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time'));
8583

86-
$mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null;
87-
$debug_info[] = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled');
84+
$debug_info[] = sprintf('*MySQL version:* `%s`', 'disabled');
8885

8986
$debug_info[] = sprintf('*Operating System:* `%s`', php_uname());
9087

0 commit comments

Comments
 (0)