Skip to content

Instantly share code, notes, and snippets.

@hzbd
Last active January 11, 2022 13:24
Show Gist options
  • Save hzbd/d7ce83fe9f58dacbc10aba7397986364 to your computer and use it in GitHub Desktop.
Save hzbd/d7ce83fe9f58dacbc10aba7397986364 to your computer and use it in GitHub Desktop.
github action dingtalk bot
# -*- coding:utf-8 -*-
#
# Copyright (c) 2021 六二三
# Licensed under the MIT License.
# All rights reserved.
#
#---------------------------------+
# * PyEnv: Python3
# * Things to do before starting:
# 1.add action secrect `DINGTALK_ACCESS_TOKEN` & `DINGTALK_SECRET`.
# 2.add job action step.
#---------------------------------+
# - name: get commit message
# env:
# ACTIONS_ALLOW_UNSECURE_COMMANDS: "true"
# run: |
# echo ::set-env name=commit_message::$(git log --format=%B -n 1 ${{ github.event.after }})
# - name: Sending Build Status
# if: always()
# env:
# DINGTALK_ACCESS_TOKEN: ${{ secrets.DINGTALK_ACCESS_TOKEN }}
# DINGTALK_SECRET: ${{ secrets.DINGTALK_SECRET }}
# JOB_STATE: ${{ job.status }}
# RUNNER_OS: ${{ runner.os }}
# GITHUB_REF: ${{ github.ref }}
# GITHUB_RUN_ID: ${{ github.run_id }}
# GITHUB_REPOSITORY: ${{ github.repository }}
# GITHUB_WORKFLOW: ${{ env.GITHUB_WORKFLOW }}
# COMMIT: ${{ env.commit_message }}
# run: |
# python3 dingtalkkit.py
#---------------------------------+
#
import time
import hmac
import hashlib
import base64
import urllib
import requests
import os
def dingtalk_send(token, secret, json_data=""):
url = "https://oapi.dingtalk.com/robot/send"
ts = int(round(time.time() * 1000))
str = '{}\n{}'.format(ts, secret)
hmc = hmac.new(secret.encode('utf-8'), str.encode('utf-8'), digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmc))
endpoint = '{}?access_token={}&timestamp={}&sign={}'.format(url, token, ts, sign)
headers = {'Content-Type': 'application/json'}
resp = requests.post(url=endpoint, headers=headers, json=json_data)
print(resp.text)
if __name__ == '__main__':
token = os.getenv('DINGTALK_ACCESS_TOKEN')
secret = os.getenv('DINGTALK_SECRET')
job_status = os.getenv('JOB_STATE')
github_ref = os.getenv('GITHUB_REF')
github_run_id = os.getenv('GITHUB_RUN_ID')
runner_os = os.getenv('RUNNER_OS')
github_repository = os.getenv('GITHUB_REPOSITORY')
github_workflow = os.getenv('GITHUB_WORKFLOW')
git_commit = os.getenv('COMMIT')
if job_status == 'success':
color = "#1ce43f"
elif job_status == 'cancelled':
color = "#c1c1c1"
else:
color = "#ff4a83"
message = {
"msgtype": "markdown",
"markdown": {
"title": github_workflow,
"text": "### " + github_workflow + " #" + github_run_id + "\n\n> status: **<font color=" + color + ">" + str(job_status).upper() + "</font>** \n\n> head ref: **" + github_ref + "** \n\n> runner os: **" + runner_os + "** \n\n> actions commit: [`" + git_commit + "`](https://github.com/" + github_repository + "/actions)\n\n"
}
}
dingtalk_send(token, secret, json_data=message)
@hzbd
Copy link
Author

hzbd commented Jan 28, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment