file uploads, polls, payments, and AI chat responses - Gianguyen1234/app-doc GitHub Wiki
Let's add file uploads, polls, payments, and AI chat responses to your Laravel Telegram Bot.
1️⃣ Send & Receive Files 📁
Send a File to Users
Modify TelegramService.php
:
public function sendDocument($fileUrl, $caption = '')
{
return $this->telegram->sendDocument([
'chat_id' => $this->chat_id,
'document' => $fileUrl,
'caption' => $caption
]);
}
✅ Usage:
$telegram->sendDocument("https://example.com/sample.pdf", "Here’s your document!");
Receive Files from Users
Edit routes/web.php
:
Route::post('/telegram/webhook', function (Request $request, TelegramService $telegram) {
$update = $request->all();
if (isset($update['message']['document'])) {
$file_id = $update['message']['document']['file_id'];
$telegram->sendMessage("Thanks for the file! 📂\nFile ID: " . $file_id);
}
return response()->json(['status' => 'ok']);
});
2️⃣ Create Polls & Surveys 📊
Modify TelegramService.php
:
public function sendPoll($question, $options)
{
return $this->telegram->sendPoll([
'chat_id' => $this->chat_id,
'question' => $question,
'options' => json_encode($options)
]);
}
✅ Usage:
$options = ["Yes", "No", "Maybe"];
$telegram->sendPoll("Do you like Laravel?", $options);
3️⃣ Accept Payments 💰
Enable Telegram Payments
1️⃣ Open [@BotFather](https://t.me/BotFather)
2️⃣ Send /mybots
→ Select your bot
3️⃣ Go to Bot Settings → Payments → Add a Payment Provider (e.g., Stripe)
4️⃣ Get your Payment Token
Laravel Code to Send an Invoice
Modify TelegramService.php
:
public function sendInvoice($title, $description, $price, $payload)
{
return $this->telegram->sendInvoice([
'chat_id' => $this->chat_id,
'title' => $title,
'description' => $description,
'payload' => $payload,
'provider_token' => env('TELEGRAM_PAYMENT_TOKEN'),
'currency' => 'USD',
'prices' => json_encode(['label' => 'Total', 'amount' => $price * 100](/Gianguyen1234/app-doc/wiki/'label'-=>-'Total',-'amount'-=>-$price-*-100))
]);
}
✅ Usage:
$telegram->sendInvoice("Laravel Course", "Learn Laravel with this course!", 10, "order_123");
4️⃣ AI Chatbot (GPT-4 Powered) 🤖
OpenAI API Key
Step 1: GetGo to OpenAI → Create an account → Get an API Key
Step 2: Install OpenAI SDK
Run this in your Laravel project:
composer require openai-php/client
Step 3: Add AI Chat Response
Modify TelegramService.php
:
use OpenAI;
public function chatWithAI($message)
{
$client = OpenAI::client(env('OPENAI_API_KEY'));
$response = $client->completions()->create([
'model' => 'gpt-4',
'prompt' => $message,
'max_tokens' => 50,
]);
return trim($response['choices'][0]['text']);
}
✅ Usage:
Edit routes/web.php
:
Route::post('/telegram/webhook', function (Request $request, TelegramService $telegram) {
$update = $request->all();
if (isset($update['message']['text'])) {
$userMessage = $update['message']['text'];
$aiResponse = $telegram->chatWithAI($userMessage);
$telegram->sendMessage($aiResponse);
}
return response()->json(['status' => 'ok']);
});
🔥 Final Test
1️⃣ Send a file → Bot replies with "Thanks for the file!"
2️⃣ Start a poll → Users can vote (Yes, No, Maybe)
3️⃣ Send an invoice → Users can pay via Telegram
4️⃣ Send any message → AI responds dynamically 🤖