PHP code example of alexitdev91 / laravel-telegram-bot
1. Go to this page and download the library: Download alexitdev91/laravel-telegram-bot 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/ */
alexitdev91 / laravel-telegram-bot example snippets
use AlexItDev91\LaravelTelegramBot\TelegramBot;
final class SendTelegramAlert
{
public function __construct(
private TelegramBot $telegram,
) {
}
public function __invoke(): void
{
$this->telegram->channel('inbox')->sendMessage([
'text' => 'New inbound email',
]);
}
}
use AlexItDev91\LaravelTelegramBot\TelegramBot;
final readonly class TenantAlert
{
public function __construct(private TelegramBot $telegram)
{
}
public function send(Tenant $tenant): mixed
{
return $this->telegram->to(
chatId: (string) $tenant->telegram_channel_id,
token: $tenant->telegram_bot_token,
)->sendMessage([
'text' => 'Tenant alert',
]);
}
}
use AlexItDev91\LaravelTelegramBot\MiniApps\TelegramMiniAppInitDataValidator;
use Illuminate\Http\Request;
final readonly class TelegramMiniAppSessionController
{
public function __construct(
private TelegramMiniAppInitDataValidator $telegramMiniApps,
) {
}
public function __invoke(Request $request): array
{
$data = $this->telegramMiniApps->validate(
initData: (string) $request->string('initData'),
botToken: (string) config('telegram-bot.token'),
maxAgeSeconds: 300,
);
return [
'telegram_user_id' => (string) $data->user()?->id(),
'start_param' => $data->startParam(),
];
}
}
use AlexItDev91\LaravelTelegramBot\DeepLinks\TelegramDeepLink;
use AlexItDev91\LaravelTelegramBot\DeepLinks\TelegramStartPayloadSigner;
final readonly class ReferralLinkFactory
{
public function __construct(
private TelegramStartPayloadSigner $payloads,
) {
}
public function link(string $referralCode): string
{
$payload = $this->payloads->sign(
payload: $referralCode,
secret: (string) config('app.key'),
ttlSeconds: 3600,
);
return TelegramDeepLink::start('CompanyBot', $payload)->url();
}
}
use AlexItDev91\LaravelTelegramBot\Enums\TelegramParseMode;
use AlexItDev91\LaravelTelegramBot\Laravel\Notifications\TelegramBotNotificationChannel;
use AlexItDev91\LaravelTelegramBot\Laravel\Notifications\TelegramNotificationMessage;
use Illuminate\Notifications\Notification;
class DeployFinished extends Notification
{
private const string CHANNEL = 'alerts';
private const string TEXT = 'Deploy finished';
public function via(object $notifiable): array
{
return [TelegramBotNotificationChannel::class];
}
public function toTelegram(object $notifiable): TelegramNotificationMessage
{
return TelegramNotificationMessage::text(self::TEXT)
->channel(self::CHANNEL)
->parseMode(TelegramParseMode::HTML);
}
}
use AlexItDev91\LaravelTelegramBot\DTO\Messages\InlineKeyboardMarkup;
use AlexItDev91\LaravelTelegramBot\DTO\Messages\LinkPreviewOptions;
use AlexItDev91\LaravelTelegramBot\DTO\Messages\SendMessageData;
use AlexItDev91\LaravelTelegramBot\Facades\TelegramBot;
use AlexItDev91\LaravelTelegramBot\Support\TelegramCallbackData;
final class DeployAlert
{
private const string BOT = 'support';
private const string BUTTON_RETRY = 'Retry';
private const string BUTTON_OPEN = 'Open run';
private const string CALLBACK_RETRY = 'deploy:retry';
private const string CHAT_ID = '-1001234567890';
private const int RUN_ID = 42;
private const string RUN_URL = 'https://example.test/runs/42';
private const string TEXT = 'Build failed';
public function send(): mixed
{
return TelegramBot::bot(self::BOT)->sendMessage(new SendMessageData(
chatId: self::CHAT_ID,
text: self::TEXT,
linkPreviewOptions: LinkPreviewOptions::disabled(),
replyMarkup: InlineKeyboardMarkup::make()
->callback(self::BUTTON_RETRY, TelegramCallbackData::action(self::CALLBACK_RETRY)->with('run', self::RUN_ID))
->url(self::BUTTON_OPEN, self::RUN_URL),
));
}
}
use AlexItDev91\LaravelTelegramBot\DTO\Requests\SendRichMessageRequestData;
use AlexItDev91\LaravelTelegramBot\DTO\Rich\InputRichMessage;
use AlexItDev91\LaravelTelegramBot\Facades\TelegramBot;
TelegramBot::bot('support')->sendRichMessage(SendRichMessageRequestData::make(
chatId: '-1001234567890',
richMessage: InputRichMessage::html('<h1>Deploy</h1><p>Finished</p>')
->skipEntityDetection(),
));