Skip to content

Instantly share code, notes, and snippets.

@gnumoksha
Last active December 5, 2021 14:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gnumoksha/a95f237d82733ce1f748 to your computer and use it in GitHub Desktop.
Save gnumoksha/a95f237d82733ce1f748 to your computer and use it in GitHub Desktop.
Telegram-cli example usage

Install the required packages

apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev make git -y

Compile

cd /usr/src
git clone --recursive https://github.com/vysheng/tg.git
cd tg
# The latest build seems broken
#git checkout -b tgviacron 01f5d9a3b671edc15a5cff285601115845489985
#git submodule update --init --recursive
./configure
make
mkdir viacron
cp bin/telegram-cli viacron/
cp tg-server.pub viacron/
cd viacron

Create config file /usr/src/tg/viacron/telegram.config

default_profile = "viacron";

viacron = {
    config_directory = "/usr/src/tg/viacron/";
};

Authorize:

./telegram-cli -k tg-server.pub -c telegram.config

Get the id

This guide send a message for user or group id because this decrease the errors.

for user

user_info User_Name_In_Contact_list
User User Name updated photo
User User Name updated name
User User Name @userFoo (#1234567):
        real name: User Name
        phone: 551122223333
        online (was online [2015/05/03 07:16:54])

1234567 in this case

for group

> chat_info Enterprise_Name_In_Contact_List
Chat Enterprise 123 updated admin
Chat Enterprise 123 updated members
Chat Enterprise 123 (id 1234567) members:
                Usuario Tal invited by Tobias Sette at [2015/05/14 02:48:45]
                Tobias Sette invited by Tobias Sette at [2015/05/14 02:48:45] admin
User Tobias Sette offline (was online [2015/05/14 02:53:59])

1234567 in this case

Create /usr/src/tg/viacron/telegram_standalone.sh

#!/bin/bash
# See
# https://github.com/gnumoksha/zabbix_utils/blob/master/gnu%2Blinux/alertscripts/

MAIN_DIRECTORY="/usr/src/tg/viacron/"
USER=$1
SUBJECT=$2
TEXT=$3

cd $MAIN_DIRECTORY
if [[ $? -ne 0 ]]; then
        echo "Error to enter in the main directory"
        exit 1
fi

./telegram-cli -k tg-server.pub -c telegram.config -WR -e "msg $USER $SUBJECT" || exit 1

exit 0

Note: is better to use parameter -U yourSystemUser in telegram-cli. Reduces the possibilities of error.

Change permissions:

chmod +x /usr/src/tg/viacron/telegram_standalone.sh
chown -R yourUser: /usr/src/tg/

Command line usage:

Use user# prefix for users, for groups use chat#.

/usr/src/tg/viacron/telegram_standalone.sh user#12345 "GNU is not unix"
@LukeLR
Copy link

LukeLR commented May 20, 2015

I don't think that the latets Build is broken, I rather guess that it didn't work because of the clone: when cloning the tg-repository, you need to pass git the --recursive flag, because it contains a linked repository (the tg-library). Probably that should work.

@gnumoksha
Copy link
Author

Thanks for advice, but the process was "workaround" because the vysheng/tg#536

@behnamazimi
Copy link

Hi,
I'm new in Telegram api and cli, I want to learn that so I will be happy if you explain more about this example.
For example, that is somehow complicated to understand User User Name updated photo!
Bests

@ngopher
Copy link

ngopher commented Aug 27, 2017

Hi gnumoshka, are you ok?
Sorry, I have a question about some tasks in telegram-cli.
I develop with Golang, using mtproto framework. I can not implement method for add new contact :((
can you help me to Solve this problem? The language is not matter, I can Convert between languages myself.
Please Help me @gnumoshka

@antonrud
Copy link

antonrud commented Apr 6, 2018

@LukeLR, thx for advice. It worked!
(also pls note: "With version 2.13 of Git and later, --recursive has been deprecated and --recurse-submodules should be used instead")

Copy link

ghost commented Apr 17, 2018

Hi there.
It seems that there is a snappy package for tg-cli now, so there is no need to clone and compile repo, Tadaaaa! :D
https://www.omgubuntu.co.uk/2016/10/use-telegram-cli-in-terminal-ubuntu

@Madhava-mng
Copy link

hellow , how can I change the prompt interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment