Home

Awesome

PHP Telegram Bot Manager

Join the bot support group on Telegram Donate

Scrutinizer Code Quality Codecov Tests Status

Latest Stable Version Dependencies Total Downloads License

This project builds on top of PHP Telegram Bot and as such, depends on it!

The main purpose of this mini-library is to make the interaction between your webserver and Telegram easier. I strongly suggest your read the PHP Telegram Bot instructions first, to understand what this library does exactly.

Installation and usage is pretty straight forward:

Require this package with Composer

composer require php-telegram-bot/telegram-bot-manager:^2.1

NOTE: This will automatically also install PHP Telegram Bot into your project (if it isn't already).

Advanced: Due to the fact that the core library is not a stable version yet, this project is partly locked to the core version, to ensure reliable functioning.

It is possible however, to override the core version that this library requires:

"require": {
    "php-telegram-bot/telegram-bot-manager": "^2.1",
    "longman/telegram-bot": "dev-master as 0.81"
}

This example will pull the master version of the core library, making it appear to be version 0.81, which then satisfies the requirement.

Performing actions

What use would this library be if you couldn't perform any actions?!

There are a few parameters available to get things rolling:

ParameterDescription
ssecret: This is a special secret value defined in the main manager.php file.
This parameter is required to call the script via browser!
aaction: The actual action to perform. (handle (default), webhookinfo, cron, set, unset, reset)
handle executes the getUpdates method; webhookinfo to get result from getWebhookInfo, cron executes cron commands; set / unset / reset the webhook.
lloop: Number of seconds to loop the script for (used for getUpdates method).
This would be used mainly via CLI, to continually get updates for a certain period.
iinterval: Number of seconds to wait between getUpdates requests (used for getUpdates method, default is 2).
This would be used mainly via CLI, to continually get updates for a certain period, every i seconds.
ggroup: Commands group for cron (only used together with cron action, default group is default).
Define which group of commands to execute via cron. Can be a comma separated list of groups.

via browser

Simply point your browser to the manager.php file with the necessary GET parameters:

Webhook

Set, unset and reset the webhook:

getUpdates

Handle updates once:

Handle updates for 30 seconds, fetching every 5 seconds:

cron

Execute commands via cron:

via CLI

When using CLI, the secret is not necessary (since it could just be read from the file itself).

Call the manager.php file directly using php and pass the parameters:

Webhook

Set, unset and reset the webhook:

getUpdates

Handle updates once:

Handle updates for 30 seconds, fetching every 5 seconds:

cron

Execute commands via cron:

Create the manager PHP file

You can name this file whatever you like, it just has to be somewhere inside your PHP project (preferably in the root folder to make things easier). (Let's assume our file is called manager.php)

Let's start off with a simple example that uses the webhook method:

<?php

use TelegramBot\TelegramBotManager\BotManager;

// Load composer.
require_once __DIR__ . '/vendor/autoload.php';

try {
    $bot = new BotManager([
        // Vitals!
        'api_key'      => '12345:my_api_key',

        // Extras.
        'bot_username' => 'my_own_bot',
        'secret'       => 'super_secret',
        'webhook'      => [
            'url' => 'https://example.com/manager.php',
        ]
    ]);
    $bot->run();
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

Set vital bot parameters

The only vital parameter is the API key:

$bot = new BotManager([
    // (string) Bot API key provided by @BotFather.
    'api_key' => '12345:my_api_key',
    ...
]);

Set extra bot parameters

Apart from the necessary API key, the bot can be easily configured using extra parameters.

Set the webhook? Enable admins? Add custom command paths?

All no problem!

The secret is a user-defined key that is required to execute any of the library's features via webhook. Best make it long, random and very unique!

For 84 random characters:

(You get 2 guesses why 84 is a good number :wink:)

Below is a complete list of all available extra parameters.

$bot = new BotManager([
    ...
    // (string) Bot username that was defined when creating the bot.
    'bot_username'     => 'my_own_bot',

    // (string) A secret password required to authorise access to the webhook.
    'secret'           => 'super_secret',

    // (array) All options that have to do with the webhook.
    'webhook'          => [
        // (string) URL to the manager PHP file used for setting up the webhook.
        'url'             => 'https://example.com/manager.php',
        // (string) Path to a self-signed certificate (if necessary).
        'certificate'     => __DIR__ . '/server.crt',
        // (int) Maximum allowed simultaneous HTTPS connections to the webhook.
        'max_connections' => 20,
        // (array) List the types of updates you want your bot to receive.
        'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
        // (string) Secret token to validate webhook requests.
        'secret_token'    => 'super_secret_token',
    ],

    // (bool) Only allow webhook access from valid Telegram API IPs.
    'validate_request' => true,
    // (array) When using `validate_request`, also allow these IPs.
    'valid_ips'        => [
        '1.2.3.4',         // single
        '192.168.1.0/24',  // CIDR
        '10/8',            // CIDR (short)
        '5.6.*',           // wildcard
        '1.1.1.1-2.2.2.2', // range
    ],

    // (array) All options that have to do with the limiter.
    'limiter'          => [
        // (bool) Enable or disable the limiter functionality.
        'enabled' => true,
        // (array) Any extra options to pass to the limiter.
        'options' => [
            // (float) Interval between request handles.
            'interval' => 0.5,
        ],
    ],

    // (array) An array of user ids that have admin access to your bot (must be integers).
    'admins'           => [12345],

    // (array) Mysql credentials to connect a database (necessary for [`getUpdates`](#using-getupdates-method) method!).
    'mysql'            => [
        'host'         => '127.0.0.1',
        'port'         => 3306,           // optional
        'user'         => 'root',
        'password'     => 'root',
        'database'     => 'telegram_bot',
        'table_prefix' => 'tbl_prfx_',    // optional
        'encoding'     => 'utf8mb4',      // optional
    ],

    // (array) List of configurable paths.
    'paths'            => [
        // (string) Custom download path.
        'download' => __DIR__ . '/Download',
        // (string) Custom upload path.
        'upload'   => __DIR__ . '/Upload',
    ],

    // (array) All options that have to do with commands.
    'commands'         => [
        // (array) A list of custom commands paths.
        'paths'   => [
            __DIR__ . '/CustomCommands',
        ],
        // (array) A list of all custom command configs.
        'configs' => [
            'sendtochannel' => ['your_channel' => '@my_channel'],
            'weather'       => ['owm_api_key' => 'owm_api_key_12345'],
        ],
    ],

    // (array) All options that have to do with cron.
    'cron'             => [
        // (array) List of groups that contain the commands to execute.
        'groups' => [
            // Each group has a name and array of commands.
            // When no group is defined, the default group gets executed.
            'default'     => [
                '/default_cron_command',
            ],
            'maintenance' => [
                '/db_cleanup',
                '/db_repair',
                '/message_admins Maintenance completed',
            ],
        ],
    ],

    // (string) Override the custom input of your bot (mostly for testing purposes!).
    'custom_input'     => '{"some":"raw", "json":"update"}',
]);

Using getUpdates method

Using the getUpdates method must not have a webhook parameter set and requires a MySQL database connection:

$bot = new BotManager([
    ...
    // Extras.
    'mysql' => [
        'host'         => '127.0.0.1',
        'port'         => 3306,           // optional
        'user'         => 'root',
        'password'     => 'root',
        'database'     => 'telegram_bot',
        'table_prefix' => 'tbl_prfx_',    // optional
        'encoding'     => 'utf8mb4',      // optional
    ],
]);

Now, the updates can be done either through the browser or via CLI.

Custom getUpdates output

A callback can be defined, to override the default output when updates are handled via getUpdates.

Example of the default output:

...
2017-07-10 14:59:25 - Updates processed: 1
123456: <text>
2017-07-10 14:59:27 - Updates processed: 0
2017-07-10 14:59:30 - Updates processed: 0
2017-07-10 14:59:32 - Updates processed: 0
2017-07-10 14:59:34 - Updates processed: 1
123456: <photo>
2017-07-10 14:59:36 - Updates processed: 0
...

Using custom callback that must return a string:

// In manager.php after $bot has been defined:
$bot->setCustomGetUpdatesCallback(function (ServerResponse $get_updates_response) {
    $results = array_filter((array) $get_updates_response->getResult());

    return sprintf('There are %d update(s)' . PHP_EOL, count($results));
});

output:

...
There are 0 update(s)
There are 0 update(s)
There are 2 update(s)
There are 1 update(s)
...

Development

When running live bot tests on a fork, you must enter the following environment variables to your repository settings:

API_KEY="12345:your_api_key"
BOT_USERNAME="username_of_your_bot"

It probably makes sense for you to create a new dummy bot for this.

Security

See SECURITY for more information.

Donate

All work on this bot consists of many hours of coding during our free time, to provide you with a Telegram Bot library that is easy to use and extend. If you enjoy using this library and would like to say thank you, donations are a great way to show your support.

Donations are invested back into the project :+1:

Thank you for keeping this project alive :pray:


<div align="center"> <b> <a href="https://tidelift.com/subscription/pkg/packagist-php-telegram-bot-telegram-bot-manager?utm_source=packagist-php-telegram-bot-telegram-bot-manager&utm_medium=referral&utm_campaign=readme-footer">Get professional support for this package with a Tidelift subscription</a> </b> <br> <sub> Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. </sub> </div>