cluic / wxauto

Windows版本微信客户端(非网页版)自动化,可实现简单的发送、接收微信消息,简单微信机器人
MIT License
2.88k stars 465 forks source link

哈喽,有办法实现这个功能吗:当任意好友向我发送消息时,将微信聊天窗口跳转到该好友的聊天窗口,因为我想将chatgpt接入微信,可以自动回复消息,目前只能在指定的几个好友里完成这个功能,如果能自动跳转任意好友就更好了,不过很感谢提供的代码,非常好用,谢谢了~ #109

Open blcuyzc opened 1 year ago

Lucky-lhq commented 9 months ago

import time from wxauto import WeChat import re from test import *

定义个人微信号

MY_WXNAME = 'L'

实例化微信机器人

wechat = WeChat()

定义提取消息中的前面内容的函数

def extract_content(user_new_msg): content_list = [] pattern = r'(.*?)(\d+条新消息)' for message in user_new_msg: match = re.match(pattern, message) if match: content_before_number = match.group(1) content_list.append(content_before_number) return content_list

主处理函数

def process_new_messages(): while True: user_new_msg = wechat.GetSessionList() print(user_new_msg)

    if not user_new_msg:
        time.sleep(10)
    else:
        content_list = extract_content(user_new_msg)
        for content in content_list:
            wechat.Search(content)
            chat_history = wechat.GetLastMessage
            time.sleep(1)
            print(chat_history)

            if chat_history is None:
                print('还未有消息产生')
            elif chat_history[0] == MY_WXNAME:
                continue
            else:
                text.clear()
                question = checklen(getText("user", f'{chat_history[1]}'))
                SparkApi.answer = ""
                SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
                getText("assistant", SparkApi.answer)
                msg = text[1]['content']
                text.clear()
                wechat.SendMsg(msg)
                # user_new_msg.append(chat_history[0])

if name == "main": process_new_messages()

test.py

import SparkApi

以下密钥信息从控制台获取

appid = "" # 填写控制台中获取的 APPID 信息 api_secret = "" # 填写控制台中获取的 APISecret 信息 api_key = "" # 填写控制台中获取的 APIKey 信息

用于配置大模型版本,默认“general/generalv2”

domain = "general" # v1.5版本

domain = "generalv2" # v2.0版本

云端环境的服务地址

Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat" # v1.5环境的地址

Spark_url = "ws://spark-api.xf-yun.com/v2.1/chat" # v2.0环境的地址

text = []

length = 0

def getText(role, content): jsoncon = {} jsoncon["role"] = role jsoncon["content"] = content text.append(jsoncon) return text

def getlength(text): length = 0 for content in text: temp = content["content"] leng = len(temp) length += leng return length

def checklen(text): while (getlength(text) > 8000): del text[0] return text

text.clear()

Input = input("\n" + "我:")

question = checklen(getText("user", '讲个笑话猪八戒的笑话'))

SparkApi.answer = ""

print("星火:", end="")

SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)

getText("assistant", SparkApi.answer)

print(text[1]['content'])

text.clear()

我自己 写了一个 用的 讯飞的 api

tiantianshangxiang commented 2 months ago

从 wxauto 导入 微信从测试导入 import 的导入时间 *

定义个人微信号

MY_WXNAME = 'L'

实例化微信机器人

微信 = WeChat()

定义提取消息中的前面内容的函数

定义 extract_content(user_new_msg): content_list = [] 模式 = r'(.*?)(\d+条新消息)' for message in user_new_msg: match = re.match(pattern, message) if match: content_before_number = match.group(1) content_list.append(content_before_number) return content_list

主处理函数

def process_new_messages(): while True: user_new_msg = 微信。GetSessionList() 打印(user_new_msg)

    if not user_new_msg:
        time.sleep(10)
    else:
        content_list = extract_content(user_new_msg)
        for content in content_list:
            wechat.Search(content)
            chat_history = wechat.GetLastMessage
            time.sleep(1)
            print(chat_history)

            if chat_history is None:
                print('还未有消息产生')
            elif chat_history[0] == MY_WXNAME:
                continue
            else:
                text.clear()
                question = checklen(getText("user", f'{chat_history[1]}'))
                SparkApi.answer = ""
                SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
                getText("assistant", SparkApi.answer)
                msg = text[1]['content']
                text.clear()
                wechat.SendMsg(msg)
                # user_new_msg.append(chat_history[0])

if name == “main”: process_new_messages()

test.py

导入 SparkApi

以下密钥信息从控制台获取

appid = “” # 填写控制台中获取的 APPID 信息 api_secret = “” # 填写控制台中获取的 APISecret 信息 api_key = “” # 填写控制台中获取的 APIKey 信息

用于配置大模型版本,默认“general/generalv2”

domain = “general” # v1.5版本

domain = “generalv2” # v2.0版本

云端环境的服务地址

Spark_url = “ws://spark-api.xf-yun.com/v1.1/chat” # v1.5环境的地址

Spark_url = “ws://spark-api.xf-yun.com/v2.1/chat” # v2.0环境的地址

文本 = []

长度 = 0

def getText(role, content): jsoncon = {} jsoncon[“role”] = role jsoncon[“content”] = content text.append(jsoncon) 返回文本

def getlength(text): length = 0 for content in text: temp = content[“content”] leng = len(temp) length += leng 返回长度

def checklen(text): while (getLength(text) > 8000): del text[0] 返回文本

text.clear()

输入 = input(“\n” + “我:”)

question = checklen(getText(“user”, '讲个笑话猪八戒的笑话'))

SparkApi.answer = “”

print(“星火:”, end=“”)

SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)

getText(“助手”, SparkApi.answer)

打印(文本[1]['内容'])

text.clear()

我自己 写了一个 用的 讯飞的 api

你的星火回复慢吗 我的回复好慢啊