微信自动回复的实现

微信自动回复的实现

    • 自动回复实现
    • 在服务器上运行
      • 第一个问题:python版本太旧需要更新
      • 第二个问题:在linux中二维码显示不完整
      • 第三个问题:断开ssh后进程也关了

最近考试比较多,还有各种事情蜂拥而至,微信上消息不断,就想着搞一个微信自动回复出来,要求也不高,只是能自动回复就好了。

自动回复实现

调用的是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

使用

  1. 给自己发open就开启自动回复了
  2. 发close关闭自动回复
  3. 有一个自动添加好友的功能
  4. 另外我把收到公众号之后也自动回复关掉了,不然自动回复回复自动回复不敢想象。(别问我怎么知道)

运行:

python autoreply.py   #记得路径要对

在服务器上运行

代码一直在本地运行着,一不小心把cmd关掉怎么办,我要是出门电脑关了怎么办?晚上断电了怎么办?
因此我又想着把这个进程挂载到服务器上跑。

第一个问题:python版本太旧需要更新

解决方法:

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

注意事项

  1. 可能由于openssl版本过久导致import ssl失败(即pip install ***时失败)需要更新openssl以及重新编译安装python
  2. python2和python3同时存在,调用python3时命令例如:
python3 autoreply.py 
pip3 install itchat

第二个问题:在linux中二维码显示不完整

试图在终端里打开png,太复杂失败了,然后查阅了iitchat手册,发现参数改成2就好了。
解决办法:将enableCmdQR的参数改为2

第三个问题:断开ssh后进程也关了

参照阮一峰的守护进程
还有一些关于控制后台运行的指令:LINUX 杀死、暂停、继续、后台运行进程
解决方法:

  1. 运行进程:python3 autoreply.py &;//最后的符号表示在后台运行
  2. ctrl+c暂停;
  3. bg 1恢复在后台运行;
  4. disown -h //不移出后台任务,但是让它们不会收到SIGHUP信号

你可能感兴趣的:(生活)