Self-made chat robot realizes dialogue with chatgpt or WeChat friends [attached code]

I have nothing to do, and I want to implement a chat robot that can talk to chatgpt or WeChat friends . The chat robot can also be applied to voice input of QQ friends or other places. The functions are relatively simple, and will be updated slowly in the later period, so that the computer-computer interaction experience will continue to improve.

project description:

Voice input "Open Voice Assistant" , wait for the user's voice input (the waiting time is 2 seconds, which can be adjusted by yourself), and then input the voice-to-text into the dialog box (the recognition time is 5 seconds, which can be adjusted by yourself), and the voice input" is turned off Voice Assistant" will bring up the program. Use speech recognition to complete text input and realize chat function.


Environment description

python language

windows10


Preparation

First install the following libraries

pip install pyperclip
pip install pyautogui
pip install speech_recognition
pip install pyaudio
pip install wave

The sending text dialogue of this project is used to simulate the sending button of the mouse, so it is necessary to obtain the coordinate position of the "send" button relative to the screen .

This coordinate position is also very easy to obtain, and it can be displayed directly by using the WeChat screenshot shortcut key.

For example, if you want to get the coordinates of the send button in the WeChat dialog box, click on the screenshot of WeChat, put the mouse on the send button, and there is a POS that is the coordinate position.


 

function display

You need to turn on scientific Internet access during the whole process of use (there are many methods on the Internet here, find it yourself)

Chat with WeChat friends

1. Modify the coordinate position of the send button

2. If it is a WeChat conversation, you need to comment out the following lines:

            # 对于chatgpt需要在点击一下对话框
            pyautogui.moveTo(639, 929)
            pyautogui.mouseDown()  # 鼠标左键按下,发送内容
            pyautogui.mouseUp()  # 鼠标左键抬起

I am here (1405, 787)

pyautogui.moveTo(1405, 787)  # 鼠标光标移动至发送按钮(这里是坐标)
pyautogui.mouseDown()  # 鼠标左键按下,发送内容
pyautogui.mouseUp()  # 鼠标左键抬起
python wechat_root/wechat.py

3. Language input "Enable Language Assistant" will activate the function. 

开始录制...
录制结束...
result2:
{   'alternative': [{'confidence': 0.97500253, 'transcript': '开启语音助手'}],
    'final': True}

 4. When the "microphone" icon is displayed in the lower right corner of your windows taskbar, it means that the recording is turned on, and when the icon is turned off, it means that the recording is over. The default recording time is 5 seconds, which can be modified by yourself.

5. At this time, the voice-to-text will be entered in the WeChat dialog box and sent automatically.

Each input interval is 2 seconds (can be modified by yourself). Just modify the following.

    if text_ == '开启语音助手':  # 开启
        time.sleep(2)  # 等待2s输入一次

Nothing is sent if the user doesn't enter anything. Sent only if input is valid.

 

 6. The user voice inputs "turn off the voice assistant" to exit.


Talk to chatGPT 

It is the same as the above WeChat conversation, except that the chatgpt send button and dialog box coordinates need to be modified.

For example, my chatgpt send button is (1531,933), and the coordinates of the dialog box are (639,929).

            pyautogui.moveTo(1531, 933)  # 鼠标光标移动至发送按钮(这里是坐标)
            pyautogui.mouseDown()  # 鼠标左键按下,发送内容
            pyautogui.mouseUp()  # 鼠标左键抬起
            # 对于chatgpt需要在点击一下对话框
            pyautogui.moveTo(639, 929)
            pyautogui.mouseDown()  # 鼠标左键按下,发送内容
            pyautogui.mouseUp()  # 鼠标左键抬起

The user voice input "turn on the voice assistant" can start the dialogue.

 


 full code

https://github.com/YINYIPENG-EN/chatbot.git

Guess you like

Origin blog.csdn.net/z240626191s/article/details/131460932