Gene_Nostrada
Gene_Nostrada

Reputation: 346

How to Use TDLib with PHP to Retrieve Data from a Telegram Channel?

I'm trying to use TDLib with PHP to retrieve data from a public Telegram channel. I couldn't find clear documentation or examples online for achieving this. Below is what I have tried so far.

I am using FFI to load tdjson.dll and interact with TDLib. I’ve successfully initialized the client and set parameters for authentication. However, I’m not sure how to proceed with:

Joining a public channel by its username or ID.
Retrieving messages or posts from that channel.

Here is my current code:


<?php

$ffi = FFI::cdef("
    void *td_json_client_create();
    void td_json_client_destroy(void *client);
    void td_json_client_send(void *client, const char *request);
    const char *td_json_client_receive(void *client, double timeout);
", "tdjson.dll");


$client = $ffi->td_json_client_create();


function sendRequest($ffi, $client, $requestArray)
{
    $ffi->td_json_client_send($client, json_encode($requestArray));
}

function receiveResponse($ffi, $client, $timeout = 10.0)
{
    $response = $ffi->td_json_client_receive($client, $timeout);
    return $response ? json_decode($response, true) : null;
}


sendRequest($ffi, $client, [
    '@type' => 'setTdlibParameters',
    'parameters' => [
        'api_id' => my_api_id, 
        'api_hash' => 'my_api_hash', 
        'device_model' => 'Desktop',
        'system_version' => 'Windows',
        'application_version' => '1.0',
        'database_directory' => './tdlib_data',
        'use_message_database' => false,
        'use_secret_chats' => false,
        'system_language_code' => 'en',
    ]
]);
receiveResponse($ffi, $client);

What I Need Help With:

How can I join a public Telegram channel using TDLib in PHP?
What is the correct way to fetch messages from that channel?

What I've Tried:

Reading the TDLib documentation and examples, but most of them are either in Python or not relevant to PHP.
Experimenting with various API calls (like getChat, searchPublicChat) but with no success.

Notes:

I’m able to authenticate with Telegram successfully.
The account is already logged in, so there is no need for additional authentication steps.

Any guidance, example code, or suggestions would be greatly appreciated! Thank you in advance.

Upvotes: 2

Views: 129

Answers (1)

AYMENJD
AYMENJD

Reputation: 83

First you need to load the chat by username using searchPublicChat(username) and then call joinChat(searchPublicChat.id).

See: https://core.telegram.org/tdlib/getting-started#getting-chat-messages.

All TDLib methods are available and up-to-date at: https://github.com/tdlib/td/blob/master/td/generate/scheme/td_api.tl.

Upvotes: 0

Related Questions