首頁 > 軟體

基於chatgpt的微信自動回覆功能實現

2023-02-25 06:01:43

微信自動回覆 基於聊天api的

import pyautogui
import pyperclip
import keyboard
import requests
import time

print('程式即將開始,請開啟微信!')

# 檢測是否有新訊息
def findNews():
    left, top, width, height = pyautogui.locateOnScreen("news.png", confidence=0.9)
    pyautogui.click(left + 20, top + 20)
    print('發現了新訊息')

# 傳送訊息
def sendMsg():
    left, top, width, height = pyautogui.locateOnScreen('icon.png', confidence=0.9)
    print('獲取到了圖示位置')
    X = left + width
    pyautogui.rightClick(X, top - 40)
    pyautogui.click(X + 10, top - 40 + 10)
    friendMsg = pyperclip.paste() #將拷貝板內的文字轉換為字串
    print('好友的訊息:' + friendMsg)
    url = 'https://v.api.aa1.cn/api/api-xiaoai/talk.php'
    print('正在思考如何回覆...')
    res = requests.get(url, params="msg=" + friendMsg)
    time.sleep(1)
    reply = res.text
    print('即將傳送的訊息:' + reply)
    pyperclip.copy(reply)
    pyautogui.click(X, top + 50)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(3)
    pyautogui.press('enter')
    print('傳送成功!')
    time.sleep(1)
    # 恢復原始狀態
    print('恢復原始狀態')
    left, top, width, height = pyautogui.locateOnScreen('reset.png', confidence=0.9)
    pyautogui.click(left + 20, top  + 20)

# 開始執行
while True:
    # time.sleep(1)
    # 如果按下退格鍵,則退出迴圈
    if keyboard.is_pressed('backspace'):
        print('按下了退格鍵,程式即將結束')
        break
    
    # 捕獲錯誤
    try:
        findNews()
        sendMsg()

    except TypeError:
        print('沒有發現新訊息...', time.time())

pyautogui.alert(text='Python程式已結束!', title='提示', button='好的')
print("程式已結束!") 

微信自動回覆 基於chatgpt的

import openai
import pyautogui
import pyperclip
import keyboard
import time

openai.api_key = "你的chat-gpt API"
def chat_gpt(prompt):# 你的問題prompt = prompt# 呼叫 ChatGPT 介面
    model_engine = "text-davinci-003"
    completion = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,)
    response = completion.choices[0].text
    return response
        

print('程式即將開始,請開啟微信!')

# 檢測是否有新訊息
def findNews():
    left, top, width, height = pyautogui.locateOnScreen("news.png", confidence=0.9)
    pyautogui.click(left + 20, top + 20)
    print('發現了新訊息')

# 傳送訊息
def sendMsg():
    left, top, width, height = pyautogui.locateOnScreen('icon.png', confidence=0.9)
    print('獲取到了圖示位置')
    X = left + width
    pyautogui.rightClick(X, top - 35)
    pyautogui.click(X + 10, top - 40 + 10)
    friendMsg = pyperclip.paste() #將拷貝板內的文字轉換為字串
    print('好友的訊息:' + friendMsg)
    #url = 'https://v.api.aa1.cn/api/api-xiaoai/talk.php'
    print('正在思考如何回覆...')
    #res = requests.get(url, params="msg=" + friendMsg)
    #time.sleep(1)
    reply = chat_gpt(friendMsg).replace('?','').strip()
    print('即將傳送的訊息:' + reply)
    pyperclip.copy(reply)
    pyautogui.click(X, top + 50)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    pyautogui.press('enter')
    print('傳送成功!')
    #time.sleep(1)
    # 恢復原始狀態
    print('恢復原始狀態')
    left, top, width, height = pyautogui.locateOnScreen('reset.png', confidence=0.9)
    pyautogui.click(left + 20, top  + 20)

# 開始執行
while True:
    # time.sleep(1)
    # 如果按下退格鍵,則退出迴圈
    if keyboard.is_pressed('backspace'):
        print('按下了退格鍵,程式即將結束')
        break
    
    # 捕獲錯誤
    try:
        findNews()
        sendMsg()

    except TypeError:
        print('沒有發現新訊息...', time.time())

pyautogui.alert(text='Python程式已結束!', title='提示', button='好的')
print("程式已結束!") 

到此這篇關於基於chatgpt的微信自動回覆功能實現的文章就介紹到這了,更多相關chatgpt微信自動回覆內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com