Telegram

From Organic Design wiki
Revision as of 01:47, 1 April 2020 by Nad (talk | contribs) (telegram-cli)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Cone.png This article or section is a stub. Stubs are articles that have not yet received substantial attention from the authors. They are short or insufficient pieces of information and require additions to further increase the article's usefulness. The project values stubs as useful first steps toward complete articles.


telegram-cli

I was interested in experimenting with telegram-cli, the command-line Telegram interface, and thought I better add a few notes.

First I installed it with snap, but later found that there's actually a deb available, and some distros have telegram-cli available natively via apt which may be easier.

apt install snapd
snap install telegram cli

Then you simply run it with no parameters, and it will ask you for your account phone number. After you enter it you'll receive a code over SMS and in your other Telegram if it's open, enter the code and you're all ready to go. The full list of commands are available from the help command within the utility.

You can then run telegram commands in a one-off way from scripts via shell e.g.

telegram-cli -W -e "msg Henry_McFoobas Hi there what's up?!"

To send a multi-line message from Perl you need to escape the newlines and surround the message in double-escaped quotes, for example here $msg is multi-line:

$msg =~ s/\n/\\n/g;
qx( telegram-cli -W -e "msg Henry_McFoobas \\"$msg\\"" );

But still the messages can't contain dollar-signs or double quotes, here's a Perl sub-routine to handle these symbols, it takes the first part of the command such as msg Henry_McFoobas in the first parameter, and the textual message part of the command (if any) in a second parameter:

sub telegram {
	my( $cmd, $msg ) = @_;
	if( $msg ) {
		$msg =~ s/"/\\\\\\"/g;
		$cmd .= " \\\"$msg\\\"";
	}
	$cmd =~ s/\n/\\n/g;
	$cmd =~ s/\$/\\\$/g;
	qx( telegram-cli -W -e "$cmd" );
}

If you installed it from snap then the downloads dir for sending files will be somewhere like ~/snap/telegram-cli/SOME_NUMBER/.telegram-cli/downloads, you need to specify the full path when using it from shell, e.g.

telegram-cli -W -e "send_video Henry_McFoobas /home/fred/snap/telegram-cli/25/.telegram-cli/downloads/MyVideo.mp4"

See also