PHP code example of cawa0505 / telegram-bot

1. Go to this page and download the library: Download cawa0505/telegram-bot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cawa0505 / telegram-bot example snippets


BlackRiver\TelegramBot\ServiceProvider::class,

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

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

$this->client->send('sendMessage', [
    'chat_id' => $this->request->json('message.chat.id'),
    'text' => 'Hello, '.trim($message),
]);

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

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!',
    ]);
});

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

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

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

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

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));
});

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'),
        ]);
    }
);

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

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.
}

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

$app->post(config('telegram.token'), function (BlackRiver\TelegramBot\Bot $bot) {
    $bot->listen();
});
bash
php artisan vendor:publish --provider="BlackRiver\TelegramBot\ServiceProvider"
bash
php artisan webhook:set
bash
php artisan make:bot-command NameCommand
bash
mkdir config/ && cp vendor/black-river/telegram-bot/config/telegram.php config/