调用的是itchat的包
import itchat
from itchat.content import TEXT
from itchat.content import *
import sys
import time
import re
import requests, json
import os
# When recieve the following msg types, trigger the auto replying.
@itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isMpChat=False)
def text_reply(msg):
global auto_reply, robort_reply, peer_list
if msg['FromUserName'] == myUserName and msg['Content'] == u"open":
auto_reply = True
itchat.send_msg(u"[自动回复]已经打开。\n", msg['FromUserName'])
elif msg['FromUserName'] == myUserName and msg['Content'] == u"close":
auto_reply = False
itchat.send_msg(u"[自动回复]已经关闭。\n", msg['FromUserName'])
# elif not msg['FromUserName'] == myUserName:
else:
if auto_reply == True:
itchat.send_msg(u"[自动回复]您好,我现在正在自习,一会再和您联系。\n", msg['FromUserName'])
return
@itchat.msg_register([FRIENDS])
def add_friend(msg):
#print(msg, flush=True)
global g_autoAddFriend
if g_autoAddFriend == True:
itchat.add_friend(**msg['Text'])
itchat.send_msg('你好!很高兴认识你!', msg['RecommendInfo']['UserName'])
logging("收到[%s]添加好友请求,已经自动通过!" %msg['RecommendInfo']['NickName'])
# Main
if __name__ == '__main__':
# Set the hot login
itchat.auto_login(enableCmdQR=2, hotReload=True)
# Get your own UserName
myUserName = itchat.get_friends(update=True)[0]["UserName"]
print(myUserName)
auto_reply = True
robort_reply = True
peer_list = []
itchat.run()
代码挺简单的,复制下来直接就能用。就是记得先:
pip install itchat
使用:
运行:
python autoreply.py #记得路径要对
代码一直在本地运行着,一不小心把cmd关掉怎么办,我要是出门电脑关了怎么办?晚上断电了怎么办?
因此我又想着把这个进程挂载到服务器上跑。
解决方法:
1 安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
2.下载python3.7.0源码
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
3.解压Python-3.7.0.tgz
tar -zxvf Python-3.7.0.tgz
4.执行配置文件,编译,编译安装
cd Python-3.7.0
./configure --prefix=/usr/local/python3
make && make install
5.建立链接
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3
注意事项:
python3 autoreply.py
pip3 install itchat
试图在终端里打开png,太复杂失败了,然后查阅了iitchat手册,发现参数改成2就好了。
解决办法:将enableCmdQR的参数改为2
参照阮一峰的守护进程
还有一些关于控制后台运行的指令:LINUX 杀死、暂停、继续、后台运行进程
解决方法: