diff --git a/composer.json b/composer.json index 8cdb377..f48734f 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "twig/extra-bundle": "^3.7", "twig/intl-extra": "^3.10", "nyholm/psr7": "^1.8", - "react/filesystem": "^0.1.2" + "react/filesystem": "^0.1.2", + "react/async": "^4.3" }, "suggest": { "mirzaev/files": "Easy working with files", diff --git a/composer.lock b/composer.lock index b19865c..1a3413a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "330a5fd85753f4fb8d74b9564bae298d", + "content-hash": "f2c3677e133cdc48eb5a0ac9a778ed10", "packages": [ { "name": "badfarm/zanzara", @@ -1695,6 +1695,81 @@ }, "time": "2021-05-03T11:20:27+00:00" }, + { + "name": "react/async", + "version": "v4.3.0", + "source": { + "type": "git", + "url": "https://github.com/reactphp/async.git", + "reference": "635d50e30844a484495713e8cb8d9e079c0008a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/reactphp/async/zipball/635d50e30844a484495713e8cb8d9e079c0008a5", + "reference": "635d50e30844a484495713e8cb8d9e079c0008a5", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "react/event-loop": "^1.2", + "react/promise": "^3.2 || ^2.8 || ^1.2.1" + }, + "require-dev": { + "phpstan/phpstan": "1.10.39", + "phpunit/phpunit": "^9.6" + }, + "type": "library", + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "React\\Async\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Lück", + "email": "christian@clue.engineering", + "homepage": "https://clue.engineering/" + }, + { + "name": "Cees-Jan Kiewiet", + "email": "reactphp@ceesjankiewiet.nl", + "homepage": "https://wyrihaximus.net/" + }, + { + "name": "Jan Sorgalla", + "email": "jsorgalla@gmail.com", + "homepage": "https://sorgalla.com/" + }, + { + "name": "Chris Boden", + "email": "cboden@gmail.com", + "homepage": "https://cboden.dev/" + } + ], + "description": "Async utilities and fibers for ReactPHP", + "keywords": [ + "async", + "reactphp" + ], + "support": { + "issues": "https://github.com/reactphp/async/issues", + "source": "https://github.com/reactphp/async/tree/v4.3.0" + }, + "funding": [ + { + "url": "https://opencollective.com/reactphp", + "type": "open_collective" + } + ], + "time": "2024-06-04T14:40:02+00:00" + }, { "name": "react/cache", "version": "v1.2.0", diff --git a/examples/crontab/records.sh b/examples/crontab/records.sh new file mode 100644 index 0000000..9c69f30 --- /dev/null +++ b/examples/crontab/records.sh @@ -0,0 +1 @@ +*/10 * * * * php /var/www/campanula/garden/campanula/system/public/telegram_week.php diff --git a/garden/campanula/system/databases/scripts/message.php b/garden/campanula/system/databases/scripts/message.php new file mode 100644 index 0000000..40f2b49 --- /dev/null +++ b/garden/campanula/system/databases/scripts/message.php @@ -0,0 +1,57 @@ +database->read( + filter: fn(record $record) => $record->sended === 1 && $record->identifier_gammu === 21, + amount: 3, + offset: 0 +); + +var_dump($message); diff --git a/garden/campanula/system/models/message.php b/garden/campanula/system/models/message.php new file mode 100644 index 0000000..0c0dead --- /dev/null +++ b/garden/campanula/system/models/message.php @@ -0,0 +1,145 @@ + + */ +final class message extends core implements record_interface +{ + use record_trait; + + /** + * File + * + * @var string $database Path to the database file + */ + protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'messages.baza'; + + /** + * Database + * + * @var database $database The database + */ + public protected(set) database $database; + + /** + * Constructor + * + * @method record|null $record The record + * + * @return void + */ + public function __construct(?record $record = null) + { + // Initializing the database + $this->database = new database() + ->encoding(encoding::utf8) + ->columns( + new column('identifier', type::long_long_unsigned), + new column('identifier_gammu', type::long_long_unsigned), + new column('sender', type::string, ['length' => 20]), + new column('date', type::string, ['length' => 32]), + new column('text', type::string, ['length' => 128]), + new column('sended', type::char), + new column('updated', type::integer_unsigned), + new column('created', type::integer_unsigned) + ) + ->connect($this->file); + + // Initializing the record + $record instanceof record and $this->record = $record; + } + + /** + * Write + * + * @param int $identifier_gammu The gammu sms identifier + * @param string $sender + * @param string $date Timestamp + * @param string $text + * @param bool $sended Is the message was sended? + * + * @return int|false The record identifier, if created + */ + public function write( + int $identifier_gammu, + string $sender = '', + string $date = '', + string $text = '', + bool $sended = false + ): int|false { + $record = $this->database->record( + $this->database->count() + 1, + $identifier_gammu, + $sender, + $date, + $text, + (int) $sended, + svoboda::timestamp(), + svoboda::timestamp() + ); + + // Writing the record into the database + $created = $this->database->write($record); + + // Exit (success) + return $created ? $record->identifier : false; + } + + /** + * Serialize + * + * @return self The instance from which the method was called (fluent interface) + */ + public function serialize(): self + { + // Serializing the record parameters + $this->record->sended = (int) $this->record->sended; + + // Exit (success) + return $this; + } + + /** + * Deserialize + * + * @return self The instance from which the method was called (fluent interface) + */ + public function deserialize(): self + { + // Deserializing the record parameters + $this->record->sended = (bool) $this->record->sended; + + // Exit (success) + return $this; + } +} diff --git a/garden/campanula/system/public/telegram_week.php b/garden/campanula/system/public/telegram_week.php new file mode 100644 index 0000000..c00f67f --- /dev/null +++ b/garden/campanula/system/public/telegram_week.php @@ -0,0 +1,153 @@ +setParseMode(config::PARSE_MODE_MARKDOWN); +$config->useReactFileSystem(true); + +// Initializing the robot +$robot = new Zanzara(ROBOT['key'], $config); + +// Initializing the request +$request = $gammu->prepare('SELECT * FROM `' . GAMMU['table_inbox'] . '` WHERE `' . GAMMU['table_inbox'] . '`.`ReceivingDateTime` <= NOW() + interval 1 week ORDER BY `inbox`.`ReceivingDateTime` ASC'); + +// Initializing the request parameters +$parameters = []; + +// Sending the request +$request->execute($parameters); + +// Fetching the response +$response = (array) $request->fetchAll(pdo::FETCH_ASSOC); + +foreach ($response as $message) { + // Iterating over messages from last week + + // Searching for the message + $model = new message()->read(filter: fn(record $record) => $record->sended === 1 && $record->identifier_gammu === (int) $message['ID']); + + if ($model instanceof message) { + // The message was sent + } else { + // The message was not send + + // Initializing the message parameters + $sender = unmarkdown($message['SenderNumber']); + $date = unmarkdown($message['ReceivingDateTime']); + $text = unmarkdown($message['TextDecoded']); + + // Sending the message + await($robot->getTelegram()->sendMessage( + << ROBOT['receiver'], + 'link_preview_options' => [ + 'is_disabled' => true + ], + 'disable_notification' => false + ] + )->then(function () use ($message, $sender, $date, $text) { + // Sended the message + + // Initializing the model + $model = new message(); + + // Creating the message + $model->write( + identifier_gammu: $message['ID'], + sender: $sender, + date: $date, + text: $text, + sended: true + ); + })); + + sleep(1); + } +} diff --git a/garden/campanula/system/settings/gammu.php.sample b/garden/campanula/system/settings/gammu.php.sample new file mode 100644 index 0000000..de34fde --- /dev/null +++ b/garden/campanula/system/settings/gammu.php.sample @@ -0,0 +1,12 @@ + 'mysql', + 'server' => 'localhost', + 'database' => '', + 'name' => '', + 'password' => '' + + 'table_inbox' => 'inbox' +]); diff --git a/garden/campanula/system/settings/telegram.php.sample b/garden/campanula/system/settings/telegram.php.sample index 6bfcb66..9d10388 100644 --- a/garden/campanula/system/settings/telegram.php.sample +++ b/garden/campanula/system/settings/telegram.php.sample @@ -3,5 +3,6 @@ // Robot define('ROBOT', [ 'identifier' => null, - 'key' => '' + 'key' => '', + 'receivers' => '' // telegram identifier (example: '8138746766') ]);