未分类

Itchat自动回复与数据库保存

使用ItChat库微信消息自动回复/pymysql保存数据库

前提

注意登录网页微信只有老用户可以,新申请的微信号无法登录会出现以下提示:

1
2
3
4
<error><ret>1203</ret><message>为了你的帐号安全,此微信号已不允许登录网页微信。
你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com
Mac微信下载地址:https://mac.weixin.qq.com</message></error>

全局数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 自己id,用于判断是否是自己发送的消息
my_UserName = "my_UserName"
# 关键词恢复字典
message_dict = {
"你最帅": "回复消息1",
"祝福": "回复消息2",
"佩奇": "回复消息3",
}
# 记录消息数,用于首次回复,第二次不再回复
message_record = {
"userName": 1
}
# 需要回复消息的微信群名称
reply_group = {1: "消息测试", 2: "疯狂找媳妇儿", 3: "不准时下班小组"}

登录查找好友保存数据库

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
def after_login():
print("您已登录")
# 连接数据库
db = pymysql.connect(host='localhost',
user='root',
password='lonbon',
db='wechat',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor() # 使用cursor()方法获取操作游标
friends = itchat.get_friends(update=True)[0:] # 获取好友信息
print("好友:" + str(friends))
for friend in friends:
#判断好友是否已经存在在数据库
sql_select = "SELECT nickname,remarkname FROM contacts WHERE nickname =%s"
try:
cursor.execute(sql_select, (friend['NickName'])) # 查询好友昵称在数据库是否存在
result = cursor.fetchall()
db.commit()
if (len(result) > 0):
sql = "UPDATE contacts SET username =%s,time=%s WHERE nickname = %s"
try:
cursor.execute(sql, (friend['UserName'], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
friend['NickName'])) # 更新好友id
db.commit() # 提交到数据库执行
except Exception as e:
# db.rollback()# 发生错误时回滚
print("sql_select_Error", e)
else:
sql_insert = """INSERT INTO contacts (nickname,username, remarkname
, signature, province,city,membercount,sex
,attrstatus,snsflag,contactflag,headimgurl,time) VALUES ('%s','%s'
,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')""" % (
str(friend['NickName']), str(friend['UserName']), str(friend['RemarkName'])
, str(friend['Signature']), str(friend['Province']), str(friend['City']), str(friend['MemberCount'])
, str(friend['Sex']), str(friend['AttrStatus']), str(friend['SnsFlag']), str(friend['ContactFlag'])
, str(friend['HeadImgUrl']), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

cursor.execute(sql_insert)
db.commit()

except Exception as e:
# db.rollback() # 发生错误时回滚
print("sql_insert_Error", e)
# finally:
# 关闭数据库连接
# cursor.close()
# db.close()

# 获得完整的群聊列表
groups = itchat.get_chatrooms()
print("获得完整的群聊列表:" + str(groups))
for item in groups:
sql_select_group = "SELECT nickname,remarkname FROM groups WHERE nickname =%s"
try:
cursor.execute(sql_select_group, (item['NickName'])) # 查询群昵称在数据库是否存在
result = cursor.fetchall()
db.commit()
if (len(result) > 0):
print("已存在群:" + item['NickName'])
sql_update_group = "UPDATE groups SET username =%s,time=%s WHERE nickname = %s" # 更新群的id
try:
cursor.execute(sql_update_group, (
item['UserName'], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), item['NickName']))
db.commit()
except Exception as e:
# 发生错误时回滚
# db.rollback()
print("发生了错误")
print("Error", e)
else:
sql_insert_group = """INSERT INTO groups (nickname,username, remarkname
, signature, province,city,membercount,sex
,attrstatus,snsflag,contactflag,headimgurl,time) VALUES ('%s','%s'
,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')""" % (
str(item['NickName']), str(item['UserName']), str(item['RemarkName'])
, str(item['Signature']), str(item['Province']), str(item['City']), str(item['MemberCount'])
, str(item['Sex']), str(item['AttrStatus']), str(item['SnsFlag']), str(item['ContactFlag'])
, str(item['HeadImgUrl']), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

cursor.execute(sql_insert_group)
db.commit()

except Exception as e:
# db.rollback() # 发生错误时回滚
print("sql_select_group_Error", e)
# finally:
# 关闭数据库连接
cursor.close()
db.close()

退出登录

1
2
3
4
#退出登录
def after_logout():
print("已退出")

监听好友消息并回复

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
# @itchat.msg_register(itchat.content.MAP)  # 位置文本
# @itchat.msg_register(itchat.content.NOTE) # 通知文本
# @itchat.msg_register(itchat.content.PICTURE) # 监听图片/表情
# @itchat.msg_register(itchat.content.FRIENDS) # 好友邀请
# @itchat.msg_register(itchat.content.VIDEO) # 监听视频
@itchat.msg_register(itchat.content.TEXT) # 监听文本内容
def print_content(msg):
print("msg是:" + str(msg))
NickName = msg['User']['NickName'] # 对方名字
FromUserName = msg['FromUserName'] # 发送人的id
user = itchat.search_friends(name=NickName)[0] # 获取特定UserName的用户信息
text = msg['Text'] # 发送的文字消息
print("my_UserName:" + my_UserName)
if FromUserName != my_UserName:
hasKey = False
theKey = ""
for key in message_dict.keys():
if (key in text):
hasKey = True
theKey = key
if (hasKey):
print("找到关键字:" + str(theKey))
user.send("检测到关键字:" + str(theKey)) # message_dict[theKey]
# user.send_image("./image/209717696.jpg") # 发送图片
# user.send_file("cat.py") # 发送文件
# user.send_file("cat.mp4") # 发送视频
else:
print("字典里没有关键字")
if (NickName in message_record.keys()):
print("已经发送过了")
message_record.update({str(NickName): 2})
else:
message_record.update({str(NickName): 1})
user.send(u"亲爱的 %s,你好啊" % NickName)
user.send_image("./image/2.jpg")
else:
print("yourself")

监听群消息并回复

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
@itchat.msg_register(itchat.content.TEXT, isGroupChat=True)
def reply_msg(msg):
# print("收到一条群信息:", msg['ActualNickName'], msg['Content'])
print("收到一条群信息:", str(msg))
# chat_rooms = itchat.search_chatrooms(name='消息测试')
# if len(chat_rooms) > 0:
# itchat.send_msg('测试:这是一条自动回复消息,您发的消息是:%s' % msg['Content'], chat_rooms[0]['UserName'])

from_user = msg['FromUserName'] # 来自谁的消息(自己发的是自己的id,其他人发的是群id)
content = msg['Content']
print(content)
db_conn = pymysql.connect(host='localhost',
user='root',
password='lonbon',
db='wechat',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

cursor = db_conn.cursor()
sql_select = "SELECT nickname,remarkname FROM groups WHERE username =%s"
try:
cursor.execute(sql_select, (from_user)) # 查询群昵称在数据库是否存在
result = cursor.fetchall()
db_conn.commit()
if (len(result) > 0):
print("已存在:" + str(result))
groups_name = str(result[0]['nickname'])
print("已存在:" + groups_name) # 群名称
if (groups_name in reply_group.values()):
# chat_rooms = itchat.search_chatrooms(name=groups_name)
# if (len(chat_rooms) > 0):
# itchat.send_msg('测试:这是一条自动回复消息,群昵称:%s,消息是:%s' % (content, groups_name), chat_rooms[0]['UserName'])
# else:
# print("chat_rooms_len==0")
try:
itchat.send_msg('测试:这是一条自动回复消息,群昵称:%s,消息是:%s' % (content, groups_name), from_user)
except:
print("发送失败")
else:
print("不在自动恢复列表里")

else:
print("未找到群")
# sql_insert_group = """INSERT INTO groups (nickname,username, remarkname
# , signature, province,city,membercount,sex
# ,attrstatus,snsflag,contactflag,headimgurl,time) VALUES ('%s','%s'
# ,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')""" % (
# str(msg['NickName']), str(msg['UserName']), str(msg['RemarkName'])
# , str(msg['Signature']), str(msg['Province']), str(msg['City']), str(msg['MemberCount'])
# , str(msg['Sex']), str(msg['AttrStatus']), str(msg['SnsFlag']), str(msg['ContactFlag'])
# , str(msg['HeadImgUrl']), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# cursor.execute(sql_insert_group)
# db_conn.commit()
except Exception as e:
# db.rollback() # 发生错误时回滚
print("Error", e)
finally:
# 关闭数据库连接
cursor.close()
db_conn.close()

执行方法

1
2
3
4
5
6
7
if __name__ == '__main__':
# sched = BlockingScheduler()
itchat.auto_login(loginCallback=after_login, exitCallback=after_logout)
result = itchat.search_friends() # 获取自己的用户信息,返回自己的属性字典
# my_name = result.NickName
my_UserName = result.UserName # 我的id(用于判断是否是我发的消息)
itchat.run()

代码及数据库链接

分享到