Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateodioev committed Apr 19, 2023
1 parent 98b7163 commit 94c7432
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
89 changes: 85 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Create new instance and add your command (*for now only text commands is availab
```php
$bot = new Bot($botToken);

$bot->on('message', YourCommandInstance);
$bot->onEvent(YourCommandInstance);

$bot->byWebhook();
// or
Expand All @@ -44,7 +44,7 @@ class MyCommand extends MessageCommand
}
}

$bot->on('message', MyCommand::get());
$bot->onEvent(MyCommand::get());
```

This command works with `/start any text here` or `/start`. If you need another prefixes (/, !) you can add with method `addPrefix` or with property `prefix`
Expand All @@ -61,9 +61,13 @@ class MyCommand extends MessageCommand

...
}

// or
$myCommand = MyCommand::get();
$myCommand->setPrefixes(['/', '!']); // Set prefix, no need to set `$prefix` property
```

### Using middlewares
### Using middlewares

You can add middlewares to your command. Middlewares are closures that will be executed before the command. All middlewares results will be passed to the command as an array.

Expand Down Expand Up @@ -94,4 +98,81 @@ function authUser(Context $ctx, Api $bot) {
return $user;
}
```
> You can use `StopCommand` exception to stop command execution
> You can use `StopCommand` exception to stop command execution
### Loging

#### Basic usage

```php
use Mateodioev\TgHandler\Log\{Logger, TerminalStream};

$log = new Logger(new TerminalStream); // Print logs in terminal
$bot->setLogger($log); // Save logger
```

#### Collections of streams

```php
use Mateodioev\TgHandler\Log\{BulkStream, Logger, TerminalStream, PhpNativeStream};

BulkStream::add(new TerminalStream); // print logs in terminal
BulkStream::add((new PhpNativeStream)->activate(__DIR__)); // save logs in .log file and catch php warnings

$bot->setLogger(new Logger(new BulkStream));
```

#### Set log level

```php
use Mateodioev\TgHandler\Log\{Logger, TerminalStream};

$log = new Logger(new TerminalStream);

// disable all logs
$log->setLevel(Logger:ALL, false);
// Enable only critical, error and emergency messages
$log->setLevel(Logger::CRITICAL | Logger::ERROR | Logger::EMERGENCY);

$bot->setLogger($log);
```

#### Use

The logger can be used from the bot or event instances (MessageCommand, CallbackCommand, etc)

_Bot:_
```php
$bot->getLogger()->debug('This is a debug message');
// output: [Y-m-d H:i:s] [DEBUG] This is a debug message
$bot->getLogger()->info('This is a debug message with {paramName}', [
'paramName' => 'context params'
]);
// output: [Y-m-d H:i:s] [INFO] This is a debug message with context params
```

_Event instances:_

```php
use Mateodioev\TgHandler\Commands\MessageCommand;
use Mateodioev\Bots\Telegram\Api;
use Mateodioev\TgHandler\Context;

class MyCommand extends MessageCommand
{
protected string $name = 'start';
protected string $description = 'Start the bot';

public function handle(Api $bot, Context $context)
{
$this->logger()->debug('Loging inside event');
$this->logger()->info('User {name} use the command {commandName}', [
'name' => $context->getUserName() ?? 'null',
'commandName' => $this->name
]);
}
}

// register the command
$bot->onEvent(MyCommand::get());
```
3 changes: 1 addition & 2 deletions examples/bot.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use Mateodioev\TgHandler\Log\{BulkStream, Logger, TerminalStream};
use Mateodioev\TgHandler\Log\{BulkStream, Logger, TerminalStream, PhpNativeStream};
use Mateodioev\TgHandler\{Bot, Context};
use Mateodioev\TgHandler\Log\PhpNativeStream;
use Mateodioev\Utils\Exceptions\RequestException;

require __DIR__ . '/../vendor/autoload.php';
Expand Down

0 comments on commit 94c7432

Please sign in to comment.