To handle incoming events (like receiving a message), register a handler using the EventCallback service and then use ListenCallback within an HTTP server to process the requests.
- Initialize the client with
lark.WithEventCallbackVerify. - Use a specific handler method (e.g.,
HandlerEventIMMessageReceiveV1) to define the logic for a specific event type. - Use
cli.EventCallback.ListenCallback inside an HTTP handler to route the request body to the registered event handlers.
cli := lark.New(
lark.WithAppCredential("<APP_ID>", "<APP_SECRET>"),
lark.WithEventCallbackVerify("<ENCRYPT_KEY>", "<VERIFICATION_TOKEN>"),
)
// handle message callback
cli.EventCallback.HandlerEventIMMessageReceiveV1(func(ctx context.Context, cli *lark.Lark, schema string, header *lark.EventV2Header, event *lark.EventV2IMMessageReceiveV1) (string, error) {
_, _, err := cli.Message.Reply(event.Message.MessageID).SendText(ctx, "hi, "+event.Message.Content)
return "", err
})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
cli.EventCallback.ListenCallback(r.Context(), r.Body, w)
})
log.Fatal(http.ListenAndServe(":9726", nil))