lark

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2023 License: MIT Imports: 7 Imported by: 3

README

Lark

Prerequisites

Depending on your requirements, you'll need either a custom app or a Lark group chat webhook. The latter is easier to set up, but can only send messages to the group it is in. You may refer to the doc here to set up a webhook bot, and the doc here to set up a custom app.

Usage

Webhook

For webhook bots, we only need the webhook URL, which might look something like https://open.feishu.cn/open-apis/bot/v2/hook/xxx. Note that there is no method to configure receivers, because the webhook bot can only send messages to the group in which it was created.

package main

import (
	"context"
	"log"

	"github.com/casdoor/notify"
	"github.com/casdoor/notify/service/lark"
)

// Replace this with your own webhook URL.
const webHookURL = "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"

func main() {
	larkWebhookSvc := lark.NewWebhookService(webHookURL)

	notifier := notify.New()
	notifier.UseServices(larkWebhookSvc)

	if err := notifier.Send(context.Background(), "subject", "message"); err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("notification sent")
}
Custom App

For custom apps, we need to pass in the App ID and App Secret when creating a new notification service. When adding receivers, the type of the receiver ID must be specified, as shown in the example below. You may refer to the section entitled "Query parameters" in the doc here for more information about the different ID types.

package main

import (
	"context"
	"log"

	"github.com/casdoor/notify"
	"github.com/casdoor/notify/service/lark"
)

// Replace these with the credentials from your custom app.
const (
	appId     = "xxx"
	appSecret = "xxx"
)

func main() {
	larkCustomAppService := lark.NewCustomAppService(appId, appSecret)

	// Lark implements five types of receiver IDs. You'll need to specify the
	// type using the respective helper functions when adding them as receivers.
	larkCustomAppService.AddReceivers(
		lark.OpenID("xxx"),
		lark.UserID("xxx"),
		lark.UnionID("xxx"),
		lark.Email("xyz@example.com"),
		lark.ChatID("xxx"),
	)

	notifier := notify.New()
	notifier.UseServices(larkCustomAppService)

	if err := notifier.Send(context.Background(), "subject", "message"); err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("notification sent")
}

Documentation

Overview

Package lark provides message notification integration for Lark. Two kinds of bots on Lark are supported -- webhooks and custom apps. For information on webhook bots, see https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/bot-v3/use-custom-bots-in-a-group, and for info on custom apps, see https://open.larksuite.com/document/home/develop-a-bot-in-5-minutes/create-an-app.

Usage:

package main

import (
  "context"
  "log"

  "github.com/casdoor/notify"
  "github.com/casdoor/notify/service/lark"
)

const (
  webhookURL = "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
  appId      = "xxx"
  appSecret  = "xxx"
)

func main() {
  // Two types of services are available depending on your requirements.
  larkWebhookService := lark.NewWebhookService(webhookURL)
  larkCustomAppService := lark.NewCustomAppService(appId, appSecret)

  // Lark implements five types of receiver IDs. You'll need to specify the
  // type using the respective helper functions when adding them as receivers
  // for the custom app service.
  larkCustomAppService.AddReceivers(
    lark.OpenID("xxx"),
    lark.UserID("xxx"),
    lark.UnionID("xxx"),
    lark.Email("xyz@example.com"),
    lark.ChatID("xxx"),
  )

  notifier := notify.New()
  notifier.UseServices(larkWebhookService, larkCustomAppService)

  if err := notifier.Send(context.Background(), "subject", "message"); err != nil {
    log.Fatalf("notifier.Send() failed: %s", err.Error())
  }

  log.Println("notification sent")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomAppService

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

CustomAppService is a Lark notify service using a Lark custom app.

func NewCustomAppService

func NewCustomAppService(appID, appSecret string) *CustomAppService

NewCustomAppService returns a new instance of a Lark notify service using a Lark custom app.

func (*CustomAppService) AddReceivers

func (c *CustomAppService) AddReceivers(ids ...*ReceiverID)

AddReceivers adds recipients to future notifications. There are five different types of receiver IDs available in Lark and they must be specified here. For example:

larkService.AddReceivers(
  lark.OpenID("ou_c99c5f35d542efc7ee492afe11af19ef"),
  lark.UserID("8335aga2"),
  lark.UnionID("on_cad4860e7af114fb4ff6c5d496d1dd76"),
  lark.Email("xyz@example.com"),
  lark.ChatID("oc_a0553eda9014c201e6969b478895c230"),
)

func (*CustomAppService) Send

func (c *CustomAppService) Send(ctx context.Context, subject, message string) error

Send takes a message subject and a message body and sends them to all previously registered recipient IDs.

type ReceiverID

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

ReceiverID encapsulates a receiver ID and its type in Lark.

func ChatID

func ChatID(s string) *ReceiverID

ChatID specifies an ID as a Lark Chat ID.

func Email

func Email(s string) *ReceiverID

Email specifies a receiver ID as an email.

func OpenID

func OpenID(s string) *ReceiverID

OpenID specifies an ID as a Lark Open ID.

func UnionID

func UnionID(s string) *ReceiverID

UnionID specifies an ID as a Lark Union ID.

func UserID

func UserID(s string) *ReceiverID

UserID specifies an ID as a Lark User ID.

type WebhookService

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

WebhookService is a Notify service that uses a Lark webhook to send messages.

func NewWebhookService

func NewWebhookService(webhookURL string) *WebhookService

NewWebhookService returns a new instance of a Lark notify service using a Lark group chat webhook. Note that this service does not take any notification receivers because it can only push messages to the group chat it belongs to.

func (*WebhookService) Send

func (w *WebhookService) Send(_ context.Context, subject, message string) error

Send sends the message subject and body to the group chat.

Jump to

Keyboard shortcuts

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