PHP code example of vjik / telegram-bot-api

1. Go to this page and download the library: Download vjik/telegram-bot-api 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/ */

    

vjik / telegram-bot-api example snippets


use Vjik\TelegramBot\Api\TelegramBotApi;

// API
$api = new TelegramBotApi(
    // Telegram bot authentication token
    '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw',
);

use Vjik\TelegramBot\Api\Type\InputFile

// Specify a URL for outgoing webhook
$api->setWebhook('https://example.com/webhook');

// Send text message
$api->sendMessage(
    chatId: 22351, 
    text: 'Hello, world!',
);

// Send local photo
$api->sendPhoto(
    chatId: 22351, 
    photo: InputFile::fromLocalFile('/path/to/photo.jpg'),
);

// Result is an array of `Vjik\TelegramBot\Api\Update\Update` objects
$updates = $api->getUpdates();

/**
 * @var \Vjik\TelegramBot\Api\TelegramBotApi $api
 * @var \Vjik\TelegramBot\Api\Type\File $file 
 */
 
// By `File` instance
$url = $api->makeFileUrl($file);

// By file path is taken from the Telegram response
$url = $api->makeFileUrl('photos/file_2');

/**
 * @var \Vjik\TelegramBot\Api\TelegramBotApi $api
 * @var \Vjik\TelegramBot\Api\Type\File $file
 */
 
// Get file content by `File` instance
$fileContent = $api->downloadFile($file);

// Get file content by file path
$fileContent = $api->downloadFile('photos/file_2');

// Download and save file by `File` instance
$fileContent = $api->downloadFileTo($file, '/local/path/to/file.jpg');

// Download and save file by file path
$fileContent = $api->downloadFileTo('photos/file_2', '/local/path/to/file.jpg');