idct / php-telegram-sender
Simple PHP library which simplifies the task of sending bot messages using Telegram IM
Requires
- php: ^7.1
- ext-curl: *
- myclabs/php-enum: ^1.7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- php-coveralls/php-coveralls: ^2.1
- php-mock/php-mock-phpunit: ^2.5
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-10-24 11:49:01 UTC
README
Simple PHP library which simplifies the task of sending bot messages using Telegram IM.
Installation
The best way to install the library in your project is by using Composer:
composer require idct/php-telegram-sender
of course you can still manually include all the required files in your project using using
statements yet Composer and autoloading is more than suggested.
Usage
Actions are handled via static methods as it is meant to be as simple as possible and at the moment there is no real benfit from instances of the main class.
To use it first add somewhere the use
statement:
use IDCT\TelegramSender\TelegramSender;
Then you have few methods available:
- to check if a bot is valid by its bot id:
TelegramSender::checkIfBotIsValid(123123123);
where 123123123 is bot's id.
Returns true
or false
depending on the fact if bot id is valid.
- you can retrieve basic bot's information using:
TelegramSender::retrieveBotInfo(123123123);
where 123123123 is bot's id.
Returns an instance of BotInfo
.
- the main method of the library is
sendMessage
:
TelegramSender::sendMessage(Bot $bot, Channel $channel, string $message, ParseMode $parseMode = null, bool $disableWebPagePreview = false, bool $disableAudioNotification = false, int $threadId = null);
Messages support HTML and Markdown depending on the $parseMode
arguemnt's value.
$channel
must be an instance of PublicChannel
or PrivateChannel
.
Example:
TelegramSender::sendMessage( new Bot(123123123, 'AAFKeyoJC6wHmHW85TfUktEMc2x5iz9melE'), new PrivateChannel(90808012), 'my message' );
Values for the Bot
and Channel
constructors can be obtained from the Telegram app: check descriptions below.
Method returns an array of elements listed here: https://core.telegram.org/bots/api#message
TODO: Message entity in the library.
How to create a new bot?
- Contact
BotFather
user on Telegram
- Type
/newbot
to theBotFather
and answer all the asked questions.
- The part which looks like 635092640:AAGfG72lFg0L_Uf8Cfhb5wBi4UFTm7R2lDY are your bot's id and key.
The part before
:
is the id and the part after is the key. So for the example above it would be:
id: 635092640
key: AAGfG72lFg0L_Uf8Cfhb5wBi4UFTm7R2lDY
so you would create an instance of the bot as follows:
$bot = new Bot(635092640, 'AAGfG72lFg0L_Uf8Cfhb5wBi4UFTm7R2lDY');
Where to get the channel's id from?
-
Create a channel using your Telegram client (
New channel
option in the hamburger menu on the left usually). -
Add your bot as the admin of the channel.
-
Open
https://web.telegram.org/
and sign in using your account. -
Click on the desired channel. The url in your browser will change.
-
If the url changes to:
https://web.telegram.org/#/im?p=c1282543751_17534450962567305630
then1282543751
is your channel's id. So basically the part betweenc
and_
after?
(in the query string). -
Use it to create
PrivateChannel
orPublicChannel
instance depending on the visibility of your channel.
...I know it is quite complicated, but at the moment Telegram's API does not expose any method to get bot's channels.
Contribution
In order to contribute open a Pull Request or an Issue. At the moment there are two major points where contribution is more than welcome: support for additional methods of Telegram's API and better unit tests. Handling of the responses: like creating an entity class for the response would be very useful too.