Skip to content

Commit

Permalink
Local Bot API Server support
Browse files Browse the repository at this point in the history
Third-party http-client support
window.Telegram.WebApp.initData validation
  • Loading branch information
BoShurik committed Sep 26, 2023
1 parent 903df6b commit eb1fb3e
Show file tree
Hide file tree
Showing 18 changed files with 637 additions and 214 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ jobs:
name: Remove psalm
run: composer remove vimeo/psalm --dev --no-update

-
name: Remove http client dependencies
run: composer remove psr/http-client psr/http-factory symfony/http-client guzzlehttp/guzzle --dev --no-update

-
name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
Expand Down
2 changes: 2 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
'no_unused_imports' => true,
'single_quote' => true,
'no_extra_blank_lines' => true,
'array_indentation' => true,
'cast_spaces' => true,
'phpdoc_align' => [
'align' => 'left',
],
'binary_operator_spaces' => true,
'single_line_empty_body' => false,
])
;
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
- Add `\TelegramBot\Api\BotApi::revokeChatInviteLink` api method
- Add `\TelegramBot\Api\BotApi::approveChatJoinRequest` api method
- Add `\TelegramBot\Api\BotApi::declineChatJoinRequest` api method
- Add support for third party http clients (`psr/http-client` and `symfony/http-client`)
- Add support for local bot API server
- Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData`

## 2.5.0 - 2023-08-09

Expand Down
44 changes: 11 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ require_once "vendor/autoload.php";

try {
$bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN');
// or initialize with botan.io tracker api key
// $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');


//Handle /ping command
$bot->command('ping', function ($message) use ($bot) {
Expand All @@ -107,45 +104,26 @@ try {
}
```

### Botan SDK (not supported more)

[Botan](http://botan.io) is a telegram bot analytics system based on [Yandex.Appmetrica](http://appmetrica.yandex.com/).
In this document you can find how to setup Yandex.Appmetrica account, as well as examples of Botan SDK usage.

### Creating an account
* Register at http://appmetrica.yandex.com/
* After registration you will be prompted to create Application. Please use @YourBotName as a name.
* Save an API key from settings page, you will use it as a token for Botan API calls.
* Download lib for your language, and use it as described below. Don`t forget to insert your token!

Since we are only getting started, you may discover that some existing reports in AppMetriсa aren't properly working for Telegram bots, like Geography, Gender, Age, Library, Devices, Traffic sources and Network sections. We will polish that later.

## SDK usage

#### Standalone
#### Local Bot API Server

```php
$tracker = new \TelegramBot\Api\Botan('YOUR_BOTAN_TRACKER_API_KEY');

$tracker->track($message, $eventName);
```
For using custom [local bot API server](https://core.telegram.org/bots/api#using-a-local-bot-api-server)

#### API Wrapper
```php
$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');

$bot->track($message, $eventName);
use TelegramBot\Api\Client;
$token = 'YOUR_BOT_API_TOKEN';
$bot = new Client($token, null, null, 'http://localhost:8081');
```

You can use method 'getUpdates()'and all incoming messages will be automatically tracked as `Message`-event.
#### Third-party Http Client

#### Client
```php
$bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');
use Symfony\Component\HttpClient\HttpClient;
use TelegramBot\Api\BotApi;
use TelegramBot\Api\Http\SymfonyHttpClient;
$token = 'YOUR_BOT_API_TOKEN';
$bot = new Client($token, null, new SymfonyHttpClient(HttpClient::create()););
```

_All registered commands are automatically tracked as command name_

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@
},
"require-dev": {
"symfony/phpunit-bridge" : "*",
"friendsofphp/php-cs-fixer": "^3.16",
"vimeo/psalm": "^5.9"
"friendsofphp/php-cs-fixer": "~3.28.0",
"vimeo/psalm": "^5.9",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"symfony/http-client": "^4.3 | ^5.0 | ^6.0",
"guzzlehttp/guzzle": "^7.0"
},
"suggest": {
"psr/http-client": "To use psr/http-client",
"psr/http-factory": "To use psr/http-client",
"guzzlehttp/guzzle": "To use psr/http-client",
"symfony/http-client": "To use symfony/http-client"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 4 additions & 5 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
<MissingConstructor errorLevel="suppress" />
<PropertyNotSetInConstructor errorLevel="suppress" />
<RedundantCastGivenDocblockType errorLevel="suppress" />
<DeprecatedClass>
<errorLevel type="suppress">
<referencedClass name="TelegramBot\Api\Botan" />
</errorLevel>
</DeprecatedClass>
<DeprecatedConstant errorLevel="suppress" />
<DeprecatedMethod errorLevel="suppress" />
<DeprecatedClass errorLevel="suppress" />
<DeprecatedProperty errorLevel="suppress" />
</issueHandlers>
</psalm>
Loading

0 comments on commit eb1fb3e

Please sign in to comment.