PHP code example of devbx / telegram

1. Go to this page and download the library: Download devbx/telegram library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devbx / telegram example snippets


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";
}



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";
}



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'
]);

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'
]);


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;
    }
}