一个Python脚本实现视频号私信自动回复

只需要一个python,利用创作者后台的cookie和接口

可以实现自动收发私信

shipinhao.py

import re
import time
import uuid
import requests
import json
import sys
config_file = sys.argv[1] if len(sys.argv) > 1 else 'config.json'

# 打开并读取JSON文件
with open(config_file, 'r') as file:
    config = json.load(file)

# 获取sessionid和seapiBaseURL的值
sessionid = config['sessionid']
apiBase = config['apiBase']

cookie="sessionid="+sessionid
apiBase = config.get('apiBase', '')
if sessionid=="" or apiBase=="":
    print("sessionid和apiBase不能为空")
    exit()


loginCookie = ""

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Cookie": cookie,
    "Origin": "https://channels.weixin.qq.com",
    "Referer": "https://channels.weixin.qq.com/platform/private_msg",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
    "X-WECHAT-UIN": "3192142710",
    "finger-print-device-id": "460662ddd13730cd1b12357765a83e8e",
    "sec-ch-ua": "\"Chromium\";v=\"130\", \"Microsoft Edge\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}











while True:

    url = "https://channels.weixin.qq.com/cgi-bin/mmfinderassistant-bin/private-msg/get-new-msg"



    data = {
        "scene": 7,
        "reqScene": 7,
        "cookie": loginCookie,
    }

    response = requests.post(url, headers=headers, json=data)
    responseJson=response.json()

    if responseJson["errCode"]==300334 or responseJson["errCode"]==300330:

        print("cookie已经失效了,",response.text)
        break
    if  not "data" in responseJson:
        print("还没消息:",response.text)
        time.sleep(1)
        continue
    respData=responseJson["data"]
    # print(respData)

    if loginCookie!="" and len(respData["msg"])!=0:
        lastIndex=len(respData["msg"])-1
        fromUsername=respData["msg"][lastIndex]["fromUsername"]
        toUsername=respData["msg"][lastIndex]["toUsername"]
        sessionId=respData["msg"][lastIndex]["sessionId"]
        msgType=respData["msg"][lastIndex]["msgType"]
        msg=respData["msg"][lastIndex]["rawContent"]

        if not "@findermsgstranger" in fromUsername:
            time.sleep(1)
            continue

        print(fromUsername,msg)
        data = {
            "visitor_id": fromUsername,
            "content": msg,
            "visitor_name": fromUsername
        }
        response = requests.post(apiBase, json=data)
        replyContent=response.json()["result"]["content"]
        # 去除HTML标签
        replyContent = re.sub('<.*?>', '', replyContent)


            
        url = 'https://channels.weixin.qq.com/cgi-bin/mmfinderassistant-bin/private-msg/send-private-msg'
        # 生成UUID1
        uuid1 = uuid.uuid1()
        data = {
            "msgPack": {
                "sessionId": sessionId,
                "fromUsername":toUsername,
                "toUsername": fromUsername,
                "msgType": 1,
                "textMsg": {
                    "content": replyContent,
                },
                "cliMsgId": str(uuid1),
            },
            "scene": 7,
            "reqScene": 7
        }
        response = requests.post(url, headers=headers, data=json.dumps(data))

    loginCookie=respData["cookie"]
    time.sleep(1)

config.json

{
    "sessionid": "cookie里找",
    "apiBase": "https://gofly.v1kf.com 注册账号获取接口"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值