1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-28 08:39:13 +02:00
notify/service/lark
2022-08-04 17:12:33 +02:00
..
common.go feat(service): add lark service 2022-06-05 23:24:33 +08:00
custom_app_test.go feat(service): add lark service 2022-06-05 23:24:33 +08:00
custom_app.go feat(service): add lark service 2022-06-05 23:24:33 +08:00
doc.go feat(service): add lark service 2022-06-05 23:24:33 +08:00
mock_sender.go chore(filestructure): extract mocks folder to follow repo scheme 2022-08-04 17:12:33 +02:00
mock_sendToer.go chore(filestructure): extract mocks folder to follow repo scheme 2022-08-04 17:12:33 +02:00
README.md feat(service): add lark service 2022-06-05 23:24:33 +08:00
webhook_test.go feat(service): add lark service 2022-06-05 23:24:33 +08:00
webhook.go feat(service): add lark service 2022-06-05 23:24:33 +08:00

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/nikoksr/notify"
	"github.com/nikoksr/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/nikoksr/notify"
	"github.com/nikoksr/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")
}