Using the telegram API with PHP

I'm trying to use the Telegram API to make an online advertising app with PHP, but the problem is I don't even understand how to make a telegram request. This is the short code I wrote based on Telegram API and protocol:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Length" content="348">
    <meta http-equiv="Connection" content="keep-alive">
    <meta http-equiv="Host" content="149.154.167.40:80">
</head>

<body>
<?php
$url = '149.154.167.40';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);

$result = curl_exec($curl);

echo $result;

?>
</body>
</html>

      

Does anyone know how to get it to work?

+3


source to share


2 answers


The Telegram API is a pain to use, you have to apply all kinds of encryption witchcraft to work with your MTProto protocol and there are very few links or examples for PHP there. I suggest you use your new Bot API . This is a built service that abstracts all MTProto interactions behind a simple HTTP layer. First you need to generate the bot using the Bot Father and then you use the ID to interact with the API.

Receiving new messages (polling):

<?php

$bot_id = "<bot ID generated by BotFather>";

# Note: you want to change the offset based on the last update_id you received
$url = 'https://api.telegram.org/bot' . $bot_id . '/getUpdates?offset=0';
$result = file_get_contents($url);
$result = json_decode($result, true);

foreach ($result['result'] as $message) {
    var_dump($message);
}

      



Sending messages:

# The chat_id variable will be provided in the getUpdates result
$url = 'https://api.telegram.org/bot' . $bot_id . '/sendMessage?text=message&chat_id=0';
$result = file_get_contents($url);
$result = json_decode($result, true);

var_dump($result['result']);

      

You can also use webhook instead of polling for updates. You can find more information in the API documentation that I linked.

+6


source


You can use this library:

PHP implementation of the MTProto telegram protocol (better tg-cli) https://github.com/danog/MadelineProto



Simple code example:

<?php

if (!file_exists('madeline.php')) {
    copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

$MadelineProto = new \danog\MadelineProto\API('session.madeline');
$MadelineProto->start();

$me = $MadelineProto->get_self();

\danog\MadelineProto\Logger::log($me);

if (!$me['bot']) {
    $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => "Hi!\nThanks for creating MadelineProto! <3"]);
    $MadelineProto->channels->joinChannel(['channel' => '@MadelineProto']);

    try {
        $MadelineProto->messages->importChatInvite(['hash' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg']);
    } catch (\danog\MadelineProto\RPCErrorException $e) {
    }

    $MadelineProto->messages->sendMessage(['peer' => 'https://t.me/joinchat/Bgrajz6K-aJKu0IpGsLpBg', 'message' => 'Testing MadelineProto!']);
}
echo 'OK, done!'.PHP_EOL;

      

0


source







All Articles