Яндекс.Метрика
qr-код - Как-то так стрелка - Как-то так
Leading Economist + ... пиктограмма - Как-то Так THIS     Economic literacy and web solutions
Персональная помощь в финансово-экономическом моделировании
бизнеса, разработке простых CRM-систем и интеграции ИИ
Беру проекты на подработку - Пишите мне!
If you don't like what you get, change what you give. © Carlos Castaneda     KAKTOTAK.BY - все услуги от экономического проектирования до разработки WEB-проектов: индивидуальных CRM-систем, онлайн-магазинов, лэндингов    Cooperation - on an ongoing basis    Be in the topic   -    SUBSCRIBE      My notes are ECONOMICS +  в Telegram   telegram  

How to set up accounts for subscribers and unsubscribers of a Telegram channel using PHP MadelineProto

How to set up accounts for subscribers and unsubscribers of a Telegram channel using PHP MadelineProto

If you run Telegram channel sooner or later the question arises: why do people subscribe when they unsubscribe and how to understand the effectiveness of publications and advertising. Telegram itself shows almost nothing: only the total number of participants. He does not keep any logs of "who came / who left".

But there is a way to track all this accurately to the user. And not through services, but with your own hands in PHP, using the MadelineProto library - a powerful tool for working with Telegram via the MTProto API.

Why do I need to keep track of subscribers and unsubscribers?

Having the data:

  • who subscribed;
  • who left;
  • at what time;
  • after which advertisement was the influx;
  • which post caused the surge;

you can:

  • understand the effectiveness of the content;
  • evaluate the work of advertising;
  • see the quality of the attracted audience;
  • recognize outflow moments;
  • predict the growth of the channel.

Why MadelineProto?

MadelineProto:

  • free;
  • works as a full-fledged Telegram client;
  • allows you to get the list of channel participants directly;
  • works without a webhook and without a bot.

Important: bot does not see subscriptions and unsubscriptions in channels - only the MTProto client.

Solution architecture

  • We periodically receive a list of channel participants.
  • Compare it with the previous saved list.
  • We determine who came and who left.
  • Saving the data to a file or database.

Example of implementation in PHP

1. Installing MadelineProto

composer require danog/madelineproto

2. Initializing and launching a session

require 'vendor/autoload.php';

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

3. Getting a list of Telegram channel participants

$participants = [];
$offset = 0;

do {
    $result = $MadelineProto->channels->getParticipants([
        'channel' => '@my_channel',
        'filter' => ['_' => 'channelParticipantsRecent'],
        'offset' => $offset,
        'limit' => 200,
        'hash' => 0
    ]);

    foreach ($result['participants'] as $user) {
        $participants[] = $user['user_id'];
    }

    $offset += 200;
} while (count($result['participants']) > 0);

4. Comparing the current list with the previous one

$previous = file_exists('members.json')
    ? json_decode(file_get_contents('members.json'), true)
    : [];

$new = array_diff($participants, $previous);
$gone = array_diff($previous, $participants);

5. Displaying the results

if ($new) {
    echo \"New subscribers: \" . implode(', ', $new) . PHP_EOL;
}

if ($gone) {
    echo\"Unsubscribed: \" . implode(', ', $gone) . PHP_EOL;
}

6. Updating the list file

file_put_contents('members.json', json_encode($participants));

Automation via CRON

To start automatically every 10 minutes:

*/10 * * * * php /var/www/check.php

What can be improved?

  • Statistics by day - how many came/left.
  • Linking to advertising - who came after the advertising integrations.
  • Growth charts - visualization of dynamics.
  • Notifications if there is a sharp spike in subscriptions or unsubscriptions.
  • Linking to posts increases after publication.

The result

Accounting for subscribers and unsubscribers of the Telegram channel through PHP MadelineProto is a simple and accurate way to get full-fledged analytics without third-party services. The solution is installed in a few minutes, is easily automated and works stably.

If you want to integrate this solution into your service, try write to me.

автор - Михаленко Р.
M R. Автор - kaktotak.by Specialization: financial and economic design - modeling of business, investment projects of the real sector, analysis and evaluation of efficiency, optimization of the management decision system.

A wide range of web-based competencies for solving business problems.
Новые решения на Laravel+Vue

Subscribe to my telegram channel - My notes are ECONOMICS +
Там я ничего не втюхиваю и не навязываю. Просто делюсь некоторой интересной информацией по разным экономическим аспектам. Может, будет интересно !

And also - WEB-заметки и идеи для бизнеса | php+js+seo

  Personal assistance in economic design and web development:

  • Financial and economic modeling, analysis, accounting, business planning
  • Comprehensive web development/project support on the web (php/js, seo – Laravel, WordPress, Vue, telegram, administration, content, advertising in Yandex Direct

  telegram или форма обратной связи

Administrator
69

Currency Converter
RUB RUB-icon
USD USD-icon
EUR EUR-icon
CNY CNY-icon
BYN BYN-icon
UAH UAH-icon
KZT KZT-icon
SHORT- what is it about
ECONOMIC LITERACY
  Simple online solutions to problems of economics and finance  
I Want To Know Everything. Useful Tips