Telegram Bot features : 1️⃣ Send Images 📷 2️⃣ Send Buttons (Inline Keyboard) 🎛️ 3️⃣ Handle User Replies (Webhook) 🔄 - Gianguyen1234/app-doc GitHub Wiki
Let’s add more features to your Laravel Telegram Bot:
1️⃣ Send Images 📷
2️⃣ Send Buttons (Inline Keyboard) 🎛️
3️⃣ Handle User Replies (Webhook) 🔄
1️⃣ Send Images 📷
Modify your TelegramService.php
to send images:
public function sendPhoto($photoUrl, $caption = '')
{
return $this->telegram->sendPhoto([
'chat_id' => $this->chat_id,
'photo' => $photoUrl,
'caption' => $caption
]);
}
Usage Example
$telegram->sendPhoto("https://example.com/image.jpg", "Here’s an image!");
2️⃣ Send Buttons (Inline Keyboard) 🎛️
Modify your TelegramService.php
to send messages with buttons:
public function sendMessageWithButtons($message, $buttons)
{
return $this->telegram->sendMessage([
'chat_id' => $this->chat_id,
'text' => $message,
'reply_markup' => json_encode([
'inline_keyboard' => $buttons
])
]);
}
Usage Example
$buttons = [
[
['text' => 'Visit Website', 'url' => 'https://yourwebsite.com'],
['text' => 'Contact Support', 'url' => 'https://t.me/your_support_bot']
],
];
$telegram->sendMessageWithButtons("Choose an option:", $buttons);
✅ Inline buttons let users click links inside the chat.
3️⃣ Handle User Replies (Webhook) 🔄
Step 1: Set Webhook in Telegram
Run this in your terminal to set your webhook URL:
curl -X POST "https://api.telegram.org/botYOUR_BOT_TOKEN/setWebhook?url=https://your-app.com/telegram/webhook"
Replace YOUR_BOT_TOKEN
and your-app.com
with your real bot token & Laravel app URL.
Step 2: Create Webhook Route in Laravel
Edit routes/web.php
:
use Illuminate\Http\Request;
use App\Services\TelegramService;
Route::post('/telegram/webhook', function (Request $request, TelegramService $telegram) {
$update = $request->all();
if (isset($update['message']['text'])) {
$chatId = $update['message']['chat']['id'];
$text = strtolower($update['message']['text']);
if ($text === '/start') {
$telegram->sendMessage("Welcome to the bot! 🚀");
} elseif ($text === 'hello') {
$telegram->sendMessage("Hello there! How can I help?");
} else {
$telegram->sendMessage("I don't understand that command. 🤔");
}
}
return response()->json(['status' => 'ok']);
});
✅ Now your bot can reply to user messages dynamically!
Final Test 🎯
1️⃣ Run your Laravel app.
2️⃣ Send /start
or hello
to your bot in Telegram.
3️⃣ Your bot should reply instantly! 🚀