From c45f96a784f15cff49d61f7da068eced4bfe9e5c Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Wed, 15 May 2024 00:31:46 +0200 Subject: [PATCH] Add a new GitHub workflow "Run test bot on Laravel app" --- .github/workflows/run-bot-on-laravel-app.yml | 34 +++++++++++++++++ tests/ci/console.php | 39 ++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 .github/workflows/run-bot-on-laravel-app.yml create mode 100644 tests/ci/console.php diff --git a/.github/workflows/run-bot-on-laravel-app.yml b/.github/workflows/run-bot-on-laravel-app.yml new file mode 100644 index 0000000..519d148 --- /dev/null +++ b/.github/workflows/run-bot-on-laravel-app.yml @@ -0,0 +1,34 @@ +name: Run test bot on Laravel app + +on: + workflow_dispatch: + +jobs: + + tests: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: curl, bcmath, libxml, mbstring, fileinfo + coverage: none + + - name: Create Laravel app + run: | + cd ../ + composer create-project laravel/laravel:^11.0 test-app-laravel + cd ./test-app-laravel + composer config repositories.0 '{"type": "path", "url": "../telegram-bot-dialogs/"}' + composer require "koot-labs/telegram-bot-dialogs:*@dev" --update-with-all-dependencies + + - name: Setup test Command + run: cp -f ../telegram-bot-dialogs/tests/ci/console.php ./routes/console.php + + - name: Execute PHP tests + run: php artisan telegram:dialog:test 20 diff --git a/tests/ci/console.php b/tests/ci/console.php new file mode 100644 index 0000000..d4eeabd --- /dev/null +++ b/tests/ci/console.php @@ -0,0 +1,39 @@ +info("Listening for Telegram Bot updates for $ttl seconds..."); + + $end = microtime(true) + $ttl; + + while(microtime(true) < $end) { + $updates = Telegram::commandsHandler(); + $updates = is_array($updates) ? $updates : [$updates]; + + /** @var \Telegram\Bot\Objects\Update $update */ + foreach ($updates as $update) { + if ($dialogs->exists($update)) { + $dialogs->proceed($update); + } elseif (str_contains($update->getMessage()?->text, 'hello bot') || $update->getMessage()?->text === '/start') { + $dialog = new HelloExampleDialog($update->getChat()->id); + $dialogs->activate($dialog); + $dialogs->proceed($update); + } else { + $botsManager->sendMessage([ // fallback message + 'chat_id' => $update->getChat()->id, + 'text' => 'There is no active dialog at this moment. You can also start a new dialog by typing "hello bot" in the chat.', + ]); + } + } + + // Sleep for 0.2 seconds to prevent the loop from running too fast + usleep(200_000); + } + + $this->info("Finished listening for Telegram Bot updates."); +})->purpose('Test Telegram Bot and Dialogs');