Skip to content

Commit

Permalink
fix: hot reload repeat msg bug #186
Browse files Browse the repository at this point in the history
  • Loading branch information
zhayujie committed Mar 22, 2023
1 parent 2435b45 commit 33e69d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ go-cqhttp
logs/
session.token
.vscode
itchat.pkl
21 changes: 18 additions & 3 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
wechat channel
"""

import time
import itchat
import json
from itchat.content import *
Expand Down Expand Up @@ -40,10 +41,11 @@ def __init__(self):

def startup(self):
# login by scan QRCode
if (channel_conf_val(const.WECHAT, 'receive_qrcode_api')):
itchat.auto_login(enableCmdQR=2, hotReload=True, qrCallback=self.login)
hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True)
if channel_conf_val(const.WECHAT, 'receive_qrcode_api'):
itchat.auto_login(enableCmdQR=2, hot_reload=hot_reload, qrCallback=self.login)
else:
itchat.auto_login(enableCmdQR=2, hotReload=True)
itchat.auto_login(enableCmdQR=2, hotReload=hot_reload)

# start message listener
itchat.run()
Expand All @@ -59,8 +61,14 @@ def handle(self, msg):
from_user_id = msg['FromUserName']
to_user_id = msg['ToUserName'] # 接收人id
other_user_id = msg['User']['UserName'] # 对手方id
create_time = msg['CreateTime'] # 消息时间
content = msg['Text']

hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True)
if hot_reload == True and int(create_time) < int(time.time()) - 60: # 跳过1分钟前的历史消息
logger.debug("[WX]history message skipped")
return

# 调用敏感词检测函数
if sw.process_text(content):
self.send('请检查您的输入是否有违规内容', from_user_id)
Expand Down Expand Up @@ -98,6 +106,13 @@ def handle_group(self, msg):
logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
group_name = msg['User'].get('NickName', None)
group_id = msg['User'].get('UserName', None)
create_time = msg['CreateTime'] # 消息时间

hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True)
if hot_reload == True and int(create_time) < int(time.time()) - 60: # 跳过1分钟前的历史消息
logger.debug("[WX]history message skipped")
return

if not group_name:
return None
origin_content = msg['Content']
Expand Down

0 comments on commit 33e69d5

Please sign in to comment.