配置 Github Action, 推送企业微信消息
1. 将企业微信的 secret 添加到 Action Secret
打开 hexo-source 仓库设置,在 Secrets
选项中,分别新建 3个 repo secret:
- 名称设为
CORP_ID
, 内容为corpid
的内容
- 名称设为
CORP_SECRET
, 内容为secret
的内容
- 名称设为
AGENT_ID
, 内容为agentid
的内容
2. 修改 wechat-push 代码
以发送 commit log 信息为列, 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import os import json import requests
corp_id = os.environ['CORP_ID'] corp_secret = os.environ['CORP_SECRET'] agent_id = os.environ['AGENT_ID']
def get_access_token(corp_id, corp_secret): resp = requests.get(f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={corp_secret}') js = json.loads(resp.text) print(js) if js["errcode"] == 0: access_token = js["access_token"] expires_in = js["expires_in"] return access_token, expires_in
def wechat_push_text(agent_id, access_token, message): data = { "touser": "@all", "msgtype": 'text', "agentid": agent_id, "text": { "content": message }, "safe": 0, "enable_id_trans": 0, "enable_duplicate_check": 0, "duplicate_check_interval": 1800 } resp = requests.post(f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}', json=data) js = json.loads(resp.text) print(js) if js["errcode"] == 0: return js
with open('commit_log.md', 'r') as f: log = f.read() access_token, expires_in = get_access_token(corp_id, corp_secret) wechat_push_text(agent_id=agent_id, access_token=access_token, message=log)
|
3. 编写 Github Actions 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| name: Wework MSG Auto Push on: [push]
jobs: build: name: Wework MSG Auto Push by GitHub Actions runs-on: ubuntu-latest
steps: - name: 1. setup python... uses: actions/setup-python@v1
- name: 2. send commit log to wework ... env: CORP_ID: ${{ secrets.CORP_ID }} CORP_SECRET: ${{ secrets.CORP_SECRET }} AGENT_ID: ${{ secrets.AGENT_ID }}
run: | echo -e "commit log:\n" > commit_log.md echo -e "$(git log -1 --stat)" >> commit_log.md log=$(cat commit_log.md)
python -m pip install requests python wechat_push.py
|
以上。