Skip to content

Telegram Api and Mysql #1002

@shtrihs

Description

@shtrihs

Good day!

I can't make friends with telegram APi and mysql database.
Using a library longman/telegram-bot.

getUpdatesCLI.php

#!/usr/bin/env php
<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';
 
        $bot_api_key = '111111111111111111111111111';
        $bot_username = '123_bot';
        $mysql_credentials = [
            'host' => 'localhost',
            'port' => 3306, // optional
            'user' => '******',
            'password' => '123123',
            'database' => '*******',
        ];
 
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
 
    // Enable MySQL
    $telegram->enableMySql($mysql_credentials);
 
    // Handle telegram getUpdates request
    $telegram->handleGetUpdates();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // log telegram errors
    // echo $e->getMessage();
}

File getUpdatesCLI.php was created and started without errors.

hook_telegram.php

Code (Text):
<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';
//include ("telegram.php");
 
 
$commands_paths = [
    __DIR__ . '/Commands/',
];
 
// Define the IDs of all admin users in this array (leave as empty array if not used)
$admin_users = [
// 123,
];
 
        $bot_api_key = '1111111111111111111111111';
        $bot_username = '123_bot';
        $mysql_credentials = [
            'host' => 'localhost',
            'port' => 3306, // optional
            'user' => '*****',
            'password' => '123123',
            'database' => '*****',
        ];
 
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
 
    // Add commands paths containing your custom commands
    $telegram->addCommandsPaths($commands_paths);
 
       // Enable admin users
    $telegram->enableAdmins($admin_users);
 
 
    // Requests Limiter (tries to prevent reaching the API limits Telegram)
    $telegram->enableLimiter();
 
   $telegram->enableMySql($mysql_credentials);
 
    // Handle telegram webhook request
    $telegram->handle();
 
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    // log telegram errors
    // echo $e->getMessage();
}

If line $telegram->enableMySql($mysql_credentials); no, the telegrams responds normally, if the line is, then the answer is no. And nothing is written to the error log. How to be? In base also nothing is written

Put logging. When you add mysql, no entries appear in it. Without it - works


TelegramLog::initialize(
// Main logger that handles all 'error' and 'debug' logs.
    new Logger('telegram_bot', [
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_debug.log", Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_error.log", Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
    ]),
    // Updates logger for raw updates.
    new Logger('telegram_bot_updates', [
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_update.log", Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)),
    ])
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions