mrmrn
mrmrn

Reputation: 75

how to catch any error in telegram bot?

I am wondering how to catch any possible error in telegram bot API. because of when an error occurs, telegram sticks to it and does not answering another requests. I want to get rid if any errors that may cause by bugs in my code,or web-services I am using or blocking the bot or... how can I avoid from sticking to one request in telegram bot API with PHP? I think what I need is something like bellow code but more general for any kind of error:

try {

    $telegram->sendMessage([
        'chat_id'                  => '<PERSONS_ID>',
        'text'                     => 'Here is some text',
    ]);
} catch (TelegramResponseException $e) {
    $errorData = $e->getResponseData();

    if ($errorData['ok'] === false) {
        $telegram->sendMessage([
            'chat_id' => '<ADMINISTRATOR ID>',
            'text'    => 'There was an error for a user. ' . $errorData['error_code'] . ' ' . $errorData['description'],
        ]);
    }
}

Upvotes: 2

Views: 3403

Answers (2)

ljsherlock
ljsherlock

Reputation: 1

Indirectly related, but adding this answer in case someone find this searching for use with WordPress, and since I came up with this yesterday:

// https://developer.wordpress.org/reference/hooks/is_wp_error_instance/
add_action('is_wp_error_instance', 'log_registration_errors' );
function telegram_log ( $message ) {
    $apiToken = "[YOUR API TOKEN]"; 

    $data = [ 
        'chat_id' => '[YOUR CHAT ID]', 
        'text' => get_site_url() .chr(10).chr(10). $message,
        "parse_mode" => "Markdown"
    ]; 

    $ch = curl_init();
    // Set query data here with the URL
    curl_setopt($ch, CURLOPT_URL, "http://api.telegram.org/bot$apiToken/sendMessage?". http_build_query($data) ); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // the old file_get_contents solution limited the length of the query string.
    // return file_get_contents("http://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) ); 
    $response = curl_exec($ch);
    curl_close($ch);
}

Using curl instead of file_read_contents because for some reason it was limiting the query string and thus only half the message come through and the code was not highlighted.

Encode Objects and Arrays as JSON for transport:

function json_code_block ($code) {
    return chr(96).chr(96).chr(96)."json".chr(10).
    json_encode( $code, JSON_PRETTY_PRINT )
    .chr(10).chr(96).chr(96).chr(96);
}

Example usage:

telegram_log( 
    "WP Error: " . chr(10) . json_code_block( $errors ) 
    . chr(10) . "POST: " . chr(10) . json_code_block( $_POST ) 
    . chr(10) . "QUERY:" .chr(10) . json_code_block( $wp_query )
);

Upvotes: 0

mrmrn
mrmrn

Reputation: 75

finally I solved the issue by a trick.I created another bot for error handling. So I have a bot X and an error handling bot Y. here the POST method I receive webhooks from telegram:

public function postWebhook(Request $request)

    { .....
        try
        { ....
         bot X token
         everything the bot want to do...
        }
        catch (\Exception $e) 
        {
             bot Y send me the probable problem in my code....
        }
        catch (Throwable $e)
        {
               bot Y send me the probable problem in telegram such 
               as blocking ,..
        }

now I prevent sticking in an error and bot works great. even I am notified if a part of my web service has problem or my code has a mistake.

Upvotes: 0

Related Questions