PHP code example of vbespalov / laravel-telegram

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

    

vbespalov / laravel-telegram example snippets


use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendMessageRequest;
use Cnx\LaravelTelegram\BotApi\Generated\Types\Message;
use Cnx\LaravelTelegram\BotApiClient;

final readonly class SendTelegramNotification
{
    public function __construct(private BotApiClient $telegram) {}

    public function __invoke(int $chatId, string $text): Message
    {
        return $this->telegram->sendMessage(new SendMessageRequest(
            chatId: $chatId,
            text: $text,
            parseMode: 'HTML',
            disableNotification: true,
        ));
    }
}

$message = $notifier(123456789, '<b>Payment received</b>');

echo $message->messageId;
echo $message->chat->id;
echo $message->text;

$bot = $telegram->getMe();
$webhook = $telegram->getWebhookInfo();

use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendMessageRequest;
use Cnx\LaravelTelegram\Facades\BotApi;

$message = BotApi::sendMessage(new SendMessageRequest(
    chatId: 123456789,
    text: 'Sent through the facade',
));

'default_bot_config' => 'default',

'bot_configs' => [
    'default' => [
        'bot_token' => env('TELEGRAM_BOT_TOKEN'),
    ],
    'cashier' => [
        'bot_token' => env('TELEGRAM_CASHIER_BOT_TOKEN'),
    ],
],

$message = $telegram
    ->bot('cashier')
    ->sendMessage(new SendMessageRequest(
        chatId: 123456789,
        text: 'Cashier notification',
    ));

use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendMessageRequest;
use Cnx\LaravelTelegram\BotApi\Generated\Types\InlineKeyboardButton;
use Cnx\LaravelTelegram\BotApi\Generated\Types\InlineKeyboardMarkup;

$message = $telegram->sendMessage(new SendMessageRequest(
    chatId: 123456789,
    text: 'Open the transaction',
    replyMarkup: new InlineKeyboardMarkup([
        [
            new InlineKeyboardButton(
                text: 'View transaction',
                url: 'https://example.com/transactions/42',
            ),
            new InlineKeyboardButton(
                text: 'Refresh',
                callbackData: 'refresh:42',
            ),
        ],
    ]),
));

use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendMessageRequest;
use Cnx\LaravelTelegram\BotApi\Generated\Types\ReplyParameters;

$message = $telegram->sendMessage(new SendMessageRequest(
    chatId: -1001234567890,
    messageThreadId: 25,
    text: 'Reply inside a forum topic',
    replyParameters: new ReplyParameters(messageId: 100),
));

use Cnx\LaravelTelegram\BotApi\Generated\Requests\DeleteMessageRequest;
use Cnx\LaravelTelegram\BotApi\Generated\Requests\EditMessageTextRequest;

$edited = $telegram->editMessageText(new EditMessageTextRequest(
    chatId: 123456789,
    messageId: 100,
    text: 'Updated text',
));

$deleted = $telegram->deleteMessage(new DeleteMessageRequest(
    chatId: 123456789,
    messageId: 100,
));

use Cnx\LaravelTelegram\BotApi\Generated\Types\Message;
use Cnx\LaravelTelegram\BotApi\Generated\Types\Update;
use Illuminate\Http\Request;

public function webhook(Request $request): void
{
    $update = Update::from($request->all());

    if ($update->message instanceof Message) {
        $chatId = $update->message->chat->id;
        $text = $update->message->text;
    }
}

$payload = $update->toArray();
$json = json_encode($update, JSON_THROW_ON_ERROR);

use Cnx\LaravelTelegram\BotApi\Generated\Requests\SetWebhookRequest;

$telegram->setWebhook(new SetWebhookRequest(
    url: 'https://example.com/api/telegram/webhook',
    secretToken: config('services.telegram.webhook_secret'),
    allowedUpdates: ['message', 'callback_query', 'my_chat_member'],
));

use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendPhotoRequest;
use Cnx\LaravelTelegram\BotApi\InputFile;

$message = $telegram->sendPhoto(new SendPhotoRequest(
    chatId: 123456789,
    photo: InputFile::fromPath(storage_path('app/photo.jpg')),
    caption: 'Daily report',
));

$file = InputFile::fromContents(
    contents: $pdfContents,
    filename: 'report.pdf',
    mimeType: 'application/pdf',
);

use Cnx\LaravelTelegram\Exceptions\TelegramException;

try {
    $telegram->sendMessage($request);
} catch (TelegramException $exception) {
    report($exception);

    $telegramCode = $exception->telegramErrorCode();
    $retryAfter = $exception->responseParameters()?->retryAfter;
}

$result = $telegram->call(
    method: 'futureTelegramMethod',
    parameters: ['chat_id' => 123456789],
    returnType: 'Mixed',
);

// Before
Telegram::bot('cashier')->sendMessage(
    (new MessageBuilder($chatId, $text))->setParseMode(ParseMode::HTML),
);

// After
$telegram->bot('cashier')->sendMessage(new SendMessageRequest(
    chatId: $chatId,
    text: $text,
    parseMode: 'HTML',
));

// Before
Telegram::setWebhook(url: $url, secret_token: $secret);

// After
$telegram->setWebhook(new SetWebhookRequest(
    url: $url,
    secretToken: $secret,
));

// Before
use Cnx\LaravelTelegram\DTO\Update;

// After
use Cnx\LaravelTelegram\BotApi\Generated\Types\Update;

// Before
if (! $message->text instanceof Optional) {
    // ...
}

// After
if ($message->text !== null) {
    // ...
}
shell
php artisan vendor:publish \
  --provider="Cnx\LaravelTelegram\TelegramServiceProvider" \
  --tag="telegram-config"