tg-bot-api / bot-api-base
Clear and simple Telegram bot API
Installs: 62 977
Dependents: 2
Suggesters: 0
Security: 0
Stars: 224
Watchers: 16
Forks: 71
Open Issues: 3
Requires
- php: >7.3
- ext-fileinfo: *
- ext-json: *
- phpdocumentor/reflection-docblock: ^4.3|^5.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- symfony/property-access: ^3.4|^4.3|^5.0|^6.0
- symfony/property-info: ^3.4|^4.3|^5.0|^6.0
- symfony/serializer: ^3.4|^4.3|^5.0|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- phpro/grumphp: ^1.1.0
- phpstan/phpstan: ^0.10.6|^0.12.20
- phpunit/phpunit: ^8.5
- povils/phpmnd: ^2.0
- sebastian/phpcpd: ^5.0
- squizlabs/php_codesniffer: ^3.4
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- dev-master / 1.0.x-dev
- 1.0.2
- 1.0.1
- 1.0.0
- 0.4.0-beta
- 0.3.0-beta
- 0.2.0-beta
- 0.1.0-beta
- dev-feature/+48-update-readme-and-rename-workflow
- dev-feature/+48-replace-travis-to-github-actions
- dev-Big-Shark-patch-1
- dev-feature/+30-add-posibility-to-reply-webhook
This package is auto-updated.
Last update: 2025-03-18 05:17:49 UTC
README
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:
Method | Allowed type | response |
---|---|---|
add |
AddStickerToSetMethod | bool |
answer |
AnswerCallbackQueryMethod, AnswerInlineQueryMethod, AnswerPreCheckoutQueryMethod, AnswerShippingQueryMethod | bool |
create |
CreateNewStickerSetMethod | bool |
delete |
DeleteChatPhotoMethod, DeleteChatStickerSetMethod, DeleteMessageMethod, DeleteStickerFromSetMethod, DeleteWebhookMethod | bool |
edit |
EditMessageCaptionMethod, EditMessageLiveLocationMethod, EditMessageMediaMethod, EditMessageReplyMarkupMethod, EditMessageTextMethod | bool |
forward |
ForwardMessageMethod | MessageType |
kick |
KickChatMemberMethod | bool |
leave |
LeaveChatMethod | bool |
pin |
PinChatMessageMethod | bool |
promote |
PromoteChatMemberMethod | bool |
restrict |
RestrictChatMemberMethod | bool |
send |
SendPhotoMethod, SendAudioMethod, SendDocumentMethod, SendVideoMethod, SendAnimationMethod, SendVoiceMethod, SendVideoNoteMethod, SendGameMethod, SendInvoiceMethod, SendLocationMethod, SendVenueMethod, SendContactMethod, SendStickerMethod, SendMessageMethod, SendPollMethod, SendDiceMethod | MessageType |
set |
SetChatDescriptionMethod, SetChatPhotoMethod, SetChatStickerSetMethod, SetChatTitleMethod, SetGameScoreMethod, SetStickerPositionInSetMethod, SetWebhookMethod, SetPassportDataErrorsMethod, SetChatPermissionsMethod, SetChatAdministratorCustomTitleMethod, SetMyCommandMethod, SetStickerSetThumbMethod | bool |
stop |
StopMessageLiveLocationMethod | bool |
stopPoll |
StopPollMethod | Poll |
unban |
UnbanChatMemberMethod | bool |
unpin |
UnpinChatMessageMethod, UnpinAllChatMessagesMethod | bool |
upload |
UploadStickerFileMethod | FileType |
exportChatInviteLink |
ExportChatInviteLinkMethod | string |
sendChatAction |
SendChatActionMethod | bool |
getUpdates |
GetUpdatesMethod | UpdateType[] |
getMe |
GetMeMethod | UserType |
getMyCommands |
GetMyCommandsMethod | BotCommandType |
getUserProfilePhotos |
GetUserProfilePhotosMethod | UserProfilePhotosType |
getWebhookInfo |
GetWebhookInfoMethod | WebhookInfoType |
getChatMembersCount |
GetChatMembersCountMethod | int |
getChat |
GetChatMethod | ChatType |
getChatAdministrators |
GetChatAdministratorsMethod | ChatMemberType[] |
getChatMember |
GetChatMemberMethod | ChatMemberType |
getChatMenuButton |
GetChatMenuButtonMethod | MenuButtonType |
getGameHighScores |
GetGameHighScoresMethod | GameHighScoreType[] |
getStickerSet |
GetStickerSetMethod | StickerSetType |
getFile |
GetFileMethod | FileType |
sendMediaGroup |
SendMediaGroupMethod | MessageType[] |
getAbsoluteFilePath |
FileType | string |
logOut |
LogOutMethod | bool |
close |
CloseMethod | bool |
copyMessage |
CopyMessageMethod | MessageIdType |
call($method, [string $type]) |
any method class, [optional expected type] | array or excepted type object |
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'));
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.