Compare commits

1 Commits
main ... dev

Author SHA1 Message Date
hackrobot
5c33bc7d31 feat: 发送卡片链接 2024-03-04 20:53:45 +08:00

View File

@@ -75,7 +75,8 @@ class Robot(Job):
@staticmethod @staticmethod
def value_check(args: dict) -> bool: def value_check(args: dict) -> bool:
if args: if args:
return all(value is not None for key, value in args.items() if key != 'proxy') return all(value is not None for key, value in args.items()
if key != 'proxy')
return False return False
def toAt(self, msg: WxMsg) -> bool: def toAt(self, msg: WxMsg) -> bool:
@@ -116,10 +117,33 @@ class Robot(Job):
"""闲聊,接入 ChatGPT """闲聊,接入 ChatGPT
""" """
if not self.chat: # 没接 ChatGPT固定回复 if not self.chat: # 没接 ChatGPT固定回复
nq = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "")
# print(nq,'==<nq')
if nq == '加群':
# 获取所有联系人
allcontacts=self.wcf.get_contacts()
print('allcontacts==>', allcontacts)
#通过wxid查询用户 头像/昵称/性别,并存储数据库 [目前无法获取头像]
senferInfo = self.wcf.get_info_by_wxid(wxid=msg.sender)
# 'wxid': 'wxid_4413224132412', 'code': 'smallzhiyun', 'remark': '', 'name': 'Eric', 'country': '', 'province': '', 'city': '', 'gender': '男'}
print('senferInfo==>', senferInfo)
# 发送link卡片信息 (url设置key=wxid)
#用户打开后,将wxid和opid存储到localstorage,并将绑定关系设置到数据库, 防止重复绑定
self.wcf.send_rich_text(
name='异度世界',
title=f'欢迎{senferInfo["name"]}加入群聊,标题,最多两行',
digest='摘要,三行',
account='gh_bebb67a13c77',
url=f'https://www.baidu.com?wxid={msg.sender}',
thumburl=
'http://wx.qlogo.cn/mmopen/uicvdbOicOINDh2GSMGquIBT5kVvq6qY129SLCSWRgicFjhAwbhJXhx6jiaVleDdyaPYNYbejV1IE1fn0t7jINvXmEphabXasBa6/64',
receiver=msg.sender)
return False
rsp = "你@我干嘛?" rsp = "你@我干嘛?"
else: # 接了 ChatGPT智能回复 else: # 接了 ChatGPT智能回复
q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "") q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "")
rsp = self.chat.get_answer(q, (msg.roomid if msg.from_group() else msg.sender)) rsp = self.chat.get_answer(
q, (msg.roomid if msg.from_group() else msg.sender))
if rsp: if rsp:
# 回复群消息 # 回复群消息
@@ -128,7 +152,6 @@ class Robot(Job):
# 回复私聊 # 回复私聊
else: else:
self.sendTextMsg(rsp, msg.sender) self.sendTextMsg(rsp, msg.sender)
return True return True
else: else:
self.LOG.error(f"无法从 ChatGPT 获得答案") self.LOG.error(f"无法从 ChatGPT 获得答案")
@@ -186,6 +209,7 @@ class Robot(Job):
self.wcf.enable_recv_msg(self.onMsg) self.wcf.enable_recv_msg(self.onMsg)
def enableReceivingMsg(self) -> None: def enableReceivingMsg(self) -> None:
def innerProcessMsg(wcf: Wcf): def innerProcessMsg(wcf: Wcf):
while wcf.is_receiving_msg(): while wcf.is_receiving_msg():
try: try:
@@ -198,7 +222,10 @@ class Robot(Job):
self.LOG.error(f"Receiving message error: {e}") self.LOG.error(f"Receiving message error: {e}")
self.wcf.enable_receiving_msg() self.wcf.enable_receiving_msg()
Thread(target=innerProcessMsg, name="GetMessage", args=(self.wcf,), daemon=True).start() Thread(target=innerProcessMsg,
name="GetMessage",
args=(self.wcf, ),
daemon=True).start()
def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None: def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None:
""" 发送消息 """ 发送消息
@@ -230,8 +257,12 @@ class Robot(Job):
获取联系人(包括好友、公众号、服务号、群成员……) 获取联系人(包括好友、公众号、服务号、群成员……)
格式: {"wxid": "NickName"} 格式: {"wxid": "NickName"}
""" """
contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") contacts = self.wcf.query_sql(
return {contact["UserName"]: contact["NickName"] for contact in contacts} "MicroMsg.db", "SELECT UserName, NickName FROM Contact;")
return {
contact["UserName"]: contact["NickName"]
for contact in contacts
}
def keepRunningAndBlockProcess(self) -> None: def keepRunningAndBlockProcess(self) -> None:
""" """