Giter Site home page Giter Site logo

catchzeng / feishu Goto Github PK

View Code? Open in Web Editor NEW
86.0 2.0 18.0 66 KB

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

License: MIT License

Makefile 8.96% Go 87.73% Dockerfile 1.01% Shell 2.30%

feishu's Introduction

feishu

Go codecov Go Report Card Release GoDoc

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

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

文档

飞书文档

特性

  • 文本(text)消息

  • 富文本(post)消息

  • 图片(image)消息

  • 群名片(share_chat)消息

  • 消息卡片(interactive)消息

安装

Docker 安装

docker pull catchzeng/feishu

二进制安装

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

go install 安装

# Go 1.16+
go install github.com/CatchZeng/[email protected]

# Go version < 1.16
go get -u github.com/CatchZeng/[email protected]

使用方法

配置文件

可以在 $/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

feishu's People

Contributors

catchzeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

feishu's Issues

Cannot import as package

I copied the example code from Readme.md, when compiling with go mod, it hints:

main.go:6:2: import "github.com/CatchZeng/feishu" is a program, not an importable package

The line 6 is the import sentence:

import "github.com/CatchZeng/feishu"

How can I fix this?

readme文档中的示例不正确

`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)

}`

其中
resp, err := client.Send(msg)

Send方法返回的三个返回值,只有两个返回值进行接收,建议改为
req, resp, err := client.Send(msg)

是否可以支持proxy代理方式

非常感谢大佬的分享,因为应用是内网部署的不能直接访问公网,请问是否可以支持通过proxy代理的功能,比如squid

代码中只映射了错误请求的结构体

代码中只映射了错误请求的结构体

// Response response struct
type Response struct {
Code int64 json:"code"
Msg string json:"msg"
}

如请求成功,返回体为:

{
"Extra": null,
"StatusCode": 0,
"StatusMessage": "success"
}
如请求体格式错误,返回体如下。请检查:

请求体内容格式是否与各消息类型的示例代码一致
请求体大小不能超过20k
{
"code": 9499,
"msg": "Bad Request",
"data": {}
}

参考文档
https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN#f62e72d5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.