声明:本文转载自 Evensgn’s Station,转载已经获得 Evensgn 本人许可。
QQ 有一个简单的自动回复功能,在账号状态为“离开/忙碌/请勿打扰”时若收到好友的消息,可以自动回复一句用户设置的内容,常见的比如“[自动回复] 您好,我现在有事不在,请稍后再联系。”
目前,微信的个人账号是没有自动回复功能的,刚好寒假里自己看了一点点 Python 的基础,又发现了一个微信的 Python 接口 itchat,便想到写一个 Python 小程序来自己实现微信个人账号的自动回复。
使用此程序前首先需要安装 itchat,可以使用以下命令安装:
1 |
pip install itchat |
简单介绍一下itchat,这是@LittleCoder开源的WeChat的Python接口,模拟了微信PC客户端的各种行为,提供了功能较为全面的API,可以用于对个人微信账号进行有趣的功能扩展。
点击此链接查看itchat的官方文档。感谢@LittleCoder。
有了好用的接口 itchat,写这个小程序便很简单了,一共写了一百多行代码(完整代码在文章最后,也可以在Github查看),实现了自己设想的几点功能:
- 对于特定的好友分别设置不同的自动回复内容
- 可将自动回复的内容列表保存到文件,也可从文件导入自动回复内容列表
- 能够使用微信发送命令进行方便的控制
将此程序在服务器上运行,就可以实现 24 小时的自动回复了。
控制命令
我选择的控制方式是向账号的文件传输助手发送控制命令,这样可以在使用的过程中随时发送命令对自动回复进行方便的修改与控制,并将得到程序发送的反馈信息。
控制命令的列表如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
/help Show this table /autoreply off Turn off auto-reply /autoreply on Turn on auto-reply /autodict reset Reset auto-reply dictionary /autodict show Show auto-reply dictionary /autodict add [A] [B] Add an auto-reply item for [A] as [B] /autodict del [A] Delete the auto-reply item for [A] /autodict load [file] Load auto-reply dictionary from [file] /autodict save [file] Save auto-reply dictionary to [file] /autoprefix set [A] Set auto-reply prefix as [A] /autoprefix off Hide auto-reply prefix /autoprefix on Show auto-reply prefix |
初始设置
在下面这段代码中进行初始设置:
1 2 3 4 5 6 7 8 9 |
# default settings autoReply = True showAutoPrefix = True # default auto-reply prefix autoPrefix = '[Auto Reply] ' # autoReply: 是否进行自动回复 # showAutoPrefix: 是否在自动回复消息前添加前缀 # autoPrefix: 自动回复的前缀 |
编辑自动回复列表存储文件
在需要批量添加自动回复或回复内容较长时,使用控制命令逐条添加较为繁琐,可以直接将自动回复列表保存到一个文件中,再使用‘/autodict load [file]’命令从文件导入自动回复列表,我设定的在文件中存储自动回复列表的格式如下:
1 2 3 |
<item><name>好友A备注</name><text>对A的自动回复内容</text></item> <item><name>好友B备注</name><text>对B的自动回复内容</text></item> <item><name>好友C备注</name><text>对C的自动回复内容</text></item> |
完整代码
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#!/usr/bin/env python #-*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import requests, itchat from itchat.content import * import re # default settings autoReply = True showAutoPrefix = True # default auto-reply prefix autoPrefix = '[Auto Reply] ' autoDict = {} def underlineToSpace(s): rs = '' for c in s: if c == '_': rs += ' ' else: rs += c return rs @itchat.msg_register([TEXT, PICTURE, RECORDING], isGroupChat = False) def reply(msg): global autoReply global showAutoPrefix global autoPrefix global autoDict # react to message from yourself if msg['ToUserName'] == 'filehelper': arg = re.compile(' ').split(msg['Text']) nonSense = False try: if arg[0] == '/help': reply = '''[Help Information] /help Show this table /autoreply off Turn off auto-reply /autoreply on Turn on auto-reply /autodict reset Reset auto-reply dictionary /autodict show Show auto-reply dictionary /autodict add [A] [B] Add an auto-reply item for [A] as [B] /autodict del [A] Delete the auto-reply item for [A] /autodict load [file] Load auto-reply dictionary from [file] /autodict save [file] Save auto-reply dictionary to [file] /autoprefix set [A] Set auto-reply prefix as [A] /autoprefix off Hide auto-reply prefix /autoprefix on Show auto-reply prefix ''' elif arg[0] == '/autoreply': if arg[1] == 'off': autoReply = False reply = 'Turn off auto-reply.' elif arg[1] == 'on': autoReply = True reply = 'Turn on auto-reply.' else: nonSense = True elif arg[0] == '/autodict': if arg[1] == 'reset': autoDict = {} reply = 'Reset auto-reply dictionary.' elif arg[1] == 'show': reply = '[Auto-reply Dictionary]' for k in autoDict: reply += '\n[' + k + '] ' + autoDict[k] elif arg[1] == 'add': autoDict[arg[2]] = underlineToSpace(arg[3]) reply = 'Add an auto-reply item for \'' + arg[2] + '\'.' elif arg[1] == 'del': autoDict.pop(arg[2]) reply = 'Delete the auto-reply item for \'' + arg[2] + '\'.' elif arg[1] == 'load': fileName = arg[2] with open(fileName, 'r') as inFile: allText = inFile.read() pattern = re.compile('<item><name>(.*?)</name><text>(.*?)</text></item>', re.S) items = re.findall(pattern, allText) autoDict = {} for item in items: autoDict[item[0]] = item[1] reply = 'Load auto-reply dictionary from file \'' + fileName + '\'.' elif arg[1] == 'save': fileName = arg[2] allText = '' for k in autoDict: allText += '<item><name>' + k + '</name><text>' + autoDict[k] + '</text></item>\n' with open(fileName, 'w') as outFile: outFile.write(allText) reply = 'Save auto-reply dictionary to file \'' + fileName + '\'.' else: nonSense = True elif arg[0] == '/autoprefix': if arg[1] == 'set': autoPrefix = underlineToSpace(arg[2]) reply = 'Set auto-reply prefix as \'' + autoPrefix + '\'.' elif arg[1] == 'off': showAutoPrefix = False reply = 'Hide auto-reply prefix \'' + autoPrefix + '\'.' elif arg[1] == 'on': showAutoPrefix = True reply = 'Show auto-reply prefix \'' + autoPrefix + '\'.' else: nonSense = True except: nonSense = True if nonSense: reply = 'Use /help to view help information.' itchat.send(reply, toUserName = 'filehelper') # if message is from other accounts else: friend = itchat.search_friends(userName = msg['FromUserName']) if friend: remarkName = friend['RemarkName'] # if cannot find this friend else: remarkName = 'RemarkName Error' reply = '' # auto-reply if autoReply: if remarkName in autoDict: if showAutoPrefix: reply = autoPrefix reply += autoDict[remarkName] itchat.send(reply, msg['FromUserName']) itchat.auto_login(enableCmdQR = True) itchat.run() |
-
Google Chrome 52
Windows
-
Opera 12
Windows 8
Thanks in support of sharing such a fastidious opinion, piece of writing is nice,
thats why i have read it completely
厉害了我的哥,你准备学这个??