我的.网络
分类: 技术(16) 维护(3) 域名(4)

使用API生成早报及历史上的今天数据并推送到企业微信机器人

我有一些活跃的群组,每天早上我都会在群组里发送早报及历史上的今天,每天一分钟,了解天下事,同时提升群组成员的早间热度,共同陶冶情操。所以我寻找到相关API,并利用Python脚本推送到机器人,然后把获取到的数据经过整理发送给我就可以直接转发到群组。

1.脚本的编写

在开始编写脚本之前确保你的服务器安装了Python并运行正常,根据服务器系统的不同请自行安装。

每日早报Python脚本如下:

import requests
import json
import hashlib
import base64

# 1. 发送请求获取图片
api_url = "https://api.qqsuu.cn/api/dm-60s" # 每日早报的API,为图片版
response = requests.get(api_url)

if response.status_code == 200:
    # 2. 获取图片数据
    image_data = response.content

    # 3. 计算图片的 MD5 值
    md5 = hashlib.md5(image_data).hexdigest()

    # 4. 构建企业微信机器人消息体,包含图片的 base64 数据和 MD5 值
    data = {
        "msgtype": "image",
        "image": {
            "base64": base64.b64encode(image_data).decode('utf-8'),
            "md5": md5
        }
    }

    # 5. 发送消息到企业微信机器人
    robot_id = 'xxxxxxxxxxxxxxx'  # 替换为你的企业微信机器人的 key
    headers = {'content-type': 'application/json;charset=utf-8'}
    url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + robot_id
    body = json.dumps(data)
    res = requests.post(url, data=body, headers=headers)

    if res.status_code == 200:
        print("图片已成功发送到企业微信机器人")
    else:
        print(f"发送到企业微信机器人时出错: {res.status_code}")
else:
    print(f"请求 API 时出错: {response.status_code}")

历史上的今天Python脚本如下:

import requests
import json

# 1. 发送请求获取API结果
api_url = "https://www.ipip5.com/today/api.php?type=txt" # 这是获取历史上的今天数据API
response = requests.get(api_url)

if response.status_code == 200:
    # 2. 处理API结果
    api_result = response.text.split('\n')[:-1]  # 去除最后一行
    modified_result = []

    # 替换并处理每一行
    for line in api_result:
        if "感谢 www.ipip5.com 提供数据支持" in line:
            # 替换并去除年份
            line = line.replace("感谢 www.ipip5.com 提供数据支持", "Powered by Jianfei Wang")
            line = line.split()[-1]  # 去除年份
        modified_result.append(line)

    # 在结果最后一行添加 "Powered by Jianfei Wang"
    modified_result.append("Powered by Jianfei Wang") # 此处为我自己自定义添加

    final_result = "\n".join(modified_result)

    # 3. 发送消息到企业微信机器人
    robot_id = 'xxxxxxxxxxxxxxxx'  # 替换为你的企业微信机器人的 key
    wechat_robot_url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={robot_id}'

    data = {
        "msgtype": "text",
        "text": {"content": final_result}
    }

    headers = {'content-type': 'application/json;charset=utf-8'}
    wechat_response = requests.post(wechat_robot_url, data=json.dumps(data), headers=headers)

    if wechat_response.status_code == 200:
        print("消息已成功发送到企业微信机器人")
    else:
        print(f"发送到企业微信机器人时出错: {wechat_response.status_code}")
else:
    print(f"请求 API 时出错: {response.status_code}")

2.执行脚本

如上所示,例如我把每日早报的Python脚本命名为 news.py ,那么在终端中执行脚本的命令如下:

Python3 /xxx/xxx/news.py #也就是 news.py 的位置

3.定时推送

由于我们不可能每次都手动执行脚本,所以可以利用宝塔的“计划任务”设定每天某一个时间点来执行脚本运行,任务类型选择“Shell脚本”,这样就会在设定的时间点准时自动收到最新数据(其实还是要看你的服务器延迟速度),如下图所示:
2024-01-07T14:29:05.png
设置完成之后,可点击“执行”按钮进行测试是否成功推送,若失败请检查脚本位置及执行命令是否正确。

分类:技术


在我们信仰的代码中,我们热爱开发
订阅站点地图其他项目关于
© 2023-2024 我的.网络 版权所有
⭡回到顶部