9951 explained code solutions for 126 technologies


php-laravelHow can I set up a Telegram bot using PHP and Laravel?


  1. First, you need to create a Telegram bot using BotFather in Telegram. You will receive an API token after the bot is created.

  2. Then, you can use the Telegram Bot PHP SDK to set up your bot using PHP and Laravel. You can install the SDK using Composer:

composer require irazasyed/telegram-bot-sdk
  1. After the SDK is installed, you can create a Telegram Bot class in your Laravel project. You can use the following code as an example:
<?php

namespace App\Telegram;

use Telegram\Bot\Api;

class Bot
{
    protected $telegram;

    public function __construct()
    {
        $this->telegram = new Api(env('TELEGRAM_BOT_TOKEN'));
    }

    public function sendMessage($chatId, $text)
    {
        $this->telegram->sendMessage([
            'chat_id' => $chatId,
            'text' => $text
        ]);
    }
}
  1. You need to add your Telegram bot token to your .env file:
TELEGRAM_BOT_TOKEN=<your-token-here>
  1. Finally, you can use the Bot class in your controllers or routes to send messages. For example, you can use the following code to send a message to a specific chat:
$bot = new Bot();
$bot->sendMessage($chatId, 'Hello World!');

Helpful links

Edit this code on GitHub