tg-bot-api/bot-api-base

Clear and simple Telegram bot API


README

Telegram bot api Latest Version on Packagist Software License Coverage Status build Quality Score PHP Version >= 7.2 Symfony Recipe

Quality Gate Status Bugs Duplicated Lines (%) Security Rating

Supported Telegram Bot API 5.0 (November 4, 2020)

Installation

Via Composer

composer require tg-bot-api/bot-api-base --prefer-dist

Usage

We support all psr17 and psr18 implementations, but we will use guzzle6 for example

composer require php-http/guzzle6-adapter http-interop/http-factory-guzzle --prefer-dist
$botKey = '<bot key>';

$requestFactory = new Http\Factory\Guzzle\RequestFactory();
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
$client = new Http\Adapter\Guzzle6\Client();

$apiClient = new \TgBotApi\BotApiBase\ApiClient($requestFactory, $streamFactory, $client);
$bot = new \TgBotApi\BotApiBase\BotApi($botKey, $apiClient, new \TgBotApi\BotApiBase\BotApiNormalizer());

$userId = '<user id>';

$bot->send(\TgBotApi\BotApiBase\Method\SendMessageMethod::create($userId, 'Hi'));

You can configure it to work in symfony, for example, in this way.

If you want to use your own api server - you can set url as 4th param in bot api

$bot = new \TgBotApi\BotApiBase\BotApi('<bot key>', $apiClient, new \TgBotApi\BotApiBase\BotApiNormalizer(), '<your-domain>');

Allowed methods:

Implemented all methods and types referenced by official Api

You can use BotApiComplete instance as helper to call all methods from official Api like this:

$botKey = '<bot key>';

$requestFactory = new Http\Factory\Guzzle\RequestFactory()
$streamFactory = new Http\Factory\Guzzle\StreamFactory();
$client = new Http\Adapter\Guzzle6\Client();

$apiClient = new \TgBotApi\BotApiBase\ApiClient($requestFactory, $streamFactory, $client);
$bot = new \TgBotApi\BotApiBase\BotApiComplete($botKey, $apiClient, new \TgBotApi\BotApiBase\BotApi\BotApiNormalizer());

$userId = '<user id>';

$bot->sendMessage(\TgBotApi\BotApiBase\Method\SendMessageMethod::create($userId, 'Hi'));

Learn api

Fetching webhooks

Method fetch() of WebhookFetcher handling Psr\Http\Message\RequestInterface or string and always returns instance of UpdateType or throwing BadRequestException.

$fetcher = new \TgBotApi\BotApiBase\WebhookFetcher(new \TgBotApi\BotApiBase\BotApiNormalizer());
$update = $fetcher->fetch($request);

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email wformps@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.