forked from Evensgn/itchat-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_autoreply.py
132 lines (126 loc) · 4.09 KB
/
wechat_autoreply.py
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()