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,
));
}
}
use Cnx\LaravelTelegram\BotApi\Generated\Requests\SendMessageRequest;
use Cnx\LaravelTelegram\Facades\BotApi;
$message = BotApi::sendMessage(new SendMessageRequest(
chatId: 123456789,
text: 'Sent through the facade',
));
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\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;
}
}