PHP code example of vzgcoders / telegramphp

1. Go to this page and download the library: Download vzgcoders/telegramphp 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/ */

    

vzgcoders / telegramphp example snippets


$telegram = new Telegram([
    'token' => $token,
    'socket_options' => ['tls' => ['cafile' => 'C:/php/cacert.pem']],
]);

use Telegram\Events\Event;
use Telegram\Parts\Message;
use Telegram\Telegram;

$telegram = new Telegram(['token' => getenv('TELEGRAM_TOKEN')]);

$telegram->on(Event::MESSAGE, function (Message $message) use ($telegram) {
    if ($message->text === 'ping') {
        $message->reply('pong');
    }
});

$telegram->run();

$telegram->sendMessage($chatId, 'Deploying now', parse_mode: 'HTML', disable_notification: true);

$telegram->getChat('@durov')->then(function (ChatFullInfo $chat) {
    echo $chat->title, ' — ', $chat->description, PHP_EOL;
});

$telegram->request('someFutureMethod', ['chat_id' => $chatId], ['Message']);

use Telegram\Events\Event;
use Telegram\Parts\CallbackQuery;

$telegram->on(Event::CALLBACK_QUERY, function (CallbackQuery $query) {
    $query->answer('Working on it');
    $query->editMessage('Deployed.');
});

$telegram = new Telegram([
    'token' => $token,
    'allowed_updates' => [Event::MESSAGE, Event::CALLBACK_QUERY],
    'drop_pending_updates' => true,
]);

$telegram = new Telegram([
    'token' => $token,
    'webhook' => ['listen' => '0.0.0.0:8080', 'path' => '/hook', 'secret_token' => $secret],
]);

$telegram->setWebhook('https://bot.example.com/hook', secret_token: $secret);
$telegram->run();

$message->chat->title;
$message['from']['username'];
$message->date->diffForHumans();
$message->entities->first()->type;

$message->reply('on it');
$message->react('👍');
$message->pin();
$message->forward($otherChatId);

$chat->sendMessage('hello');
$chat->ban($userId, ['until_date' => time() + 3600]);

$user->mention();          // <a href="tg://user?id=…">Ada Lovelace</a>
$file->save('/tmp/photo.jpg');

use Telegram\Builders\InlineKeyboard;
use Telegram\Builders\InputFile;
use Telegram\Builders\ReplyKeyboard;

$telegram->sendMessage($chatId, 'Deploy?', reply_markup: InlineKeyboard::new()
    ->callback('Ship it', 'deploy:yes')
    ->callback('Hold', 'deploy:no')
    ->row()
    ->url('What changed', 'https://example.com/diff'));

$telegram->sendMessage($chatId, 'Where are you?', reply_markup: ReplyKeyboard::new()
    ->requestLocation('Send my location')
    ->resize()
    ->oneTime());

$telegram->sendPhoto($chatId, InputFile::fromPath('cat.jpg'), caption: 'mine');

$telegram->sendMediaGroup($chatId, [
    ['type' => 'photo', 'media' => InputFile::fromPath('one.jpg')],
    ['type' => 'photo', 'media' => InputFile::fromPath('two.jpg')],
]);

$telegram->downloadFile($photo->file_id)->then(fn (string $bytes) => file_put_contents('photo.jpg', $bytes));

$telegram = new Telegram([
    'token' => getenv('TELEGRAM_TOKEN'),
    'base_url' => 'http://localhost:8081',

    // The same machine: the server's paths are this process's.
    'local_files' => ['C:\telegram-bot-api'],

    // Or in Docker: the server's path => the volume it is mounted from.
    // 'local_files' => ['/var/lib/telegram-bot-api' => 'D:\telegram-bot-api'],
]);

$telegram->localFilePath($document->file_id)->then(fn (?string $path) => rename($path, 'D:\archive\big.mkv'));

$telegram->sendDocument($chatId, $telegram->getLocalFiles()->toUri('D:\telegram-bot-api\big.mkv'));

use Telegram\CommandClient\TelegramCommandClient;
use Telegram\Parts\Message;

$bot = new TelegramCommandClient([
    'token' => getenv('TELEGRAM_TOKEN'),
    'description' => 'A helpful bot',
]);

$bot->registerCommand('ping', fn () => 'pong', ['description' => 'Checks the bot is alive']);

$bot->registerCommand('echo', fn (Message $message, array $args) => implode(' ', $args), [
    'description' => 'Repeats what you say',
    'aliases' => ['say'],
]);

$bot->run();

$telegram->chats->get($chatId);
$telegram->chats->fetch($chatId);          // getChat, cached on the way back
$telegram->users->fetchMember($chatId, $userId);

use Telegram\Http\Exceptions\ForbiddenException;
use Telegram\Http\Exceptions\HttpException;

$telegram->sendMessage($chatId, 'hello')->then(null, function (HttpException $e) {
    if ($e instanceof ForbiddenException) {
        // Blocked by the user, or kicked from the chat.
    }

    $e->getErrorCode();        // 403
    $e->getRetryAfter();       // seconds, on a 429
    $e->getMigrateToChatId();  // the supergroup a migrated group became
});
bash
composer create-project discord-php/phpdoc-tool:^1.0 phpdoc-tool --no-interaction \
  --repository='{"type":"vcs","url":"https://github.com/discord-php/phpdoc-tool"}'