-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_callbacks.go
75 lines (58 loc) · 1.71 KB
/
sample_callbacks.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
package main
import (
"fmt"
"log"
)
/*
# Guide: How to design a chatbot callback
- Filter the incoming event to make sure it is what you want to process:
```go
// Check if incoming event is text message.
if event.Message.Type != "text" {
log.Println("Received non-text message. Ignoring.")
return nil
}
```
- Interacting with the channel/user:
Private message:
```go
err := bot.SendPrivateMessage(receiver, reply_message)
```
Broadcast message:
```go
err := bot.SendBroadcastMessage(reply_message)
```
- Register the callback (in the main function):
```go
bot.RegisterWebhookEventCallback(YourCallbackFunction)
```
Followings are some examples of chatbot callbacks.
*/
// # Simple webhook event callback.
//
// This callback puts the incoming message to the log (stdout).
func SimpleWebhookEventCallback(bot *TaipeionBot, event ChatbotWebhookEvent) error {
// Check if incoming event is text message.
if event.Message.Type != "text" {
log.Println("[SimpleStdCallback] Received non-text message. Ignoring.")
return nil
}
log.Printf("[SimpleStdCallback] Received event: %#v\n", event)
return nil
}
// # Private message callback.
//
// This callback is used to send a reply to a cer
func PrivateMessageCallback(bot *TaipeionBot, event ChatbotWebhookEvent) error {
// Check if incoming event is text message.
if event.Message.Type != "text" {
log.Println("[PrivateMessageCallback] Received non-text message. Ignoring.")
return nil
}
chan_id := event.Destination
reply_message := event.Message.Text
receiver := event.Source.UserId
// Process the incoming message.
reply_message = fmt.Sprintf("ChannelId: %d\nEcho: %s", chan_id, reply_message)
return bot.SendPrivateMessage(receiver, reply_message, chan_id)
}