php-telegram-bot / telegram-bot-manager

PHP Telegram Bot Manager
MIT License
208 stars 65 forks source link

bot manager respond: invalid Access #55

Closed AlessandroMarchetto closed 4 years ago

AlessandroMarchetto commented 4 years ago

Support Question

Required Information

? !
Operating system Name and version
PHP Telegram Bot Manager version 1.5.0
PHP Telegram Bot version 0.62
PHP version 7,2
MySQL version 10,2
Update Method Webhook
Self-signed certificate no
RAW update (if available) {...}

Summary

HI, after many day's of try i dediced to ask for help. I'm trying to create a bot for telegram, i'm using hostinger cloud global service, and i connected via SSH to my linux space (i thing hostinger is linux) My first test was with set.php, unset.php and it work perfectly. But i decided to use manager.php so i can set password (secret) to protect set, unset and reset.

If i try use manager.php from ssh i can do set, unset, reset, webhookinfo without any problem. so the command: php manager.php a=reset / set / unset / WebHookInfo The Work without any problem If i try from browser i get always invalid access. https://mybotlink/manager.php?s=123456789?a=set/unset/reset/WebHookInfo return always invalid access

I spend a lot of time (3 full days) without found a solution. Someone could help me to solve this problem ?

After my name the content of composer.json and manager.php under //Bot Setting and // MySql Setting all parameter are not real, made only for this support help I hope this is enough to discover where is my error. Thank's a lot for help Alex

<- This is content of composer.json: -------------------------------------->

{
    "name": "telegram-bot/example-bot",
    "type": "project",
    "description": "PHP Telegram Bot Example",
    "keywords": ["telegram", "bot", "example"],
    "license": "MIT",
    "homepage": "https://github.com/php-telegram-bot/example-bot",
    "support": {
        "issues": "https://github.com/php-telegram-bot/example-bot/issues",
        "source": "https://github.com/php-telegram-bot/example-bot"
    },
    "authors": [
        {
            "name": "PHP Telegram Bot Team",
            "homepage": "https://github.com/php-telegram-bot/example-bot/graphs/contributors",
            "role": "Developer"
        }
    ],
    "require": {
        "php-telegram-bot/telegram-bot-manager": "*",
        "longman/telegram-bot": "*"
    }
}

<- This is content of manager.php: -------------------------------------->

<?php
/**
 * README
 * This configuration file is intended to be used as the main script for the PHP Telegram Bot Manager.
 * Uncommented parameters must be filled
 *
 * For the full list of options, go to:
 * https://github.com/php-telegram-bot/telegram-bot-manager#set-extra-bot-parameters
 */

// By Alex to show \n
header('Content-type: text/plain');

// use TelegramBot\TelegramBotManager\BotManager;

// Bot Settings
$bot_api_key      = 'my api info';
$bot_username     = 'my channel info';
$bot_webhook_url  = 'https://mybotlink/manager.php';
$bot_secret       = '1234567890';
// MySql Settings
$mysql_host       = '127.0.0.1';
$mysql_port       = 3306;
$mysql_database   = 'my db name';
$mysql_user       = 'my db user';
$mysql_password   = 'my db password';

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

// Get ip info request to show if ip is correct
// $ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
// echo 'indirizzo ip : ' . $ip . PHP_EOL;

try {
    $bot = new TelegramBot\TelegramBotManager\BotManager([
        // Add you bot's API key and name
        'api_key'      => $bot_api_key,
        'bot_username' => $bot_username,

        // Secret key required to access the webhook
        'secret'       => $bot_secret,

        'webhook'      => [
              // When using webhook, this needs to be uncommented and defined
              'url' => $bot_webhook_url,
              // Use self-signed certificate
              // 'certificate' => __DIR__ . '/server.crt',
              // Limit maximum number of connections
              'max_connections' => 5,
              // (array) List the types of updates you want your bot to receive.
              'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
          ],

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

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

          // (array) All options that have to do with commands.
          'commands'         => [
              // (array) A list of custom commands paths.
              'paths'   => [
                      __DIR__ . '/Commands',
                  ],
              ],

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

          // (array) Paths where the log files should be put.
          'logging'          => [
              // (string) Log file for all incoming update requests.
              'update' => __DIR__ . '/LOG/Bot-update.log',
              // (string) Log file for debug purposes.
              'debug'  => __DIR__ . '/LOG/Bot-debug.log',
              // (string) Log file for all errors.
              'error'  => __DIR__ . '/LOG/Bot-error.log',
          ],

          // (array) Mysql credentials to connect a database (necessary for [`getUpdates`](#using-getupdates-method) method!).
          'mysql'            => [
              'host'         => $mysql_host,
              'port'         => $mysql_port,
              'user'         => $mysql_user,
              'password'     => $mysql_password ,
              'database'     => $mysql_database,
              // 'table_prefix' => 'tbl_prfx_',    // optional
              // 'encoding'     => 'utf8mb4',      // optional
          ],

          // (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',
                      '/log_rotate',
                      '/message_admins Maintenance completed',
                  ],
              ],
          ],

          // (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,
              ],
          ],

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

    ]);

    $bot->run();

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

catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    echo 'Telegram Excedption : ' . $e . PHP_EOL;
    // Log telegram errors
    Longman\TelegramBot\TelegramLog::error($e);
}
catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
    // Silence is golden!
    // Uncomment this to catch log initialisation errors
    echo 'Telegram Log Excedption : ' . $e . PHP_EOL;
}
catch (\Exception $e) {
    echo 'exception : ' . $e->getMessage() . PHP_EOL;
}
noplanman commented 4 years ago

Hi @AlessandroMarchetto!

So sorry to hear that you've spent so much time on this, but well done for reaching out 💪

As long as the secret is correct, and your IP is really the one added to the valid_ips array, the request should work. Could you please try to set validate_request to false to make sure that's not the problem?

Also, $bot->setCustomGetUpdatesCallback(...); must come before $bot->run();

AlessandroMarchetto commented 4 years ago

Hi, thank's a lot for your answer. I put validate request as false // (bool) Only allow webhook access from valid Telegram API IPs. 'validate_request' => false, I inserted an echo to check my ip $ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; echo 'indirizzo ip : ' . $ip . PHP_EOL;

The ip show is my public ip, i also added my admins id and with /help i can see both admin and user command.

i use my url bott + manager.php?s=1234567890?a=reset

and now is working i can reset, set, unset

i don't know why, now it work also with // (bool) Only allow webhook access from valid Telegram API IPs. 'validate_request' => true,

after unch i will try to move again $bot=... after $bot->run(); $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)); }); $bot->run();

Thank's a lot for you help Bye and thank's Alex

noplanman commented 4 years ago

Glad to hear that! So all good then?

AlessandroMarchetto commented 4 years ago

About manager, every think is ok. But i'm trying to write 2 bot, one to access coinbase api and get some result, and another to control channel and contents. And i can't find example to use, and poor documentation. But i study and try to solve.

Thank's very much for your help Alex