Files
dingtalk-bot/README.md

7.1 KiB
Raw Permalink Blame History

钉钉消息推送 Gitea Action

一个用于向钉钉群发送消息的 Gitea Action支持文本、Markdown、链接、ActionCard 和 FeedCard 消息类型。

快速开始

1. 获取钉钉 Webhook URL

  1. 在钉钉群中添加"自定义机器人"
  2. 复制生成的 Webhook URL
  3. 配置安全设置

2. 在 Gitea 仓库中使用

创建 .gitea/workflows/dingtalk-notify.yml 文件:

name: DingTalk Notification
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - name: Send DingTalk Message
        uses: actions/dingtalk-bot@v0.1
        with:
          webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
          message_type: 'text'
          content: '🎉 代码已推送到 ${{ github.repository }} 仓库!'

输入参数

参数名 必需 默认值 描述
webhook_url - 钉钉机器人 Webhook URL
message_type text 消息类型:textmarkdownlinkactionCardfeedCard
content - 消息内容
title - 消息标题markdown 和 link 类型必需)
at_mobiles - @的手机号,多个用逗号分隔
at_all false 是否@所有人
link_url - 链接地址link 类型必需)
pic_url - 图片地址link 类型可选)
btn_orientation 0 actionCard 按钮排列方向:0竖直、1水平
single_title - actionCard 单按钮标题(整体跳转)
single_url - actionCard 单按钮链接(整体跳转)
buttons - actionCard 多按钮配置JSON 数组),每项 { title, actionURL }
feed_links - feedCard 链接列表JSON 数组),每项 { title, messageURL, picURL }

输出参数

参数名 描述
success 是否发送成功
message_id 消息ID如果可用
error_message 错误信息(如果失败)

使用示例

文本消息

- name: Send Text Message
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'text'
    content: '✅ 构建成功!提交者:${{ github.actor }}'
    at_mobiles: '13800138000,13900139000'

Markdown 消息

- name: Send Markdown Message
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'markdown'
    title: '构建报告'
    content: |
      ## 🎉 构建成功

      **仓库**: ${{ github.repository }}
      **分支**: ${{ github.ref_name }}
      **提交者**: ${{ github.actor }}
      **提交信息**: ${{ github.event.head_commit.message }}

      [查看详情](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
    at_all: true

链接消息

- name: Send Link Message
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'link'
    title: '新版本发布'
    content: '项目已发布新版本,点击查看详情'
    link_url: 'https://github.com/${{ github.repository }}/releases/latest'
    pic_url: 'https://avatars.githubusercontent.com/u/${{ github.actor_id }}'

ActionCard整体跳转单按钮

- name: Send ActionCard Single Button
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'actionCard'
    title: '乔布斯 20 年'
    content: |
      ![乔布斯](https://example.com/steve.jpg)

      乔布斯 20 年
    btn_orientation: '0'
    single_title: '阅读全文'
    single_url: 'https://www.dingtalk.com/'

ActionCard独立跳转多按钮

- name: Send ActionCard Multiple Buttons
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'actionCard'
    title: '构建结果'
    content: |
      ## 本次构建完成
      - 版本:${{ github.ref_name }}
      - 提交者:${{ github.actor }}
    btn_orientation: '1'
    buttons: >-
      [
        { "title": "查看日志", "actionURL": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" },
        { "title": "查看发布页", "actionURL": "https://github.com/${{ github.repository }}/releases/latest" }
      ]

FeedCard图文链接

- name: Send FeedCard
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'feedCard'
    feed_links: >-
      [
        {
          "title": "时代的火车向前开",
          "messageURL": "https://www.dingtalk.com/",
          "picURL": "https://static.dingtalk.com/media/xxx1.jpg"
        },
        {
          "title": "正当防卫体育大战",
          "messageURL": "https://www.dingtalk.com/",
          "picURL": "https://static.dingtalk.com/media/xxx2.jpg"
        }
      ]

高级配置

条件发送

# 构建失败时发送通知
- name: Send Notification on Failure
  if: failure()
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'text'
    content: '❌ 构建失败!请检查代码。'
    at_all: true

# 构建成功时发送通知
- name: Send Notification on Success
  if: success()
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ secrets.DINGTALK_WEBHOOK_URL }}
    message_type: 'markdown'
    title: '✅ 构建成功'
    content: |
      ## 构建完成

      **项目**: ${{ github.repository }}
      **分支**: ${{ github.ref_name }}
      **提交者**: ${{ github.actor }}
      **提交信息**: ${{ github.event.head_commit.message }}

      [查看详情](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
    at_mobiles: ['${{ secrets.NOTIFY_MOBILE }}']

多环境配置

- name: Send to Different Groups
  uses: actions/dingtalk-bot@v0.1
  with:
    webhook_url: ${{ github.ref == 'refs/heads/main' && secrets.PROD_DINGTALK_WEBHOOK || secrets.DEV_DINGTALK_WEBHOOK }}
    message_type: 'text'
    content: '部署到 ${{ github.ref == "refs/heads/main" && "生产环境" || "测试环境" }} 完成'