想搞一个企业微信消息自动提醒。
注册了腾讯轻联发现要收费。
于是自己去Github部署了。
企微建立机器人
首先你有群管理员权限,然后点消息推送。
取个名字,复制Webhook地址。
Github新建仓库
写一个main.py扔进去。
import requests
import json
import os
from datetime import datetime, timedelta, timezone
# 1. 设置北京时间
beijing_tz = timezone(timedelta(hours=8))
current_time = datetime.now(beijing_tz).strftime('%Y-%m-%d %H:%M')
# webhook放进全局变量里,从全局变量读取
webhook_url = os.environ["WECOM_WEBHOOK"]
# 2. 构造消息 (改为 text 类型)
data = {
"msgtype": "text",
"text": {
"content": f"🔔 每日提醒\n----------------\n当前时间:{current_time}\n大家记得按时打卡哦",
# 3. 这一行是关键,必须放在这里才能生效
"mentioned_list": ["@all"]
}
}
# 发送请求
response = requests.post(webhook_url, json=data)
print(response.text)
新建workflow
在Actions那里New workflow。
每天定时触发
name: Daily Push |
| ------------------------------------------- |
| |
| on: |
| schedule: |
| # ⚠️ 注意:这里是 UTC 时间。北京时间是 UTC+8 |
| # 所以如果你想北京时间上午 9:00 发,这里要填 1:00 (9-8=1) |
| - cron: '0 0 * * *' |
| # 允许手动点击按钮触发测试 |
| workflow_dispatch: |
| |
| jobs: |
| build: |
| runs-on: ubuntu-latest |
| steps: |
| - name: Checkout code |
| uses: actions/checkout@v2 |
| |
| - name: Set up Python |
| uses: actions/setup-python@v2 |
| with: |
| python-version: '3.x' |
| |
| - name: Install dependencies |
| run: pip install requests |
| |
| - name: Run script |
| env: |
| # 这里引用你的 Webhook 密钥 |
| WECOM_WEBHOOK: ${{ secrets.WECOM_WEBHOOK }} |
| run: python main.py
设置密钥
去Setting那里设置密钥。
新建密钥,命名为WECOM_WEBHOOK,值为之前的webhook地址。
测试
回到Actions,点击测试。