2017 © Pedro Peláez
 

library telegram-bot

Create your Telegram Bot in 5 minutes with Laravel

image

black-river/telegram-bot

Create your Telegram Bot in 5 minutes with Laravel

  • Monday, August 22, 2016
  • by black-river
  • Repository
  • 3 Watchers
  • 9 Stars
  • 99 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

Laravel Telegram Bot

cover, (*1)

Installation

Require this package, with Composer:, (*2)

composer require black-river/telegram-bot

Add the service provider to the providers array of your config/app.php:, (*3)

BlackRiver\TelegramBot\ServiceProvider::class,

Configuration

Publish the config file:, (*4)

php artisan vendor:publish --provider="BlackRiver\TelegramBot\ServiceProvider"

Set environment variables in your .env:, (*5)

APP_URL="http://your-bot.com"
...
TELEGRAM_TOKEN="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"

To use a self-signed certificate, you should also specify the certificate path:, (*6)

TELEGRAM_CERTIFICATE="/etc/nginx/ssl/your-bot.com.crt"

Quickstart

Define the default webhook route in your route file:, (*7)

Route::post(config('telegram.token'), function (BlackRiver\TelegramBot\Bot $bot) {
    $bot->listen();
});
  • Use the Bot's listen method to handle commands.

Set up the webhook url:, (*8)

php artisan webhook:set

To ensure the bot is ready, send the /ping message:, (*9)

ping-command, (*10)

To make sure there is no middleware or prefix that could "block" the default webhook route, check your app/Providers/RouteServiceProvider.php., (*11)

Webhook URL

You can change the default webhook route to your own:, (*12)

Route::post('your-secret-path', function (BlackRiver\TelegramBot\Bot $bot) {
    $bot->listen();
});
php artisan webhook:set your-secret-path

To remove the webhook integration, run php artisan webhook:remove., (*13)

Bot Commands

Create a new Bot Command in the app/Http/BotCommands directory:, (*14)

php artisan make:bot-command NameCommand

Edit the handle method of app/Http/BotCommands/NameCommand.php:, (*15)

$this->client->send('sendMessage', [
    'chat_id' => $this->request->json('message.chat.id'),
    'text' => 'Hello, '.trim($message),
]);
  • Use the Client's send method to call any of the available methods., (*16)

  • Use the Client's save method to save Telegram files., (*17)

  • The Client and Request are available within a Command via $this->client and $this->request respectively., (*18)

Add the new command to the commands array of your config/telegram.php:, (*19)

'/name' => App\Http\BotCommands\NameCommand::class,

Send the /name Johnny message:, (*20)

name-command, (*21)

Raw Webhook

Route::post('your-secret-path', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) {
    // $bot->listen();

    $update = $request->json()->all();

    $client->send('sendMessage', [
        'chat_id' => $request->json('message.chat.id'),
        'text' => 'I\'ve got it!',
    ]);
});

Facades

Add facades to the aliases array of your config/app.php:, (*22)

'TelegramBot' => BlackRiver\TelegramBot\Facades\TelegramBot::class,
'TelegramApi' => BlackRiver\TelegramBot\Facades\TelegramApi::class,

Usage:, (*23)

Route::post('your-secret-path', function () {
    // TelegramBot::listen();

    TelegramApi::send('sendMessage', [
        'chat_id' => Request::json('message.chat.id'),
        'text' => 'Hey!',
    ]);
});

Examples

Send a photo to your chat:, (*24)

Route::post('photo', function (BlackRiver\TelegramBot\Client $client) {
    $client->send('sendPhoto', [
        'chat_id' => 'your-chat-id',
        'photo' => fopen(storage_path('photo.png'), 'r'),
    ]);
});

Save incoming files:, (*25)

Route::post('your-secret-path', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) {
    $doc = $request->json('message.document');

    $filename = $doc['file_name'];

    $file = $client->send('getFile', ['file_id' => $doc['file_id']]);

    $client->save($file['result']['file_path'], storage_path($filename));
});

Extending

To extend the Client, add a new macro to the boot method of your app/Providers/AppServiceProvider.php:, (*26)

app('BlackRiver\TelegramBot\Client')->macro('sendUploadedPhoto',
    function ($chatId, \Illuminate\Http\UploadedFile $photo) {
        $saved = $photo->move(storage_path(), $photo->getClientOriginalName());

        $this->send('sendPhoto', [
            'chat_id' => $chatId,
            'photo' => fopen($saved->getRealPath(), 'r'),
        ]);
    }
);

Send an uploaded photo to your chat:, (*27)

Route::post('upload', function (Illuminate\Http\Request $request, BlackRiver\TelegramBot\Client $client) {
    $client->sendUploadedPhoto('your-chat-id', $request->file('photo'));
});

Handle errors

The Client uses Guzzle Http Client to interact with Telegram API, so you can handle Guzzle Exceptions:, (*28)

try {
    $client->send('methodName', []);
} catch (GuzzleHttp\Exception\ClientException $e) {
    // 400 level errors...
} catch (GuzzleHttp\Exception\ServerException $e) {
    // 500 level errors...
} catch (GuzzleHttp\Exception\RequestException $e) {
    // Connection timeout, DNS errors, etc.
}

Lumen

Require this package, with Composer:, (*29)

composer require black-river/telegram-bot

Add the service provider to the Register Service Providers section of your bootstrap/app.php:, (*30)

$app->register(BlackRiver\TelegramBot\ServiceProvider::class);

Set the APP_URL, TELEGRAM_TOKEN and TELEGRAM_CERTIFICATE variables in your .env., (*31)

Copy the vendor's telegram.php config file to your config directory:, (*32)

mkdir config/ && cp vendor/black-river/telegram-bot/config/telegram.php config/

Define the default webhook route in your route file:, (*33)

$app->post(config('telegram.token'), function (BlackRiver\TelegramBot\Bot $bot) {
    $bot->listen();
});

License

Laravel Telegram Bot is licensed under The MIT License (MIT)., (*34)

The Versions

22/08 2016

dev-master

9999999-dev

Create your Telegram Bot in 5 minutes with Laravel

  Sources   Download

MIT

The Requires

 

by Oleg Seleznev

laravel api lumen bot telegram

22/08 2016

0.7.0

0.7.0.0

Create your Telegram Bot in 5 minutes with Laravel

  Sources   Download

MIT

The Requires

 

by Oleg Seleznev

laravel api lumen bot telegram