Skip to content

Commit

Permalink
Add a new GitHub workflow "Run test bot on Laravel app"
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed May 14, 2024
1 parent 28a67a7 commit c45f96a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/run-bot-on-laravel-app.yml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions tests/ci/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Support\Facades\Artisan;
use KootLabs\TelegramBotDialogs\DialogManager;
use KootLabs\TelegramBotDialogs\Dialogs\HelloExampleDialog;
use Telegram\Bot\BotsManager;
use Telegram\Bot\Laravel\Facades\Telegram;

Artisan::command('telegram:dialog:test {ttl=10}', function (DialogManager $dialogs, BotsManager $botsManager, int $ttl): void {
$this->info("Listening for Telegram Bot updates for $ttl seconds...");

Check failure on line 10 in tests/ci/console.php

View workflow job for this annotation

GitHub Actions / psalm

InvalidScope

tests/ci/console.php:10:5: InvalidScope: Invalid reference to $this in a non-class context (see https://psalm.dev/013)

$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');

0 comments on commit c45f96a

Please sign in to comment.