In this topic, I want to describe an additional, and for some services, the main method of authorization and user registration through Telegram is possible, like OAuth authorization through social networks and web services.
Suppose you have a service that offers users an input through Telegram. On the login page, the user is prompted to enter his
Username in the Telegram. After entering the Username, a message comes to the user’s account with a confirmation code that he enters on the login page.
First of all, we need a “live” Telegram account, that is, an account that is registered on a mobile phone number. In this case,
bots are not suitable for this task, as bots cannot write messages first.
Now for the server side.
')
On
the Telegram
application page there is an
unofficial telegram-cli application . It will perform the function of the application that will interact with the Telegram server.
To simplify the example of telegram-cli, we will put in the / usr / local / src / directory:
cd /usr/local/src/
Clone repository:
git clone --recursive https://github.com/vysheng/tg.git && cd tg
Put the necessary packages:
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev python-dev
We collect telegram-cli:
./configure make
When you first start, the application will ask you to enter the phone number of our "live" account. After entering the number, a confirmation code will be sent by SMS or to the application if this account is currently active on another device. After confirmation, the directory ".telegram-cli" will be created in your home directory in which the configuration files for the specified account will be stored.
Run:
/usr/local/src/tg/bin/telegram-cli -k /usr/local/src/tg/tg-server.pub
See the following picture:

Exit the application:
quit
Run the application as a demon for example on port 8089:
/usr/local/src/tg/bin/telegram-cli --json -k /usr/local/src/tg/tg-server.pub -W -d -P 8089 &
Now we need a script that will interact with telegram-cli.
By keywords (telegram-cli php client) a ready-made PHP solution was found on GitHub.
Installation:
git clone
https://github.com/zyberspace/php-telegram-cli-clientcd php-telegram-cli-client
curl -sS
https://getcomposer.org/installer | php
php composer.phar install
Create a tg.php file with the following content:
<?php require('vendor/autoload.php'); $telegram = new \Zyberspace\Telegram\Cli\Client('tcp://localhost:8089'); $USERNAME = ''; if($user = $telegram->exec("resolve_username {$USERNAME}")){ if($user->type == 'user'){ $code = mt_rand(10000, 99999); $message = "Hi {$user->first_name}! Your code: {$code}"; if($telegram->exec("msg user#{$user->id} {$message}")){ echo "Send message: Success\r\n"; } else { echo "Send message: Error\r\n"; } } } else { echo "User {$USERNAME} not found.\r\n"; }
In the $ USERNAME variable, enter the username of the user to whom a message with a confirmation code will be sent.
Run the script:
php tg.php
If everything went well, a message with a code will be sent to the user.
Based on this example, you can easily implement user registration / authorization through Telegram.
Thanks for attention!