Search Suggest

Telegram CLI on Raspberry PI





1.Giới thiệu 
Telegram CLI là một chương trình telegram client giống như telegram app trên điện thoại hoặc máy tính vậy, chỉ khác một điều là chương trình này chạy trên giao diện dòng lệnh (command line) chứ nó không có giao diện đồ họa như telegram app thông thường.

Source code và các hướng dẫn xem tại trang chủ:
  https://github.com/vysheng/tg

2.Cài đặt trên raspberry pi 
Việc build telegram cli trên máy tính không gặp vẫn đề gì, tuy nhiên khi build và chạy trên raspberry pi sẽ có một vài lỗi vì thế cần chỉnh sửa nhỏ.

Note:
- Bài viết này sử dụng hệ điều hành raspbian buster lite (2019-09-26-raspbian-buster-lite.img)
- Việc build telegram trên raspberry pi thật sẽ rất chậm nên bài viết này sử dụng QEMU cho nhanh


(1) git clone  https://github.com/vysheng/tg.git && cd tg
(2) Comment out  hoặc delete 2 dòng 101 and 115 ở file tgl/mtproto-utils.c

//assert (0);  <= comment out or delete
static unsigned long long BN2ull (TGLC_bn *b) {
if (sizeof (unsigned long) == 8) {
return TGLC_bn_get_word (b);
} else if (sizeof (unsigned long long) == 8) {
//assert (0); // As long as nobody ever uses this code, assume it is broken. <<== comment out
unsigned long long tmp;
/* Here be dragons, but it should be okay due to be64toh */
TGLC_bn_bn2bin (b, (unsigned char *) &tmp);
return be64toh (tmp);
} else {
assert (0);
}
}

static void ull2BN (TGLC_bn *b, unsigned long long val) {
if (sizeof (unsigned long) == 8 || val < (1ll << 32)) {
TGLC_bn_set_word (b, val);
} else if (sizeof (unsigned long long) == 8) {
//assert (0); // As long as nobody ever uses this code, assume it is broken. <<== comment out
htobe64(val);
/* Here be dragons, but it should be okay due to htobe64 */
TGLC_bn_bin2bn ((unsigned char *) &val, 8, b);
} else {
assert (0);
}
}

(3) sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make
(4) sudo apt install libssl1.0-dev
(5) ./configure
(6) nano Makefile (deleting -Werror)
Xóa -Werror trong config dưới đây

COMPILE_FLAGS=${CFLAGS} ${CPFLAGS} ${CPPFLAGS} ${DEFS} -Wall -Werror -Wextra -Wno-missing-field-initializers -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC

(7) make


Note:
- (2) Fix run bug
$ bin/telegram-cli -k tg-server.pub
Telegram-cli version 1.4.1, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 2.1.0
Telegram-cli includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit. (http://www.openssl.org/)
I: config dir=[/home/user/.telegram-cli]
[/home/user/.telegram-cli] created
[/home/user/.telegram-cli/downloads] created
> SIGNAL received
No libexec. Backtrace disabled
assertion "0" failed: file "tgl/mtproto-utils.c", line 101, function: BN2ull

- (4) Fix build bug
tgl/crypto/rsa_pem_openssl.c:41:6: error: dereferencing pointer to incomplete type ‘RSA’ {aka ‘struct rsa_st’}
   ret->e = unwrap_bn (TGLC_bn_new ());

- (6) Fix build bug
tgl/tl-parser/tl-parser.c:1907:26: error: ‘sprintf’ may write a terminating nul past the end of the destination [-Werror=format-overflow=]
sprintf (s, "%lld", lrand48 () * (1ll << 32) + lrand48 ());


3.Chạy ứng dụng
Sau khi make xong, chương trình đã được để trong thư mục bin

pi@raspberrypi:~/tg $ bin/telegram-cli --help
telegram-cli Usage
  --phone/-u                           specify username (would not be asked during authorization)
  --rsa-key/-k                         specify location of public key (possible multiple entries)
  --verbosity/-v                       increase verbosity (0-ERROR 1-WARNIN 2-NOTICE 3+-DEBUG-levels)
  --enable-msg-id/-N                   message num mode
  --config/-c                          config file name
  --profile/-p                         use specified profile
  --log-level/-l                       log level
  --sync-from-start/-f                 during authorization fetch all messages since registration
  --disable-auto-accept/-E             disable auto accept of encrypted chats
  --lua-script/-s                      lua script file
  --wait-dialog-list/-W                send dialog_list query and wait for answer before reading input
  --disable-colors/-C                  disable color output
  --disable-readline/-R                disable readline
  --alert/-A                           enable bell notifications
  --daemonize/-d                       daemon mode
  --logname/-L <log-name>              log file name
  --username/-U <user-name>            change uid after start
  --groupname/-G <group-name>          change gid after start
  --disable-output/-D                  disable output
  --tcp-port/-P <port>                 port to listen for input commands
  --udp-socket/-S <socket-name>        unix socket to create
  --exec/-e <commands>                 make commands end exit
  --disable-names/-I                   use user and chat IDs in updates instead of names
  --enable-ipv6/-6                     use ipv6 (may be unstable)
  --help/-h                            prints this help
  --accept-any-tcp                     accepts tcp connections from any src (only loopback by default)
  --disable-link-preview               disables server-side previews to links
  --bot/-b                             bot mode
  --json                               prints answers and values in json format
  --permanent-msg-ids                  use permanent msg ids
  --permanent-peer-ids                 use permanent peer ids



Chạy thử chương trình:
pi@raspberrypi:~/tg $ ./bin/telegram-cli -k tg-server.pub

Sau đó nhập phone number và mã code OTP mà telegram gửi về cho bạn



4. Python wraps around telegram cli 
Telegram CLI chỉ là một chương trình cơ bản nhất chạy bằng command line, vì thế trong thực tế cần có một chương trình wrapper để sử dụng telegram cli từ một chương trình khác.
Ví dụ trong bài viết này đang muốn làm một giao diện chat cho telegram bằng python chẳng hạn, thì khi này cần một wrapper bằng python.

Bài viết này giới thiệu một wrapper được làm sẵn, cài đặt và sử dụng xem tại trang chủ dưới đây:
https://github.com/luckydonald/pytg














Đăng nhận xét