Home

Awesome

feishu

Go codecov Go Report Card Release GoDoc

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

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

文档

飞书文档

特性

<img src="https://p6-hera.byteimg.com/tos-cn-i-jbbdkfciu3/fb5e1dd375684dd2b9b6037d86f862b0~tplv-jbbdkfciu3-image:0:0.image" width = 50% /> <img src="https://p1-hera.byteimg.com/tos-cn-i-jbbdkfciu3/c9c86efea1754e269dbdc5517b4d958a~tplv-jbbdkfciu3-image:0:0.image" width = 50% /> <img src="https://p3-hera.byteimg.com/tos-cn-i-jbbdkfciu3/661d8ee4446c47bca5ac61bfb2ef1a6f~tplv-jbbdkfciu3-image:0:0.image" width = 50% /> <img src="https://p1-hera.byteimg.com/tos-cn-i-jbbdkfciu3/5607aa65324e4e14bd94192ba81fe0b3~tplv-jbbdkfciu3-image:0:0.image" width = 50% /> <img src="https://p9-hera.byteimg.com/tos-cn-i-jbbdkfciu3/ba60b1c2835a4950926bb86687e183a8~tplv-jbbdkfciu3-image:0:0.image" width = 50% /> <img src="https://p6-hera.byteimg.com/tos-cn-i-jbbdkfciu3/4bf5072377cf4c02990ce28731634e6a~tplv-jbbdkfciu3-image:0:0.image" width = 50% />

安装

Docker 安装

docker pull catchzeng/feishu

二进制安装

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

go install 安装

# Go 1.16+
go install github.com/CatchZeng/feishu@v1.3.2

# Go version < 1.16
go get -u github.com/CatchZeng/feishu@v1.3.2

使用方法

配置文件

可以在 $/HOME/.feishu 下创建 config.yaml 填入 access_tokensecret 默认值。

access_token: "6cxxxx80-xxxx-49e2-ac86-7f378xxxx960"
secret: "k6usknqxxxxazNxxxx443d"

环境变量

$ export ACCESS_TOKEN="6cxxxx80-xxxx-49e2-ac86-7f378xxxx960"
$ export SECRET="k6usknqxxxxazNxxxx443d"
$ feishu post -i 测试 -e 测试信息 -r https://makeblock.com/ -f 链接文本 -a all

你也可以为环境变量设置一个前缀

$ export FEISHU_ENV_PREFIX="FEISHU_"
$ export FEISHU_ACCESS_TOKEN="6cxxxx80-xxxx-49e2-ac86-7f378xxxx960"
$ export FEISHU_SECRET="k6usknqxxxxazNxxxx443d"
$ feishu post -i 测试 -e 测试信息 -r https://makeblock.com/ -f 链接文本 -a all

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 参数,直接将 post json string 传入,做到更灵活的配置。如下所示:

$ 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

go get github.com/CatchZeng/feishu
package main

import (
	"log"

	"github.com/CatchZeng/feishu/pkg/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)

	req, 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
❯ feishu post -t 6cxxxx80-xxxx-49e2-ac86-7f378xxxx960 -s k6usknqxxxxazNxxxx443d -i 标题 -e 信息 -r https://makeoptim.com/ -f 链接文本 -a all -D
{"content":{"post":{"zh_cn":{"content":[[{"tag":"text","text":"信息"},{"tag":"a","text":"链接文本","href":"https://makeoptim.com/"},{"tag":"at","user_id":"all"}]],"title":"标题"}}},"msg_type":"post","sign":"HR7kQhgapScmp/2bfLWdYmC7C6pUV3C/pQUiS3OQDIA=","timestamp":"1642561080"}

-D 参数:打印发送的消息内容

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 可以使用飞书可视化搭建工具 cardbuilder 自动生成。

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
  -D, --debug                 debug
  -h, --help                  help for feishu
  -s, --secret string         secret

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

Stargazers

Stargazers over time