dev-phpsocketraw
dev-phpsocketrawA Laravel Wrapper for telegram-cli
MPL-2.0
The Requires
- php >=5.4.0
- ext-curl *
- clue/socket-raw ^1.2
by Jonathan Williamson
laravel php-cli telegram
A Laravel Wrapper for telegram-cli
This project was originally forked from: php-telegram-cli-client and credit to zyberspace for the start I needed to get this project working., (*1)
TG
alias in the config/app.php
alias section.This project allows you to use the lovely Laravel syntax you are familar with to quickly and easily send messages / images / documents / audio files / location via Telegram Messenger with the aid of telegram-cli running on your server., (*2)
It allows you do things like:, (*3)
TG::sendMessage('User_name', 'Hi there your account has been set up! Thanks!');
or, (*4)
TG::sendDocument('User_name', '/home/data/files/important.pdf');
or, (*5)
TG::sendImage('User_name', 'http://upload.wikimedia.org/wikipedia/commons/1/16/HDRI_Sample_Scene_Balls_(JPEG-HDR).jpg');
Of course if you don't like using Facades you can always revert to, (*6)
// $tg is an instantiated TgCommands Object. Laravel will create this out of the IOC container for // you if typehinted in constructor etc. $tg->sendMessage('User_name', 'Hi there your account has been set up! Thanks!');
-S
) or a port (-P
). Needs to be configured already (phone-number, etc.).Telegram-cli needs to run on a unix-socket (-S
) or a port (-P
), so laragram can connect to it. Please read the instructions at telegram-cli on how to configure
and then make
, (*7)
If your build was successful you can continue!, (*8)
If you never started telegram-cli before, you need to start it first in normal mode, so you can type in your telegram-phone-number and register it., (*9)
Assuming you installed telegram in ~/telegram
your command should look like this:
~/telegram/bin/telegram-cli
., (*10)
Once registered, you can exit telegram-cli (safe_quit
) and launch it as a daemon., (*11)
To run telegram-cli as a daemon you need to use the -d
flag and set a unix socket -S
., (*12)
~/telegram/bin/telegram-cli -k ~/telegram/tg-server.pub -dWS /tmp/tg.sck --json
Or instead you may use a TCP port, (*13)
~/telegram/bin/telegram-cli -k ~/telegram/tg-server.pub -dWP 7777 --json
The -W
switch means the contact-list gets loaded on startup - this allows you to send messages straight away. The &
at the ends means that the command will load in the background allowing you to continue with the script., (*14)
To stop the daemon use killall telegram-cli
or kill -TERM [telegram-pid]
., (*15)
You now have Telegram-cli working and waiting to be told what to send!, (*16)
To ensure your daemon does not exit unexpectantly, you can use something like supervisor to make sure the process always is running. See notes at the end of how this can be set up., (*17)
In your laravel project-root:, (*18)
composer require williamson/laragram
Open config/app.php
file, find the providers array and add to the bottom, (*19)
'Williamson\Laragram\Laravel\LaragramServiceProvider'
If you're running Laravel 5.1 or greater you can use this syntax instead in the config/app.php
file:, (*20)
Williamson\Laragram\Laravel\LaragramServiceProvider::class
Note the lack of single quotes for the laravel 5 version., (*21)
Now you add the following line to the Alias array in config/app.php
to register the shorthand TG
facade, (*22)
'TG' => \Williamson\Laragram\Laravel\LaragramFacade::class,
Open config/services.php
file, add the following array to specify what type of socket you would like to connect to the
telegram daemon., (*23)
'telegram' => [ 'socket' =>'tcp://127.0.0.1:7778' ]
Other values include unix sockets. ie 'socket' => 'unix:///tmp/tg.sck'
, (*24)
You're done!, (*25)
Now anywhere in your app you can send a telegram message quickly and easily like follows:, (*26)
// routes.php Route::get('/test', function (){ TG::sendMsg('<name or telegram id number>', 'Hello there!'); });
Remember that the name should have underscores instead of spaces eg firstname_lastname
, OR you can use user#
and then the persons telegram ID directly. This method is far more reliable. eg user#123456789
, (*27)
In addition to sendMessage, the following commands are available to you:, (*28)
broadcastMsg chatAddUser chatCreateGroup chatCreateSecret chatDelUser chatExportLink chatInfo chatRename chatSetPhoto contactAdd contactDelete contactList contactRename deleteMsg exportCard getContactList getDialogList getHistory getUserInfo markRead msg sendAudio sendContact sendDocument sendLocation sendMsg sendPhoto sendText sendTypingStart sendTypingStop sendVideo setProfileName setProfilePhoto setStatusOffline setStatusOnline setUsername
This software is licensed under the Mozilla Public License v. 2.0. For more information, read the file LICENSE
., (*29)
Assuming that you have, (*30)
/home/username/telegram
Then create a new file /etc/supervisor/conf.d/telegram.conf
, (*31)
and copy the following into it, adding/replacing any log files you wish to create, (*32)
[program:telegram] command=/bin/bash -c "rm -f /tmp/tg.sck && /home/username/telegram/bin/telegram-cli -k /home/username/telegram/tg-server.pub -dWS /tmp/tg.sck --json" ;For tcp socket this could be ;command=/bin/bash -c "/home/username/telegram/bin/telegram-cli -k /home/username/telegram/tg-server.pub -dWP 7777 --json" directory=/home/username/telegram redirect_stderr=true stopsignal=KILL stopasgroup=true autostart=true autorestart=true startretries=3 user=<insert linux username> stdout_logfile=<insert your own log folder and log filename. Must exist> stderr_logfile=<insert your own log folder and log filename. Must exist> stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=1MB
Save the file and restart supervisor (make sure that telegram-cli is NOT running before you do this, supervisor will now take care of insuring that the process always runs even if it crashes)., (*33)
sudo service supervisor restart
You can view how the process is doing by using the monitoring program sudo supervisorctl
, (*34)
A Laravel Wrapper for telegram-cli
MPL-2.0
laravel php-cli telegram