-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathevent_callback.go
76 lines (67 loc) · 2.77 KB
/
event_callback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Copyright 2022 chyroc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples
import (
"context"
"fmt"
"log"
"net/http"
"github.com/chyroc/lark"
)
// ExampleEventCallback ...
func ExampleEventCallback() {
cli := lark.New(
lark.WithAppCredential("<APP_ID>", "<APP_SECRET>"),
lark.WithEventCallbackVerify("<ENCRYPT_KEY>", "<VERIFICATION_TOKEN>"),
)
// 如果你希望你的回调是非阻塞的
// if you want your callback is non-blocking
cli = lark.New(
lark.WithAppCredential("<APP_ID>", "<APP_SECRET>"),
lark.WithEventCallbackVerify("<ENCRYPT_KEY>", "<VERIFICATION_TOKEN>"),
lark.WithNonBlockingCallback(true),
)
// handle chat create callback
cli.EventCallback.HandlerEventV1P2PChatCreate(func(ctx context.Context, cli *lark.Lark, schema string, header *lark.EventHeaderV1, event *lark.EventV1P2PChatCreate) (string, error) {
_, _, err := cli.Message.Send().ToChatID(event.ChatID).SendText(ctx, "hi")
return "", err
})
// handle message callback
cli.EventCallback.HandlerEventV2IMMessageReceiveV1(func(ctx context.Context, cli *lark.Lark, schema string, header *lark.EventHeaderV2, event *lark.EventV2IMMessageReceiveV1) (string, error) {
content, err := lark.UnwrapMessageContent(event.Message.MessageType, event.Message.Content)
if err != nil {
return "", err
}
switch event.Message.MessageType {
case lark.MsgTypeText:
_, _, err = cli.Message.Reply(event.Message.MessageID).SendText(ctx, fmt.Sprintf("got text: %s", content.Text.Text))
case lark.MsgTypeFile:
_, _, err = cli.Message.Reply(event.Message.MessageID).SendText(ctx, fmt.Sprintf("got file: %s, key: %s", content.File.FileName, content.File.FileKey))
case lark.MsgTypeImage:
_, _, err = cli.Message.Reply(event.Message.MessageID).SendText(ctx, fmt.Sprintf("got image: %s", content.Image.ImageKey))
}
return "", err
})
// handle add bot
cli.EventCallback.HandlerEventV2IMChatMemberBotAddedV1(func(ctx context.Context, cli *lark.Lark, schema string, header *lark.EventHeaderV2, event *lark.EventV2IMChatMemberBotAddedV1) (string, error) {
return "", nil
})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
cli.EventCallback.ListenCallback(r.Context(), r.Body, w)
})
fmt.Println("start server ...")
log.Fatal(http.ListenAndServe(":9726", nil))
}