Download the PHP package klev-o/telegram-bot-api without Composer
On this page you can find all versions of the php package klev-o/telegram-bot-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package telegram-bot-api
klev-o/telegram-bot-api
Simple and convenient implementation Telegram bot API with php version ^7.4 support. You'll like it)
Based on the Official Telegram api
📖Intro
This bot is full support Official Telegram api. Fully object-oriented and simple code. All available types and methods are described using classes with documentation of all fields. You don't even need to refer to the official documentation - all descriptions are present in the bot! But still, for each class, the url to the documentation is indicated, where you can study the nuances, etc.
You just have to relax and create super bots!
Attention! At the moment, the bot only supports receiving updates through Webhook. Webhook is more efficient than Long-Polling, reduces server load and guarantees almost instant data refresh for your application. But it is worth considering some of the nuances, in more detail here
🛠 Installation
Run this command in your command line:
🔌Usage
Setting up a webhook
First, you need to install Webhook, to which the telegram will send updates. This can be done using the following code:
To prevent the webhook from being installed on every request, we add a simple check. Now open the file in your browser and you should see 'webhook was set'. If any error has occurred, then it can be caught in the corresponding block
Getting Webhook Updates
To receive updates, you must use the method getWebhookUpdates():
The $update variable will be an object Update
In general, by reading the official documentation, you can see the types for the fields of objects, or the return values of methods - all this is completely consistent with the code.
For example, $update->message
is of type Message, which corresponds to Klev\TelegramBotApi\Types\Message
.
Just look at the documentation and call the methods you want!
A real example
Let's say we want the bot to reply "Hello, your username" to every message to the bot.
Let's write the following code:
As you can see, everything is very simple and straightforward. Remember, methods have many parameters that you can further customize to your preference. Description of each parameter is present in the code in phpdoc, or on the website of the official API documentation
Sending files
Sending files is very simple: you need to specify the path to the file or url in the field (if the method supports accepting files by url, see the description). Next, it will automatically check if the file exists locally and add all the necessary headers.
If the file is unreadable, you will get the error "File -filename- is not readable."
In the example below, the bot sends a local document if the user writes "doc" to the bot:
Also, nothing prevents passing a link to the file instead of a local file - the code will be absolutely the same, only this part will change:
📟Advanced
As you can see, the $bot->getWebhookUpdates()
method returns the result as an Update object. In the simplest case, we can check which field is filled in this object and, on this basis, implement further logic. But this may not be very convenient if we have any medium or large project.
Events come to the rescue (Klev\TelegramBotApi\Events\*
):
List of events |
---|
CallbackQueryEvent |
ChannelPostEvent |
ChatJoinRequestEvent |
ChatMemberEvent |
ChosenInlineResultEvent |
EditedChannelPostEvent |
EditedMessageEvent |
InlineQueryEvent |
MessageEvent |
MyChatMemberEvent |
PollAnswerEvent |
PollEvent |
PreCheckoutQueryEvent |
ShippingQueryEvent |
You can register your own handler for any of these events and be sure which update you are responding to.
By default, events are disabled. To enable them, you need to use the method $bot->setEnableEvents(true);
Consider an example:
Each Event object will have 2 required fields: update_id
and payload
. What type of payload will be in the event can be viewed in the class with the desired event
Also, as an event handler, you can use anything that corresponds to the callable type. Consider an example:
🎁Dontations
Support the project if you like it. Funds will go towards food.
Network | Currency | Wallet |
---|---|---|
Bitcoin | BTC |
1M1qhSE6sN34a4d7ZtCh6y17Vf3LtdoW62 or 14cvXywCMucKMhFYDCbmQ1ZHhayDgbD65R |
The Open Network | TON |
EQAYZK8rWrS9Fhojdc486BpplDmTSLHum440f-L2--SA2Oid or ton://transfer/UQBVsumSIvsq4PfeFMhxSV9m_zPB31cHJX4X2lAVh9BUJXm3 |
Binance Smart Chain – BEP20 | BNB, BUSD, USDT |
0x674B09Ab418bb41C075847bde004bb7F492c2121 |
🧨Troubleshooting
Please, if you find any errors or not exactly - report this problem page
And finally...
Happy botting 🤖
All versions of telegram-bot-api with dependencies
ext-json Version *
ext-curl Version *
guzzlehttp/guzzle Version ^7.2