fsBotAPI

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 15 Imported by: 0

README

FeiShu-BOT-API

飞书-群机器人-API

安装

go get github.com/electricbubble/feishu-bot-api

使用

package main

import (
	"bytes"
	"log"
	"strings"

	fsBotAPI "github.com/electricbubble/feishu-bot-api"
	"github.com/electricbubble/feishu-bot-api/md"
)

func main() {
	webhook := "https://open.feishu.cn/open-apis/bot/v2/hook/045b07bd-xxxx-xxxx-xxxx-2b49c4af1a2b"
	// 群机器人的 webhook 地址, 可以选择直接使用 👆 地址
	// 也可以选择使用该地址尾部的 36位🆔
	webhook = "045b07bd-xxxx-xxxx-xxxx-2b49c4af1a2b"

	// 开启签名校验时使用
	secretKey := "你的密钥"

	// 开启签名校验
	bot := fsBotAPI.NewBot(webhook, fsBotAPI.WithSecretKey(secretKey))
	// 如果未开启可忽略密钥 👇
	// bot := fsBotAPI.NewBot(webhook)

	{ // 发送普通文本消息
		buf := bytes.NewBufferString("新更新提醒\n")
		buf.WriteString("🤓所有人👉" + fsBotAPI.TextMentionAll() + "\n")
		buf.WriteString("🤔你是谁👉" + fsBotAPI.TextMentionByOpenID("ou_c99c5f35d542efc7ee492afe11af19ef") + "\n")

		err := bot.PushText(buf.String())
		if err != nil {
			log.Fatalln(err)
		}
	}

	{ // 发送富文本消息
		// 可同时设置 3种语言环境
		err := bot.PushPost(
			fsBotAPI.WithPost(fsBotAPI.LangChinese, "🇨🇳我是一个标题",
				fsBotAPI.WithPostElementText("🇨🇳第一行: "),
				fsBotAPI.WithPostElementLink("超链接", "https://www.feishu.cn"),
				fsBotAPI.WithPostElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g"),
				fsBotAPI.WithPostElementText("\n"),
				fsBotAPI.WithPostElementMentionAll(),
				fsBotAPI.WithPostElementText("+"),
				fsBotAPI.WithPostElementMentionByOpenID("ou_c99c5f35d542efc7ee492afe11af19ef"),
				fsBotAPI.WithPostElementImage("img_ecffc3b9-8f14-400f-a014-05eca1a4310g"),
			),
			fsBotAPI.WithPost(fsBotAPI.LangEnglish, "🇺🇸🇬🇧 title",
				fsBotAPI.WithPostElementText("🇺🇸🇬🇧 first line: "),
				fsBotAPI.WithPostElementLink("link", "https://www.feishu.cn"),
				fsBotAPI.WithPostElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g"),
				fsBotAPI.WithPostElementText("\n"),
				fsBotAPI.WithPostElementMentionAll(),
				fsBotAPI.WithPostElementText("+"),
				fsBotAPI.WithPostElementMentionByOpenID("ou_c99c5f35d542efc7ee492afe11af19ef"),
				fsBotAPI.WithPostElementImage("img_ecffc3b9-8f14-400f-a014-05eca1a4310g"),
			),
			fsBotAPI.WithPost(fsBotAPI.LangJapanese, "🇯🇵 タイトル",
				fsBotAPI.WithPostElementText("🇯🇵 1行目: "),
				fsBotAPI.WithPostElementLink("リンク", "https://www.feishu.cn"),
				fsBotAPI.WithPostElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g"),
				fsBotAPI.WithPostElementText("\n"),
				fsBotAPI.WithPostElementMentionAll(),
				fsBotAPI.WithPostElementText("+"),
				fsBotAPI.WithPostElementMentionByOpenID("ou_c99c5f35d542efc7ee492afe11af19ef"),
				fsBotAPI.WithPostElementImage("img_ecffc3b9-8f14-400f-a014-05eca1a4310g"),
			),
		)
		if err != nil {
			log.Fatalln(err)
		}
	}

	{ // 发送卡片消息, 可同时设置 3种语言环境
		mdZhCn := `**title**
~~DEL~~
🙈 看不见的人 👉` + md.MentionByID("ou_c99c5f35d542efc7ee492afe11af19ef") + "\n" +
			md.ColorGreen("这是一个绿色文本") + "\n" +
			md.ColorRed("这是一个红色文本") + "\n" +
			md.ColorGrey("这是一个灰色文本 ")

		mdEnUs := `~~empty~~`

		// 卡片消息可以设置是否允许转发,默认允许转发
		// fsBotAPI.WithCardConfig(fsBotAPI.WithCardConfigEnableForward(false))
		// 也可以将整个卡片作为链接点击, 同时支持多端跳转 👇
		// fsBotAPI.WithCardConfig(fsBotAPI.WithCardConfigEnableForward(false), fsBotAPI.WithCardConfigCardLink(
		// 	"https://www.feishu.cn",
		// 	"https://zlink.toutiao.com/kG12?apk=1",
		// 	"https://zlink.toutiao.com/h2Sw",
		// 	"https://www.feishu.cn/download",
		// ))

		// 第二参数为卡片的基础配置, 默认配置可直接 nil
		err := bot.PushCard(fsBotAPI.BgColorOrange, nil,
			fsBotAPI.WithCard(fsBotAPI.LangChinese, "标题",
				fsBotAPI.WithCardElementPlainText("文本内容"),
				fsBotAPI.WithCardElementHorizontalRule(),
				fsBotAPI.WithCardElementPlainText(strings.Repeat("文本内容2", 20), 2),
				fsBotAPI.WithCardElementHorizontalRule(),
				fsBotAPI.WithCardElementMarkdown(mdZhCn),
				fsBotAPI.WithCardElementHorizontalRule(),
				fsBotAPI.WithCardElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g",
					fsBotAPI.WithCardElementImageTitle("    *图片标题*", true),
					fsBotAPI.WithCardElementImageHover("被发现了"),
				),
				fsBotAPI.WithCardElementNote(
					fsBotAPI.WithCardElementPlainText("**普通文本**"),
					fsBotAPI.WithCardElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g",
						fsBotAPI.WithCardElementImageTitle("    *图片标题*", true),
						fsBotAPI.WithCardElementImageHover("被发现了"),
					),
					fsBotAPI.WithCardElementMarkdown("*test*"),
				),
				fsBotAPI.WithCardElementFields(
					fsBotAPI.WithCardElementField(fsBotAPI.WithCardElementPlainText("列1\nv1"), true),
					fsBotAPI.WithCardElementField(fsBotAPI.WithCardElementMarkdown("**列2**\nv2"), true),
					fsBotAPI.WithCardElementField(fsBotAPI.WithCardElementMarkdown("~无效的信息~"), false),
				),
				fsBotAPI.WithCardElementActions(
					fsBotAPI.WithCardElementAction(fsBotAPI.WithCardElementPlainText("入门必读"), "https://www.feishu.cn/hc/zh-CN/articles/360024881814", fsBotAPI.WithCardElementActionButton(fsBotAPI.ButtonDefault)),
					fsBotAPI.WithCardElementAction(fsBotAPI.WithCardElementPlainText("快速习惯飞书️"), "https://www.feishu.cn/hc/zh-CN/categories-detail?category-id=7018450035717259265", fsBotAPI.WithCardElementActionButton(fsBotAPI.ButtonPrimary)),
					fsBotAPI.WithCardElementAction(
						fsBotAPI.WithCardElementMarkdown("**多端跳转下载**"), "", fsBotAPI.WithCardElementActionButton(fsBotAPI.ButtonDanger), fsBotAPI.WithCardElementActionMultiURL(
							"https://www.feishu.cn",
							"https://zlink.toutiao.com/kG12?apk=1",
							"https://zlink.toutiao.com/h2Sw",
							"https://www.feishu.cn/download",
						),
					),
				),

				fsBotAPI.WithCardElementMarkdown("*TEST*", fsBotAPI.WithCardExtraElementImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g",
					fsBotAPI.WithCardElementImageTitle("    *图片标题*", true),
					fsBotAPI.WithCardElementImageHover("被发现了"),
				)),
			),

			fsBotAPI.WithCard(fsBotAPI.LangEnglish, "title",
				fsBotAPI.WithCardElementMarkdown(mdEnUs),
			),
		)
		if err != nil {
			log.Fatalln(err)
		}
	}

	{ // 发送图片消息
		err := bot.PushImage("img_7ea74629-9191-4176-998c-2e603c9c5e8g")
		if err != nil {
			log.Fatalln(err)
		}
	}

	{ // 发送群名片
		err := bot.PushShareChat("oc_f5b1a7eb27ae2c7b6adc2a74faf339ff")
		if err != nil {
			log.Fatalln(err)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenMsgCard added in v0.1.1

func GenMsgCard(bgColor CardTitleBgColor, cfg CardConfig, c Card, more ...Card) map[string]interface{}

func TextMentionAll added in v0.4.0

func TextMentionAll() string

TextMentionAll @所有人

func TextMentionByOpenID added in v0.4.0

func TextMentionByOpenID(id string, name ...string) string

TextMentionByOpenID @单个用户

如果 Open ID 无效,则取 name 展示

https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN#e1cdee9f

Types

type Bot

type Bot interface {
	PushText(content string) error
	PushPost(p Post, ps ...Post) error
	PushCard(bgColor CardTitleBgColor, cfg CardConfig, c Card, more ...Card) error
	PushImage(imageKey string) error
	PushShareChat(chatID string) error
}

func NewBot

func NewBot(wh string, opts ...BotOption) Bot

type BotOption

type BotOption func(*bot)

func WithBodyCodec added in v0.3.0

func WithBodyCodec(codec xhttpclient.BodyCodec) BotOption

func WithDebugOutput added in v0.3.0

func WithDebugOutput() BotOption

func WithSecretKey

func WithSecretKey(key string) BotOption

type Card

type Card func() i18nCard

func WithCard

func WithCard(lang Language, title string, elem CardElement, elements ...CardElement) Card

WithCard 卡片消息, 可指定语言环境

支持元素如下:
普通文本: WithCardElementPlainText
MarkDown: WithCardElementMarkdown
可并排字段: WithCardElementFields
按钮: WithCardElementActions
分割线: WithCardElementHorizontalRule
图片: WithCardElementImage
备注: WithCardElementNote

type CardConfig

type CardConfig func() cardConfig

func WithCardConfig

func WithCardConfig(opt CardConfigOption, opts ...CardConfigOption) CardConfig

WithCardConfig 卡片消息的属性配置

  • 是否允许卡片消息被转发, 默认值: true WithCardConfigEnableForward
  • 是否为共享卡片, 默认值: false WithCardConfigEnableUpdateMulti
  • 设置卡片跳转链接 WithCardConfigCardLink

type CardConfigOption

type CardConfigOption func(*cardConfig)
func WithCardConfigCardLink(url, android, ios, pc string) CardConfigOption

WithCardConfigCardLink 设置卡片的多端跳转链接

func WithCardConfigEnableForward

func WithCardConfigEnableForward(b bool) CardConfigOption

WithCardConfigEnableForward 设置是否允许卡片被转发, 默认允许转发

func WithCardConfigEnableUpdateMulti

func WithCardConfigEnableUpdateMulti(b bool) CardConfigOption

WithCardConfigEnableUpdateMulti 设置是否为共享卡片, 默认不共享

true: 是共享卡片,也即更新卡片的内容对所有收到这张卡片的人员可见。
false: 是独享卡片,仅操作用户可见卡片的更新内容。

type CardElemImageOption

type CardElemImageOption func() (key string, v interface{})

func WithCardElementImageCompactWidth

func WithCardElementImageCompactWidth(b bool) CardElemImageOption

WithCardElementImageCompactWidth 是否展示为紧凑型的图片

默认为 false
若配置为 true,则展示最大宽度为278px的紧凑型图片

func WithCardElementImageCustomWidth

func WithCardElementImageCustomWidth(w int) CardElemImageOption

WithCardElementImageCustomWidth 自定义图片的最大展示宽度

默认展示宽度撑满卡片的通栏图片
可在 278px~580px 范围内指定最大展示宽度
在飞书4.0以上版本生效

func WithCardElementImageHover

func WithCardElementImageHover(text string) CardElemImageOption

WithCardElementImageHover hover 图片时弹出的Tips文案

仅支持普通文本格式

func WithCardElementImageMode

func WithCardElementImageMode(mode ImageMode) CardElemImageOption

WithCardElementImageMode 图片显示模式

默认 居中裁剪模式
ImageModeCropCenter:居中裁剪模式,对长图会限高,并居中裁剪后展示
ImageModeFitHorizontal:平铺模式,宽度撑满卡片完整展示上传的图片。该属性会覆盖custom_width 属性

func WithCardElementImagePreview

func WithCardElementImagePreview(b bool) CardElemImageOption

WithCardElementImagePreview 点击后是否放大图片

缺省为true
在配置 card_link 后可设置为false,使用户点击卡片上的图片也能响应card_link链接跳转

func WithCardElementImageTitle

func WithCardElementImageTitle(text string, md ...bool) CardElemImageOption

WithCardElementImageTitle 图片的标题

默认普通文本格式
`md` 传入 `true`, 可支持 Markdown

type CardElement

type CardElement func(isEmbedded bool) interface{}

func WithCardElementActions

func WithCardElementActions(act CardElementAction, actions ...CardElementAction) CardElement

WithCardElementActions 按钮, 可指定但固定跳转, 或多端跳转

func WithCardElementFields

func WithCardElementFields(f CardElementField, fields ...CardElementField) CardElement

WithCardElementFields 能并排布局的字段元素

支持元素:
- WithCardElementPlainText
- WithCardElementMarkdown

func WithCardElementHorizontalRule

func WithCardElementHorizontalRule() CardElement

WithCardElementHorizontalRule 分割线

func WithCardElementImage

func WithCardElementImage(imgKey string, opts ...CardElemImageOption) CardElement

func WithCardElementMarkdown

func WithCardElementMarkdown(md string, extra ...CardExtraElement) CardElement

WithCardElementMarkdown MarkDown 语法展示文本内容

语法仅支持部分, 语法详情: https://open.feishu.cn/document/ukTMukTMukTM/uADOwUjLwgDM14CM4ATN

func WithCardElementNote

func WithCardElementNote(elem CardElement, elements ...CardElement) CardElement

WithCardElementNote 卡片的备注信息

支持元素:
- WithCardElementPlainText
- WithCardElementMarkdown
- WithCardElementImage

func WithCardElementPlainText

func WithCardElementPlainText(text string, lines ...int) CardElement

WithCardElementPlainText 普通文本内容

lines: 内容显示行数

type CardElementAction

type CardElementAction func() interface{}

func WithCardElementAction

func WithCardElementAction(elem CardElement, url string, opts ...CardElementActionOption) CardElementAction

type CardElementActionOption

type CardElementActionOption func() (key string, v interface{})

func WithCardElementActionButton

func WithCardElementActionButton(btn ElementButton) CardElementActionOption

func WithCardElementActionMultiURL

func WithCardElementActionMultiURL(url, android, ios, pc string) CardElementActionOption

type CardElementField

type CardElementField func() interface{}

func WithCardElementField

func WithCardElementField(elem CardElement, isShort bool) CardElementField

type CardExtraElement

type CardExtraElement func() (key string, v interface{})

func WithCardExtraElementImage

func WithCardExtraElementImage(imgKey string, opts ...CardElemImageOption) CardExtraElement

type CardTitleBgColor

type CardTitleBgColor string

CardTitleBgColor 标题背景色

最佳实践:https://open.feishu.cn/document/ukTMukTMukTM/ukTNwUjL5UDM14SO1ATN#8239feff
- 绿色(Green)代表完成/成功
- 橙色(Orange)代表警告/警示
- 红色(Red)代表错误/异常
- 灰色(Grey)代表失效
const (
	BgColorDefault   CardTitleBgColor = ""
	BgColorBlue      CardTitleBgColor = "blue"
	BgColorWathet    CardTitleBgColor = "wathet"
	BgColorTurquoise CardTitleBgColor = "turquoise"
	BgColorGreen     CardTitleBgColor = "green"
	BgColorYellow    CardTitleBgColor = "yellow"
	BgColorOrange    CardTitleBgColor = "orange"
	BgColorRed       CardTitleBgColor = "red"
	BgColorCarmine   CardTitleBgColor = "carmine"
	BgColorViolet    CardTitleBgColor = "violet"
	BgColorPurple    CardTitleBgColor = "purple"
	BgColorIndigo    CardTitleBgColor = "indigo"
	BgColorGrey      CardTitleBgColor = "grey"
)

type ElementButton

type ElementButton string
const (
	ButtonDefault ElementButton = "default"
	ButtonPrimary ElementButton = "primary"
	ButtonDanger  ElementButton = "danger"
)

type ImageMode

type ImageMode string
const (
	ImageModeCropCenter    ImageMode = "crop_center"
	ImageModeFitHorizontal ImageMode = "fit_horizontal"
)

type Language

type Language string
const (
	LangChinese  Language = "zh_cn"
	LangEnglish  Language = "en_us"
	LangJapanese Language = "ja_jp"
)

type Post

type Post func() i18nPost

func WithPost

func WithPost(lang Language, title string, elements ...PostElement) Post

WithPost 富文本消息, 可指定语言环境

支持元素如下:
普通文本: WithPostElementText
文字超链接: WithPostElementLink
图片: WithPostElementImage
@所有人: WithPostElementMentionAll
@指定用户(OpenID): WithPostElementMentionByOpenID

type PostElement

type PostElement func() postElement

func WithPostElementImage

func WithPostElementImage(imgKey string) PostElement

WithPostElementImage 富文本消息的图片元素

func WithPostElementLink(text, href string) PostElement

WithPostElementLink 富文本消息的文字超链接元素

func WithPostElementMentionAll

func WithPostElementMentionAll() PostElement

WithPostElementMentionAll 富文本消息的 @所有人

func WithPostElementMentionByOpenID

func WithPostElementMentionByOpenID(id string, name ...string) PostElement

WithPostElementMentionByOpenID 富文本消息的 @用户

Open ID 必须是有效值,否则仅显示 `@` 符号(实际效果不同于 PushText 时会显示 name)

https://open.feishu.cn/document/ugTN1YjL4UTN24CO1UjN/uUzN1YjL1cTN24SN3UjN?from=mcb#acc98e1b

func WithPostElementText

func WithPostElementText(text string, isUnescape ...bool) PostElement

WithPostElementText 富文本消息的文字元素

isUnescape 表示是不是 unescape 解码,默认为 false ,不用可以不填

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL