Skip to content

A PHP library for interacting with the Telegram BOT API, designed for use with Bitrix CMS or GuzzleHTTP client.

License

Notifications You must be signed in to change notification settings

dev-bx/telegram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram BOT API Library for PHP

This PHP library provides a convenient and easy-to-use interface for interacting with the Telegram BOT API. It is designed to be used with the Bitrix CMS or the GuzzleHTTP client.

Features

  • Easy Integration: Seamlessly integrates with Bitrix CMS and GuzzleHTTP client.
  • Full API Coverage: Supports all Telegram BOT API 8.2 methods.
  • Customizable: Easily extendable and customizable to fit your specific needs.

Installation

You can install the package via Composer:

composer require devbx/telegram

Usage

Bitrix CMS

use DevBX\Telegram;

$client = new Telegram\BitrixClient([
    'token' => '1234567890:zzz',
    'client_options' => [ // Bitrix client options
        'disableSslVerification' => false
    ]
]);

$result = $client->sendPhoto([
    'chat_id' => 1234567890,
    'photo' => [
        'filename' => 'media.jpg',
        'resource' => fopen('media.jpg', 'r+'),
        'contentType' => 'image/jpeg',
    ],
]);

echo $result->messageId."\n";

foreach ($result->photo as $photo) {
    echo "File ID - {$photo->fileId}\nWidth - {$photo->width}\nHeight - {$photo->height}\nFile size - {$photo->fileSize}\n\n";
}

Guzzle Http

use DevBX\Telegram;

$client = new \DevBX\Telegram\GuzzleClient([
    'token' => '1234567890:zzz',
    'client_options' => [ // Guzzle client options
        'verify' => false
    ]
]);

$result = $client->sendPhoto([
    'chat_id' => 1234567890,
    'photo' => [
        'filename' => 'media.jpg',
        'resource' => fopen('media.jpg', 'r+'),
        'contentType' => 'image/jpeg',
    ],
]);

echo $result->messageId."\n";

foreach ($result->photo as $photo) {
    echo "File ID - {$photo->fileId}\nWidth - {$photo->width}\nHeight - {$photo->height}\nFile size - {$photo->fileSize}\n\n";
}

Using a Local Bot API Server

Bitrix

use DevBX\Telegram;

$client = new Telegram\BitrixClient([
    'token' => '1234567890:zzz',
    'api_url' => 'http://localhost:8081/bot'
]);

$client->sendMessage([
    'chat_id' => '1234567890',
    'text' => 'message from Local Bot API Server'
]);

Guzzle Http

use DevBX\Telegram;

$client = new Telegram\GuzzleClient([
    'token' => '1234567890:zzz',
    'api_url' => 'http://localhost:8081/bot'
]);

$client->sendMessage([
    'chat_id' => '1234567890',
    'text' => 'message from Local Bot API Server'
]);

Getting updates from WebHook

use DevBX\Telegram;

$result = Telegram\Api::getWebhookUpdate();

foreach ($result as $updateType => $update) {
    if (!is_object($update))
        continue;

    switch ($updateType)
    {
        case 'message':
            /* @var Telegram\Types\Message $update */
            
            echo "Message from: {$update->from->id}\nText: {$update->text}\n";
            
            break;
    }
}

About

A PHP library for interacting with the Telegram BOT API, designed for use with Bitrix CMS or GuzzleHTTP client.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages