larkhertz

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 10 Imported by: 0

README

Lark Hertz

build codecov

Hertz middleware for go-lark.

Middlewares

  • LarkChallengeHandler: URL challenge for general events and card callback
  • LarkEventHandler: Event v2 (schema 2.0)
  • LarkCardHandler: Card callback
  • LarkMessageHandler: (Legacy) Incoming message event (schema 1.0)

Installation

go get -u github.com/go-lark/lark-hertz

Usage

package main

import (
	"context"
	"fmt"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	larkhertz "github.com/go-lark/lark-hertz"
)

func main() {
	r := server.Default()
	middleware := larkhertz.NewLarkMiddleware()

    // lark server challenge
	r.Use(middleware.LarkChallengeHandler())

    // all supported events
    eventGroup := r.Group("/event")
    {
        eventGroup.Use(middleware.LarkEventHandler())
        eventGroup.POST("/", func(c context.Context, ctx *app.RequestContext) {
            if event, ok := middleware.GetEvent(e); ok { // => returns `*lark.EventV2`
            }
        })
    }

    // card callback only
    cardGroup := r.Group("/card")
    {
        cardGroup.Use(middleware.LarkCardHandler())
        cardGroup.POST("/callback", func(c context.Context, ctx *app.RequestContext) {
            if card, ok := middleware.GetCardCallback(c); ok { // => returns `*lark.EventCardCallback`
            }
        })
    }

	r.Spin()
}
Event v2

The default mode is event v1. However, Lark has provided event v2 and it applied automatically to newly created bots.

To enable EventV2, we use LarkEventHandler instead of LarkMessageHandler:

r.Use(middleware.LarkEventHandler())

Get the event (e.g. Message):

r.POST("/", func(c context.Context, ctx *app.RequestContext) {
    event, ok = middleware.GetEvent(ctx)
    if evt, ok := middleware.GetEvent(c); ok { // => GetEvent instead of GetMessage
        if evt.Header.EventType == lark.EventTypeMessageReceived {
            if msg, err := evt.GetMessageReceived(); err == nil {
                fmt.Println(msg.Message.Content)
            }
            // you may have to parse other events
        }
    }
})
Card Callback

We may also setup callback for card actions (e.g. button). The URL challenge part is the same.

We may use LarkCardHandler to handle the actions:

r.Use(middleware.LarkCardHandler())
r.POST("/", func(c context.Context, ctx *app.RequestContext) {
    if event, ok = middleware.GetCardCallback(ctx); ok {
    }
})
Token Verification
middleware.WithTokenVerfication("asodjiaoijoi121iuhiaud")
Encryption

Notice: encryption is not available for card callback, due to restriction from Lark Open Platform.

middleware.WithEncryption("1231asda")
URL Binding

Only bind specific URL for events:

middleware.BindURLPrefix("/abc")

About

Copyright (c) go-lark Developers, 2018-2024.

Documentation

Overview

Package larkhertz is Hertz middleware for go-lark

Index

Constants

View Source
const DefaultLarkMessageKey = "go-lark-message"

DefaultLarkMessageKey compat legacy versions not use in this repo right now

Variables

This section is empty.

Functions

This section is empty.

Types

type LarkMiddleware

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

LarkMiddleware .

func NewLarkMiddleware

func NewLarkMiddleware() *LarkMiddleware

NewLarkMiddleware .

func (*LarkMiddleware) BindURLPrefix

func (opt *LarkMiddleware) BindURLPrefix(prefix string) *LarkMiddleware

BindURLPrefix .

func (LarkMiddleware) GetCardCallback added in v1.1.0

func (opt LarkMiddleware) GetCardCallback(c *app.RequestContext) (*lark.EventCardCallback, bool)

GetCardCallback from gin context

func (*LarkMiddleware) GetEvent

func (opt *LarkMiddleware) GetEvent(c *app.RequestContext) (*lark.EventV2, bool)

GetEvent should call GetEvent if you're using EventV2

func (*LarkMiddleware) GetMessage

func (opt *LarkMiddleware) GetMessage(c *app.RequestContext) (msg *lark.EventMessage, ok bool)

GetMessage from hertz context

func (LarkMiddleware) LarkCardHandler added in v1.1.0

func (opt LarkMiddleware) LarkCardHandler() app.HandlerFunc

LarkCardHandler card callback handler Encryption is automatically ignored, because it's not supported officially

func (*LarkMiddleware) LarkChallengeHandler

func (opt *LarkMiddleware) LarkChallengeHandler() app.HandlerFunc

LarkChallengeHandler Lark challenge handler

func (*LarkMiddleware) LarkEventHandler

func (opt *LarkMiddleware) LarkEventHandler() app.HandlerFunc

LarkEventHandler handle lark event v2

func (*LarkMiddleware) LarkMessageHandler

func (opt *LarkMiddleware) LarkMessageHandler() app.HandlerFunc

LarkMessageHandler Lark message handler

func (*LarkMiddleware) SetCardKey added in v1.1.0

func (opt *LarkMiddleware) SetCardKey(key string) *LarkMiddleware

SetCardKey .

func (*LarkMiddleware) SetMessageKey

func (opt *LarkMiddleware) SetMessageKey(key string) *LarkMiddleware

SetMessageKey .

func (*LarkMiddleware) WithEncryption

func (opt *LarkMiddleware) WithEncryption(key string) *LarkMiddleware

WithEncryption .

func (*LarkMiddleware) WithTokenVerification

func (opt *LarkMiddleware) WithTokenVerification(token string) *LarkMiddleware

WithTokenVerification .

Jump to

Keyboard shortcuts

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