前言

之前使用钉钉机器人做持续集成的通知,最近换成飞书作为交流工具。因此,基于钉钉机器人改造了一版飞书机器人。

feishu

feishu 是飞书机器人的 go 实现。支持 Docker、Jenkinsfile、命令行模式,module 模式;支持加签安全设置,支持链式语法创建消息;支持文本(text)、富文本(post)、图片(image)、群名片(share_chat)、消息卡片(interactive) 消息类型。

注:使用钉钉(DingTalk)的小伙伴,可以使用钉钉(DingTalk)版。

文档

特性

  • 支持Docker

  • 支持module

  • 支持加签

    image
  • 文本(text)消息

    image
  • 富文本(post)消息

    image
  • 图片(image)消息

    image
  • 群名片(share_chat)消息

    image
  • 消息卡片(interactive)消息

    image

安装

Docker 安装

docker pull catchzeng/feishu

二进制安装

到 releases 下载相应平台的二进制可执行文件,然后加入到 PATH 环境变量即可。

go get 安装

go get github.com/CatchZeng/feishu

使用方法

配置文件

$/HOME/.feishuconfig.yamlaccess_tokensecret
access_token: "6cxxxx80-xxxx-49e2-ac86-7f378xxxx960"
secret: "k6usknqxxxxazNxxxx443d"

Docker

docker run catchzeng/feishu feishu text -t 6cxxxx80-xxxx-49e2-ac86-7f378xxxx960 -s k6usknqxxxxazNxxxx443d -e "docker test"

Jenkinsfile

pipeline {
    agent {
        docker {
            image 'catchzeng/feishu'
        }
    }
    environment {
        FEISHU_TOKEN = '6cxxxx80-xxxx-49e2-ac86-7f378xxxx960'
        FEISHU_SECRET = 'k6usknqxxxxazNxxxx443d'
    }
    stages {
        stage('notify') {
            steps {
                sh 'feishu post -t ${FEISHU_TOKEN} -s ${FEISHU_SECRET} -i 标题 -e 信息 -r https://makeoptim.com/ -f 链接文本 -a all'
            }
        }
    }
}
post
$ post='{
  "zh_cn": {
    "title": "项目更新通知",
    "content": [
      [
        {
          "tag": "text",
          "text": "项目有更新: "
        },
        {
          "tag": "a",
          "text": "请查看",
          "href": "http://www.example.com/"
        }
      ]
    ]
  }
}
'
$ feishu post -t 6cxxxx80-xxxx-49e2-ac86-7f378xxxx960 -s k6usknqxxxxazNxxxx443d -p $post

作为 module

package main

import (
    "log"

    "github.com/CatchZeng/feishu"
)

func main() {
    token := "6cxxxx80-xxxx-49e2-ac86-7f378xxxx960"
    secret := "k6usknqxxxxazNxxxx443d"

    client := feishu.NewClient(token, secret)

    text := feishu.NewText("文本")
    a := feishu.NewA("链接", "https://www.baidu.com/")
    at := feishu.NewAT("all")
    line := []feishu.PostItem{text, a, at}
    msg := feishu.NewPostMessage()
    msg.SetZHTitle("测试富文本 @all").
        AppendZHContent(line)

    resp, err := client.Send(msg)
    if err != nil {
        log.Print(err)
        return
    }
    log.Print(resp)
}

命令行工具

Demo

Post
feishu post -t 6cxxxx80-xxxx-49e2-ac86-7f378xxxx960 -s k6usknqxxxxazNxxxx443d -i 标题 -e 信息 -r https://makeoptim.com/ -f 链接文本 -a all
Interactive
$ card='{
  "config": {
    "wide_screen_mode": true,
    "enable_forward": true
  },
  "elements": [
    {
      "tag": "div",
      "text": {
        "content": "**西湖**,位于浙江省杭州市西湖区龙井路1号,杭州市区西部,景区总面积49平方千米,汇水面积为21.22平方千米,湖面面积为6.38平方千米。",
        "tag": "lark_md"
      }
    },
    {
      "actions": [
        {
          "tag": "button",
          "text": {
            "content": "更多景点介绍 :玫瑰:",
            "tag": "lark_md"
          },
          "url": "https://www.example.com",
          "type": "default",
          "value": {}
        }
      ],
      "tag": "action"
    }
  ],
  "header": {
    "title": {
      "content": "今日旅游推荐",
      "tag": "plain_text"
    }
  }
}
'
$ feishu interactive -t 6cxxxx80-xxxx-49e2-ac86-7f378xxxx960 -s k6usknqxxxxazNxxxx443d -c $card
card

Help

$ feishu -h
feishu is a command line tool for feishu robot

Usage:
  feishu [command]

Available Commands:
  help        Help about any command
  image       send image message with feishu robot
  interactive send interactive message with feishu robot
  post        send post message with feishu robot
  shareChat   send shareChat message with feishu robot
  text        send text message with feishu robot
  version     feishu version

Flags:
  -t, --access_token string   access_token
  -h, --help                  help for feishu
  -s, --secret string         secret

Use "feishu [command] --help" for more information about a command.