A WeChat robot that chats with me, satisfying the lonely soul of overtime workers~

Hello everyone! I am a red panda ❤

Do you sometimes feelLonely, empty, meaningless?

Even if you are in the crowd, you will still feel the distance of your soul,

Fear of being swallowed by loneliness when alone...

This seems to be a "common disease" among contemporary young people.

Please add image description

In fact, everyone is like arun aloneThe planets,
in their respective orbits, are sometimes near and sometimes far,

Even if we meet at times, we know that we will eventually separate,

Everyone has their own track.

Once you have contact, it is only a matter of taste.

Rarely will you encounter a collision of thoughts that touches your soul.

As an ordinary person, I also have this dilemma from time to time.

So... I made a WeChat robot that can chat with me~

If you have any python-related error answers that you don't know, or source code/module installation/women's clothing masters are proficient in skills, you can come here: ( https://jq.qq.com/?_wv=1027&k=2Q3YTfym) or +V: python10010 ask me


Show results

Please add image description


WeChat send and receive messages in real time

First of all, this is the Windows version of WeChat

1. Third-party libraries

Real-time sending and receiving of messages on WeChat based on third-party pc-wechat-hook-http-api.

It should be noted that this library is based on WeChat version 3.6.0.18. After downloading the corresponding version of WeChat, overwrite the installation directly, so that the chat history can be preserved.

2. Open the injection dll

To get WeChat messages in real time, you need to hijack the response entry. In this part, we don't need to care too much about the technical details, just a fool's click.

Download the following three files from the official address of pc-wechat-hook-http-api.
Please add image description
Please add image description

Copy the HPSocket4C.dll file to the WeChat directory (eg E:\Tencent\WeChat[3.6.0.18])

Click on the Daen injector .exe file:

in:

1. The file directory refers to the WeChat installation path, refer to the above figure.
2. The DLL path refers to the full path of the DaenWxHook.dll file.
3. The process parameters can be directly used by default. In the figure, 8089 refers to the local http server port used to receive WeChat real-time messages. 8055 refers to the http server port opened by the dll. When sending a message, you only need to post data to this port.

Click inject and start, and log in to WeChat.

3. Send and receive messages in real time

Official document for sending and receiving messages in real time: https://www.apifox.cn/apidoc/project-1222856/doc-1012539

Receive WeChat messages

For receiving messages in real time, in simple terms, it is to create an http server locally with the same port as the process parameters, that is, the default address is: http://localhost:8089/wechat/. We create http server by using flask library

from flask import Flask, request
import json
import requests
app = Flask(__name__)
 
def on_rcv_chatroom_msg(from_wxid, msg):
    print("收到群消息")

def on_rcv_p2p_txt(from_wxid, msg_txt):
    print("收到文本消息", from_wxid, msg_txt)


@app.route('/wechat/', methods=['get', 'post'])
def wechat():
    data = request.stream.read()
    data = data.decode('utf-8')
    data = json.loads(data)
    if type == 'D0003':
        data = data['data']
        msg = data['msg']
        from_wxid = data['fromWxid']
        if "@chatroom" in from_wxid:
            on_rcv_chatroom_msg(from_wxid, msg)
        else:
            on_rcv_p2p_txt(from_wxid, msg)
    return ''

if __name__ == '__main__':
    app.run(debug=True, port=8089)

Please add image description

Send WeChat message

For sending messages, post messages to the specified port (default 8055):

import json
import requests 
def send_msg(wxid, is_img, msg):
    if is_img:
        payload = {
    
    "type": "Q0010", "data": {
    
    "wxid": wxid, "path": msg}}
    else:
        payload = {
    
    "type": "Q0001", "data": {
    
    "wxid": wxid, "msg": msg}}

    headers = {
    
    
        'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)',
        'Content-Type': 'application/json'
    }
    # 请求url
    url = 'http://127.0.0.1:8055/DaenWxHook/client/'
    # 请求参数

    # 调用post
    response = requests.post(url, json=payload,
                             headers=headers)  # response 响应对象
    # 获取响应状态码
    print('状态码:', response.status_code)
    # 获取响应头
    print('响应头信息:', response.headers)
    # 获取响应正文
    print('响应正文:', response.text)

def send_txt_msg(wxid, txt):
    send_msg(wxid, False, txt)

def send_img_msg(wxid, img_path):
    send_msg(wxid, True, img_path)

As above, the functions of sending text messages and picture messages are implemented respectively.


Please add image description

automated chatbot

Chatbots We also stand on the shoulders of giants,

Use the API provided by Qingyunke to realize automatic dialogue.

It should be noted that Qingyunke's robot is a female, and its name is Feifei.

If you want to customize the name, we can directly replace Feifei in the text of the reply with a customized name. The implementation code is as follows:

def talk_with_robot(msg, robot_name=None):
    url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
    html = requests.get(url)
    rt = html.json()["content"]
    rt = rt.replace("{br}","\n")
    if robot_name is not None:
        rt = rt.replace("菲菲", robot_name)
    return rt

Please add image description

For the complete source code and material, click on the business card below to get it~

Today's article is here~

I don't know if it helps you?

I'm Red Panda, see you in the next article (✿◡‿◡)

insert image description here

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/126587652