Core: User security

Created on 1 Mar 2018  路  3Comments  路  Source: php-telegram-bot/core

Hi,
I would like to know if it is possible to enable or add, quickly, something like a whitelist.
I'm developing a bot to control my Home Automation, and only threee Telegram accout must be authorized to send command to bot.
Is there just implemented something like a whitelist?

Thanks

question

Most helpful comment

Easiest solution is to extend Telegram class:


namespace YOUR\NAMESPACE;

use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Telegram;

class TelegramBot extends Telegram
{
    public function processUpdate(Update $update)
    {
        if ($this->isAuthorized([123, 432, 534])) {   //ids must be integers!
            return parent::processUpdate($update);
        }

        return new ServerResponse(
                [
                    'ok'          => false,
                    'description' => 'Access denied',
                ],
                $this->bot_username
            );
    }

    public function isAuthorized(array $authorized)
    {
        if ($this->update !== null) {
            $update_methods = [
                'getMessage',
                'getEditedMessage',
                'getChannelPost',
                'getEditedChannelPost',
                'getInlineQuery',
                'getChosenInlineResult',
                'getCallbackQuery',
            ];
            foreach ($update_methods as $update_method) {
                $object = call_user_func([$this->update, $update_method]);
                if ($object !== null && $from = $object->getFrom()) {
                    $user_id = $from->getId();
                    break;
                }
            }
        }

        return (!isset($user_id) || $user_id === null) ? false : in_array($user_id, $authorized, true);
    }
}

(untested code)

You will have to adjust hook/webook script to create new TelegramBot object instead of Telegram.

All 3 comments

Easiest solution is to extend Telegram class:


namespace YOUR\NAMESPACE;

use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Telegram;

class TelegramBot extends Telegram
{
    public function processUpdate(Update $update)
    {
        if ($this->isAuthorized([123, 432, 534])) {   //ids must be integers!
            return parent::processUpdate($update);
        }

        return new ServerResponse(
                [
                    'ok'          => false,
                    'description' => 'Access denied',
                ],
                $this->bot_username
            );
    }

    public function isAuthorized(array $authorized)
    {
        if ($this->update !== null) {
            $update_methods = [
                'getMessage',
                'getEditedMessage',
                'getChannelPost',
                'getEditedChannelPost',
                'getInlineQuery',
                'getChosenInlineResult',
                'getCallbackQuery',
            ];
            foreach ($update_methods as $update_method) {
                $object = call_user_func([$this->update, $update_method]);
                if ($object !== null && $from = $object->getFrom()) {
                    $user_id = $from->getId();
                    break;
                }
            }
        }

        return (!isset($user_id) || $user_id === null) ? false : in_array($user_id, $authorized, true);
    }
}

(untested code)

You will have to adjust hook/webook script to create new TelegramBot object instead of Telegram.

@marcolino7 Can I close off here?

@marcolino7 any update?

Was this page helpful?
0 / 5 - 0 ratings