There are several packages available for Laravel that allow you to interact with Telegram bots. One popular package is called "irazasyed/telegram-bot-sdk". Here are the steps to get started:
- Install the package via Composer:
composer require irazasyed/telegram-bot-sdk
-
Create a new Telegram bot by following the instructions on the Telegram website: https://core.telegram.org/bots#3-how-do-i-create-a-bot
-
Once you have created your bot, you will receive an API token. Copy this token and add it to your Laravel .env file:
TELEGRAM_BOT_TOKEN=your-bot-token-here
- Create a new controller in your Laravel app to handle incoming messages from the Telegram bot. Here is an example controller that simply responds to any incoming message with "Hello!":
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\Laravel\Facades\Telegram;
class TelegramController extends Controller
{
public function handle(Request $request)
{
$update = Telegram::commandsHandler(true);
$message = $update->getMessage();
$chat_id = $message->getChat()->getId();
Telegram::sendMessage([
'chat_id' => $chat_id,
'text' => 'Hello!'
]);
}
}
- Add a new route to your Laravel app to handle incoming requests from the Telegram bot. Here is an example route that maps to the "handle" method in the controller:
Route::post('/telegram/webhook', 'TelegramController@handle');
- Finally, set up the webhook for your Telegram bot by running the following command in your Laravel app:
php artisan telegram:webhook
This will register your Laravel app as the webhook URL for your Telegram bot. Now, any incoming messages to your bot will be sent to your Laravel app and handled by the "handle" method in the controller.
Note: This is just a basic example to get you started. You can customize the controller to handle different types of messages, store messages in a database, etc.