feishu

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package feishu provides a client for sending notifications via Feishu (Lark).

It supports two modes of operation:

  • Webhook mode: Send text and rich-text (post) messages through a Feishu bot webhook URL. This requires only a webhook URL and no app credentials.

  • SDK mode: Send messages and trigger phone urgent calls through the official Lark Open Platform SDK. This requires an App ID, App Secret, and optionally a User Open ID for urgent calls.

Quick Start

For simple webhook-only usage with the global convenience API:

feishu.Init(feishu.Config{
    Webhook: "https://open.feishu.cn/open-apis/bot/v2/hook/xxx",
})

if err := feishu.SendText("Hello from Go!"); err != nil {
    log.Fatal(err)
}

For multi-instance or SDK-based usage:

client := feishu.NewClient("webhook-url", "app-id", "app-secret")
if err := client.SendWebhook(feishu.TextReq{...}); err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized = errors.New("feishu: client not initialized")
	ErrNoUserOpenID   = errors.New("feishu: user_open_id not configured, cannot send urgent message")
)

Common sentinel errors.

Functions

func Init

func Init(cfg Config)

Init initializes the global Feishu bot. It is safe to call multiple times; subsequent calls replace the global instance.

func SendRichText

func SendRichText(title string, content [][]PostElem) error

SendRichText sends a post message with title and content lines via webhook using the global bot.

func SendText

func SendText(text string) error

SendText sends a simple text message via webhook using the global bot.

func SendUrgentText

func SendUrgentText(text string) error

SendUrgentText sends a text message and triggers a phone call notification using the global bot. Falls back to SendText if SDK is not configured.

Types

type BaseReq

type BaseReq struct {
	Timestamp string  `json:"timestamp,omitempty"`
	Sign      string  `json:"sign,omitempty"`
	MsgType   MsgType `json:"msg_type"`
}

type Bot

type Bot struct {
	// contains filtered or unexported fields
}

Bot holds the Feishu client and user configuration. Use NewBot for multi-instance usage or Init for the global convenience API.

func NewBot

func NewBot(cfg Config) *Bot

NewBot creates a new Bot instance from the given Config. Returns nil if neither Webhook nor AppID is configured.

func (*Bot) SendRichText

func (b *Bot) SendRichText(title string, content [][]PostElem) error

SendRichText sends a post message with title and content lines via webhook.

func (*Bot) SendText

func (b *Bot) SendText(text string) error

SendText sends a simple text message via webhook.

func (*Bot) SendUrgentText

func (b *Bot) SendUrgentText(text string) error

SendUrgentText sends a text message to the configured user via SDK, then triggers a phone call notification on that message. Falls back to SendText if SDK is not configured.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps both webhook and official Lark SDK capabilities.

func NewClient

func NewClient(webhook, appID, appSecret string) *Client

NewClient creates a Client with webhook support. If appID and appSecret are provided, the official Lark SDK client is also initialized.

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, receiveIDType, receiveID, msgType, content string) (string, error)

SendMessage sends a message via the official SDK and returns the message_id. receiveIDType: "open_id", "user_id", "union_id", "email", "chat_id"

func (*Client) SendWebhook

func (c *Client) SendWebhook(req interface{}) error

SendWebhook sends a message via the webhook URL (for text/rich-text bot messages).

func (*Client) UrgentPhone

func (c *Client) UrgentPhone(ctx context.Context, messageID string, userIDs []string) error

UrgentPhone triggers a phone call notification for the given message.

type Config

type Config struct {
	Webhook    string // Webhook URL for bot messages
	AppID      string // Lark App ID (optional, for SDK features)
	AppSecret  string // Lark App Secret (optional, for SDK features)
	UserOpenID string // User Open ID (optional, for urgent calls)
}

Config holds Feishu notification settings.

type MsgType

type MsgType string
const (
	MsgTypeText        MsgType = "text"
	MsgTypePost        MsgType = "post"
	MsgTypeInteractive MsgType = "interactive"
)

type PostBody

type PostBody struct {
	ZhCN *PostContent `json:"zh_cn,omitempty"`
	EnUS *PostContent `json:"en_us,omitempty"`
}

type PostContent

type PostContent struct {
	Title   string       `json:"title"`
	Content [][]PostElem `json:"content"`
}

type PostContentWrapper

type PostContentWrapper struct {
	Post PostBody `json:"post"`
}

type PostElem

type PostElem struct {
	Tag      string `json:"tag"`
	Text     string `json:"text,omitempty"`
	Href     string `json:"href,omitempty"`
	UserId   string `json:"user_id,omitempty"`
	UserName string `json:"user_name,omitempty"`
}

func NewAElem

func NewAElem(text, href string) PostElem

func NewAtElem

func NewAtElem(userId string) PostElem

func NewTextElem

func NewTextElem(text string) PostElem

type PostReq

type PostReq struct {
	BaseReq
	Content PostContentWrapper `json:"content"`
}

type TextContent

type TextContent struct {
	Text string `json:"text"`
}

type TextReq

type TextReq struct {
	BaseReq
	Content TextContent `json:"content"`
}

Jump to

Keyboard shortcuts

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