使用python做微信ai自动回复功能

1.引入ai大语言模型

首先这里的ai用的阿里云的通义千问大数据模型,地址:阿里云

申请一个api_key;

然后再页面上使用pip引入dashscope模块,再引入你的api_key:

import dashscope

dashscope.api_key = api_key 

from dashscope import Generation
from http import HTTPStatus

response = Generation.call(
    model='qwen-turbo',
    prompt='就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品'
)

if response.status_code == HTTPStatus.OK:
    print(response.output)  # The output text
    print(response.usage)  # The usage information
else:
    print(response.code)  # The error code.
    print(response.message)  # The error message.

这里咱们只要传入相应的文字,response就会返回相应的答案;

2.微信自动回复

首先是要引入三个模块 使用pip install

import pandas as pd
import numpy as np
from uiautomation import WindowControl, MenuControl

打开微信窗口,查找未读消息,然后把或获取到的消息,调取ai模型的api接口,具体代码如下:

import pandas as pd
import numpy as np

import dashscope

dashscope.api_key = api_key 
from http import HTTPStatus
# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html

from dashscope import Generation

from uiautomation import WindowControl, MenuControl

# 绑定微信主窗口
# wx = WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC')
wx = WindowControl(Name="微信")
print(wx)
# 切换窗口
wx.SwitchToThisWindow()
hw = wx.ListControl(Name="会话")
print('寻找会话控件绑定', hw)
df = pd.read_csv('回复数据.csv', encoding='utf-8')
while True:
    we = hw.TextControl(searchDepth=4)
    print('查找会话', we)
    while not we.Exists(0):
        pass
    if we.Name:
        we.Click(simulateMove=False)
        last_msg = wx.ListControl(Name="消息").GetChildren()[-1].Name
        prompt_text = last_msg
        response_generator = Generation.call(
            model='qwen-turbo',
            prompt=prompt_text,
            stream=True,
            top_p=0.8)
        for response in response_generator:
            if response.status_code == HTTPStatus.OK:

                if (response.output.finish_reason == 'stop'):
                    wx.SendKeys(response.output.text, waitTime=0)
                    wx.SendKeys('{Enter}', waitTime=0)
                    wx.TextControl(SubName=last_msg[:5]).RightClick()

            else:
                print(response.code)  # The error code.
                print(response.message)  # The

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值