4

I'm trying to use the Telegram API to make an online advertising app with PHP, but the problem I have is that I can't even understand making request to telegram website. This is a short code I wrote based on Telegram's 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 have any idea how to make it work?

Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
Amirmasoud
  • 590
  • 4
  • 11
  • 27
  • 1
    -.-- --- ..- / ... .... --- ..- .-.. -.. / .- ... -.- / - .... . / ...- . -. -.. --- .-. .-.-.- You should ask the vendor, do they have support forums? Otherwise are you getting any errors? Be more specific. Do you just need to [tell cURL](http://php.net/manual/en/function.curl-setopt.php) to return transfer maybe? – ficuscr Jul 02 '15 at 17:40
  • no forum, and it just returns "501 Not Implemented" on "nginx/0.3.33", as I understood based on the protocol they use we should make a keep-alive connection and post to the url they gave to us. – Amirmasoud Jul 02 '15 at 17:45
  • Ahh, maybe the you need to tell cURL to follow redirects? `CURLOPT_FOLLOWLOCATION ` Also, if it is routed as HTTPS might need to tell cURL To ignore certificate validation. No other authentication required when making request? – ficuscr Jul 02 '15 at 17:48
  • I changed curl_setopt() to curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_SSL_VERIFYHOST => FALSE )); Still not working. – Amirmasoud Jul 02 '15 at 17:55
  • http://stackoverflow.com/questions/3757071/php-debugging-curl – ficuscr Jul 02 '15 at 17:56
  • I checked it thats the reslt: Verbose information: * Rebuilt URL to: 149.154.167.40/ * Hostname was found in DNS cache * Hostname in DNS cache was stale, zapped * Trying 149.154.167.40... * Connected to 149.154.167.40 (149.154.167.40) port 80 (#0) > GET / HTTP/1.1 Host: 149.154.167.40 Accept: */* < HTTP/1.1 501 Not Implemented < Server: nginx/0.3.33 < Date: Thu, 02 Jul 2015 18:19:07 GMT < Content-Type: text/html < Content-Length: 181 < Connection: close < * Closing connection – Amirmasoud Jul 02 '15 at 18:21
  • have you ever solved this? – T.Todua Aug 09 '17 at 15:39
  • @T.Todua GitHub repo if it can help: https://github.com/amirmasoud/telegram-bot-manager – Amirmasoud Aug 09 '17 at 19:35

2 Answers2

6

The Telegram API is a pain to use, you have to apply all sort of encryption sorcery to work with their MTProto protocol and there's very little reference or example for PHP available. I would suggest you use their new Bot API. It is a service the created that abstracts all the MTProto interactions behind a simple HTTP layer. You first need to generate a bot using their 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 a webhook instead of polling for updates. You can find more information in the API documentation I linked.

Chris Brand
  • 1,962
  • 16
  • 9
  • This API doesn't have the method I want, one which obtains message info by channel/id, so I need an answer to the author's original question. :/ – dw1 Nov 26 '21 at 17:32
0

You can use this library:

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

Simple sample code:

<?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;
Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81