Code Monkey home page Code Monkey logo

bot-api-base's Introduction

Telegram Bot Api Base

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

Quality Gate Status Bugs Duplicated Lines (%) Security Rating

Supported Telegram Bot API v4.9 (June 4, 2020)

Installation

Via Composer

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

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
$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.

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 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
getGameHighScores GetGameHighScoresMethod GameHighScoreType[]
getStickerSet GetStickerSetMethod StickerSetType
getFile GetFileMethod FileType
sendMediaGroup SendMediaGroupMethod MessageType[]
getAbsoluteFilePath FileType string
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'));

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 [email protected] instead of using the issue tracker.

Credits

License

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

bot-api-base's People

Contributors

big-shark avatar cybernet avatar greenplugin avatar jan-di avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.