mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-05-12 06:41:13 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ad2997717 | |||
| 37a95980d4 | |||
| d9ef5a6612 |
@@ -12,8 +12,7 @@ name: Create and publish a Docker image
|
||||
on:
|
||||
push:
|
||||
branches: ['master']
|
||||
release:
|
||||
types: [published]
|
||||
create:
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
@@ -5,9 +5,11 @@ wechat channel
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import requests
|
||||
import io
|
||||
import time
|
||||
from common.singleton import singleton
|
||||
from lib import itchat
|
||||
import json
|
||||
from lib.itchat.content import *
|
||||
@@ -68,11 +70,11 @@ def _check(func):
|
||||
return func(self, msg)
|
||||
return wrapper
|
||||
|
||||
|
||||
@singleton
|
||||
class WechatChannel(Channel):
|
||||
def __init__(self):
|
||||
self.userName = None
|
||||
self.nickName = None
|
||||
self.user_id = None
|
||||
self.name = None
|
||||
self.receivedMsgs = ExpiredDict(60*60*24)
|
||||
|
||||
def startup(self):
|
||||
@@ -90,9 +92,9 @@ class WechatChannel(Channel):
|
||||
itchat.auto_login(enableCmdQR=2, hotReload=hotReload)
|
||||
else:
|
||||
raise e
|
||||
self.userName = itchat.instance.storageClass.userName
|
||||
self.nickName = itchat.instance.storageClass.nickName
|
||||
logger.info("Wechat login success, username: {}, nickname: {}".format(self.userName, self.nickName))
|
||||
self.user_id = itchat.instance.storageClass.userName
|
||||
self.name = itchat.instance.storageClass.nickName
|
||||
logger.info("Wechat login success, user_id: {}, nickname: {}".format(self.user_id, self.name))
|
||||
# start message listener
|
||||
itchat.run()
|
||||
|
||||
@@ -105,8 +107,8 @@ class WechatChannel(Channel):
|
||||
# isgroup: 是否是群聊
|
||||
# receiver: 需要回复的对象
|
||||
# msg: itchat的原始消息对象
|
||||
# origin_ctype: 原始消息类型,用于私聊语音消息时,避免匹配前缀
|
||||
# desire_rtype: 希望回复类型,TEXT类型是文本回复,VOICE类型是语音回复
|
||||
# origin_ctype: 原始消息类型,语音转文字后,私聊时如果匹配前缀失败,会根据初始消息是否是语音来放宽触发规则
|
||||
# desire_rtype: 希望回复类型,默认是文本回复,设置为ReplyType.VOICE是语音回复
|
||||
|
||||
@time_checker
|
||||
@_check
|
||||
@@ -160,17 +162,12 @@ class WechatChannel(Channel):
|
||||
group_id = msg['User'].get('UserName', None)
|
||||
if not group_name:
|
||||
return ""
|
||||
origin_content = msg['Content']
|
||||
content = msg['Content']
|
||||
content_list = content.split(' ', 1)
|
||||
context_special_list = content.split('\u2005', 1)
|
||||
if len(context_special_list) == 2:
|
||||
content = context_special_list[1]
|
||||
elif len(content_list) == 2:
|
||||
content = content_list[1]
|
||||
content = msg.content
|
||||
if "」\n- - - - - - - - - - - - - - -" in content:
|
||||
logger.debug("[WX]reference query skipped")
|
||||
return ""
|
||||
pattern = f'@{self.name}(\u2005|\u0020)'
|
||||
content = re.sub(pattern, r'', content)
|
||||
|
||||
config = conf()
|
||||
group_name_white_list = config.get('group_name_white_list', [])
|
||||
@@ -209,6 +206,7 @@ class WechatChannel(Channel):
|
||||
if context:
|
||||
thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback)
|
||||
|
||||
# 根据消息构造context,消息内容相关的触发项写在这里
|
||||
def _compose_context(self, ctype: ContextType, content, **kwargs):
|
||||
context = Context(ctype, content)
|
||||
context.kwargs = kwargs
|
||||
@@ -233,9 +231,9 @@ class WechatChannel(Channel):
|
||||
return None
|
||||
else: # 单聊
|
||||
match_prefix = check_prefix(content, conf().get('single_chat_prefix'))
|
||||
if match_prefix: # 判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容
|
||||
if match_prefix is not None: # 判断如果匹配到自定义前缀,则返回过滤掉前缀+空格后的内容
|
||||
content = content.replace(match_prefix, '', 1).strip()
|
||||
elif context["origin_ctype"] == ContextType.VOICE: # 如果源消息是私聊的语音消息,不匹配前缀,直接返回
|
||||
elif context["origin_ctype"] == ContextType.VOICE: # 如果源消息是私聊的语音消息,允许不匹配前缀,放宽条件
|
||||
pass
|
||||
else:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user