死肥宅也要谈恋爱之早安晚安自动化

其实是看到了Diygay的技术宅也要谈恋爱之早安晚安自动化 ,但当时有方糖可以作为系统推送,实现起来很方便也就没有去改变。

但是呢,偶然间发现并稍微了解Telegram-cli如此强大的应用,简直让人无法割舍了! t_logo.png 相信知道Telegram的已经由EH Forwarder Bot将微信的消息转发到Telegram了吧。

Telegram-cli就是Telegram的一个命令行工具了,有了它,不仅可以给女朋友发送早安晚安,或者给你的“女朋友们”发送。 甚至你也可以给你的基友群发送早安晚安了!

是不是很厉害~

那么,我们开始做吧!!

安装

我们先安装Telegram-cli,官方的README已经非常详尽,这边以Ubuntu 18.04.1 LTS为例进行安装。

  • 拉取代码
1
2
cd /usr/local/src
git clone --recursive https://github.com/vysheng/tg.git && cd tg
  • 安装依赖
1
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make 
  • 配置、编译
1
2
./configure
make

当然了,在这一步可能会遇到问题。我就遇到了以下的问题:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
tgl/crypto/rsa_pem_openssl.c: In function ‘TGLC_rsa_new’:
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 ());
      ^~
tgl/crypto/rsa_pem_openssl.c: In function ‘TGLC_rsa_n’:
tgl/crypto/rsa_pem_openssl.c:52:1: error: control reaches end of non-void function [-Werror=return-type]
 RSA_GETTER(n);
 ^~~~~~~~~~
tgl/crypto/rsa_pem_openssl.c: In function ‘TGLC_rsa_e’:
tgl/crypto/rsa_pem_openssl.c:53:1: error: control reaches end of non-void function [-Werror=return-type]
 RSA_GETTER(e);
 ^~~~~~~~~~
cc1: all warnings being treated as errors
Makefile.tgl:20: recipe for target 'objs/crypto/rsa_pem_openssl.o' failed
make: *** [objs/crypto/rsa_pem_openssl.o] Error 1

看上起像是openssl的问题,经过一番搜索找到了问题所在:

OpenSSL 1.1.x对一些API做了改动

因为Ubuntu 18.04通过包管理器安装的默认就是1.1.x版本了。

而后在issues里面找到了解决方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
diff --git a/crypto/rsa_pem_openssl.c b/crypto/rsa_pem_openssl.c
index db653f2..49a9e3f 100644
--- a/crypto/rsa_pem_openssl.c
+++ b/crypto/rsa_pem_openssl.c
@@ -36,18 +36,28 @@ TGLC_WRAPPER_ASSOC(rsa,RSA)
 // TODO: Refactor crucial struct-identity into its own header.
 TGLC_WRAPPER_ASSOC(bn,BIGNUM)
 
+/*
+ * Note: Since OpenSSL version 1.1.0-pre5 the RSA struct (rsa_st) is opaque,
+ * see https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
+ */
 TGLC_rsa *TGLC_rsa_new (unsigned long e, int n_bytes, const unsigned char *n) {
   RSA *ret = RSA_new ();
-  ret->e = unwrap_bn (TGLC_bn_new ());
-  TGLC_bn_set_word (wrap_bn (ret->e), e);
-  ret->n = unwrap_bn (TGLC_bn_bin2bn (n, n_bytes, NULL));
+  BIGNUM *ret_e = unwrap_bn (TGLC_bn_new ());
+  BIGNUM *ret_n = unwrap_bn (TGLC_bn_bin2bn (n, n_bytes, NULL));
+  RSA_set0_key (ret, ret_n, ret_e, NULL);
+  TGLC_bn_set_word (wrap_bn (ret_e), e);
   return wrap_rsa (ret);
 }
 
-#define RSA_GETTER(M)                                                          \
-  TGLC_bn *TGLC_rsa_ ## M (TGLC_rsa *key) {                                    \
-    return wrap_bn (unwrap_rsa (key)->M);                                      \
-  }                                                                            \
+#define RSA_GETTER(M)                       \
+TGLC_bn *TGLC_rsa_ ## M (TGLC_rsa *key) {   \
+    BIGNUM *rsa_n, *rsa_e, *rsa_d;          \
+    RSA_get0_key(unwrap_rsa (key),          \
+        (const BIGNUM **) &rsa_n,           \
+        (const BIGNUM **) &rsa_e,           \
+        (const BIGNUM **) &rsa_d);          \
+    return wrap_bn (rsa_ ## M);             \
+}
 
 RSA_GETTER(n);
 RSA_GETTER(e);

当编译完成后,会在当前的目录下生产 bin 目录,里面就有我们要的二进制文件了。

使用

  • 配置、运行

可以直接使用bin/telegram-cli -k server.pub运行,server.pub是一个证书文件,如果不能使用了,你需要到https://my.telegram.org/apps 创建一个应用获取。

运行后被要求输入登录的手机号以及验证码,手机号以中国号码为例,需要添加区号:+8613800138000 收到验证码验证后就可以成功登录了。

  • 基本操作

登录后,我们就可以开始做一些骚操作了。

user_info <user> 打印用户信息 (id, last online, phone)

contact_list - 获取telegram联系人列表

dialog_list - 获取telegram最近的聊天对象

msg <peer> 要发送的消息 - 向群组发起消息

msg <peer> 要发送的消息 - 向联系人发起消息

聊天:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
> chat_with_peer <peer>
Kay_Lee > 1
 [12:35]  Kay Lee <<< 1
Kay_Lee > 2
 [12:35]  Kay Lee <<< 2
User Kay Lee online (was online [2018/11/19 12:40:11])
Kay_Lee > 3
 [12:35]  Kay Lee <<< 3
Kay_Lee > 4
 [12:35]  Kay Lee <<< 4
Kay_Lee > /quit
User Kay Lee offline (was online [2018/11/19 12:35:24])

history <peer> - 查看历史记录

send_photo <peer> <photo-file-name> - 将照片发送给对等方

send_video <peer> <video-file-name> - 将视频发送给对等体

send_text <peer> <text-file-name> - 将文本文件作为纯文本消息发送

send_document <peer> <path_to_file> - 将任意文档发送给对等方

其中<peer>可以表示: a user(以@Username表示) a chat(以群组名表示) a secret chat (!_ prefix)

更多的操作参见官方wiki

  • 自动化操作

通过了解以上的简单操作,我们便可以完成Telegram-cli自动化的发送了

命令是:

1
$telegram-cli_PATH/bin/telegram-cli -W -e "send_photo 文件传输助手 '/home/kay/Desktop/20181117190534.png' "

这个例子是将桌面的图片发送到微信的文件传输助手。

当然,可以通过 /link 微信群名到一个群组实现发送消息到群。

你还可以在某些tg频道、微信公众号定时完成签到。

自己去玩吧~

守护程序

这里使用的是systemd,按照官方操作如下:

创建文件

1
vim /lib/systemd/system/telegram.service

内容如下:

1
2
3
4
5
6
7
8
[Unit]
Description=Telegram-cli systemd service for username1

[Service]
ExecStart=/usr/local/src/tg/bin/telegram-cli -d -vvvv -P 2391 -L /run/log/telegram-cli.log

[Install]
WantedBy=default.target

开机启动以及开启进程

1
2
systemctl enable telegram
systemctl start telegram

由此开启了一个监听端口2391,配合nc也可以达到目的

1
echo "msg Girlfriend 'Lets go out?' " | nc -w1 localhost 2391
点击刷新🚌