使用 WPUSH + Github Actions 每天定时推送60s早报新闻

代码:

import os
import requests


# 获取早报信息
def get_news():
    alapi_token = os.getenv('alapi_token')
    url = 'https://v2.alapi.cn/api/zaobao'
    params = {'token': alapi_token}
    response = requests.get(url, params=params)
    data = response.json()
    if data.get('code') == 200:
        return data.get('data')
    else:
        print(data.get("msg"))
        return None


# 推送消息
def push_message(title, content):
    wpush_apikey = os.getenv('wpush_apikey')
    url = 'https://api.wpush.cn/api/v1/send'
    channel = os.getenv("channel", "wechat")
    if channel not in ["wechat", "sms", "mail", "feishu", "dingtalk", "webhook", "wechat_work"]:
        channel = "wechat"

    params = {'apikey': wpush_apikey, 'title': title, 'content': content, 'channel': channel}
    response = requests.post(url, data=params)
    data = response.json()
    if data.get("code") == 0:
        return True
    else:
        print(data.get("message"))
        return False


# 主函数
def main():
    if not os.getenv('alapi_token') or not os.getenv('wpush_apikey'):
        print('请设置alapi_token和wpush_apikey环境变量!')
        return
    send_type = os.getenv("type", "image")

    if send_type not in ["image", "text"]:
        send_type = "image"

    news_data = get_news()
    if news_data:
        date = news_data.get('date')
        news_list = news_data.get('news')
        image_url = news_data.get('image')

        title = '每日60秒早报'
        content = f'### 日期:{date}\n\n'

        if send_type == "image":
            content += f'![image]({image_url})'
        else:
            content += '#### 新闻:\n'
            for news in news_list:
                content += f'- {news}\n'

        if push_message(title, content):
            print('消息推送成功!')
        else:
            print('消息推送失败!')
    else:
        print('获取早报信息失败!')


if __name__ == '__main__':
    main()

actions 代码:

name: 每日早报定时推送
on:
  workflow_dispatch:
  schedule:
    - cron: "0 8 * * *" # 每天早上8点推送
jobs:
  push:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v3

      - name: 安装python环境
        uses: actions/setup-python@v3

      - name: 安装依赖
        run: |
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: 开始推送
        env:
          wpush_apikey: ${{ secrets.WPUSH_APIKEY }} # wpush apikey
          alapi_token: ${{ secrets.ALAPI_TOKEN }}
          type: ${{ secrets.TYPE }}
          channel: ${{ secrets.CHANNEL }}
        run: |
          python main.py
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!