-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_impl.go
337 lines (281 loc) · 9.55 KB
/
server_impl.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
tp "taipeion/core"
api_platform "github.com/h-alice/tcg-api-platform-client"
"golang.org/x/sync/semaphore"
)
// # Enqueue an incoming webhook event.
func (tpb *TaipeionBot) enqueueWebhookIncomingEvent(event ChatbotWebhookEvent) {
tpb.eventQueue <- event
}
// # Broadcast message sender
//
// This method uses the message API to send a broadcast message to all users who have subscribed to the channel.
//
// Parameters:
// - message: The message to be sent.
// - target_channel: The channel's ID to send the message to.
func (tpb *TaipeionBot) SendBroadcastMessage(message string, target_channel int) error {
// Craft the message
ch_payload := tp.ChannelMessagePayload{
Ask: "broadcastMessage",
Message: tp.Message{
Type: "text",
Text: message,
},
}
// Send the message
return tpb.DoEndpointPostRequest(tpb.Endpoint, ch_payload, target_channel)
}
// # Private message sender
//
// This method uses the message API to send a private message to a user.
//
// Parameters:
// - userId: The user's ID to send the message to.
// - message: The message to be sent.
// - target_channel: The channel's ID to send the message to.
func (tpb *TaipeionBot) SendPrivateMessage(userId string, message string, target_channel int) error {
// Craft the message
ch_payload := tp.ChannelMessagePayload{
Ask: "sendMessage",
Recipient: userId,
Message: tp.Message{
Type: "text",
Text: message,
},
}
// Send the message
return tpb.DoEndpointPostRequest(tpb.Endpoint, ch_payload, target_channel)
}
// # Perform a POST request to the TaipeiON endpoint
func (tpb *TaipeionBot) DoEndpointPostRequest(endpoint string, channelPayload tp.ChannelMessagePayload, target_channel int) error {
_, err := tpb.api_client.RequestAccessToken()
if err != nil {
log.Printf("[ReqSender] Error: Unable to request access token: %s\n", err)
return err
}
_, err = tpb.api_client.RequestSignBlock()
if err != nil {
log.Printf("[ReqSender] Error: Unable to request sign block: %s\n", err)
return err
}
log.Println(channelPayload)
headers := map[string]string{
"Content-Type": "application/json",
"backAuth": tpb.Channels[target_channel].ChannelAccessToken,
}
// Perform the request
resp, err := tpb.api_client.SendRequest(tpb.Endpoint, "POST", headers, channelPayload, nil)
if err != nil {
return err
}
defer resp.Body.Close()
// Verbose logging
body, _ := io.ReadAll(resp.Body)
log.Printf("[ReqSender] Response (%d): %s\n", resp.StatusCode, string(body))
return nil
}
// # Income Request Handler Factory
//
// This function creates a handler for incoming requests.
func (tpb *TaipeionBot) incomeRequestHandlerFactory() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Check header for content length.
if r.Header.Get("Content-Length") == "" || r.Header.Get("Content-Length") == "0" {
// Ignore and send OK.
response := response{Status: "no content"}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
// Read the request body
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "[EvHandler] Error: Unable to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
// Deserialize the message
payload, err := tp.DeserializeWebhookMessage(body)
if err != nil {
log.Println("[EvHandler] Error: Unable to deserializing message:", err)
// DEBUG: Fallback to printing the raw body
log.Println("[EvHandler] Received payload:", string(body))
http.Error(w, "Malformed payload.", http.StatusBadRequest)
return
}
// Iterate over the events.
for _, event := range payload.Events {
// Create an internal event
internal_event := ChatbotWebhookEvent{
Destination: payload.Destination,
MessageEvent: event,
}
// Enqueue the event
tpb.enqueueWebhookIncomingEvent(internal_event)
}
}
}
func (tpb *TaipeionBot) webhookEventListener() error {
http.HandleFunc("/", tpb.incomeRequestHandlerFactory()) // Set the default handler.
// Start the server.
full_server_address := fmt.Sprintf("%s:%d", tpb.ServerAddress, tpb.ServerPort)
log.Println("[EvListener] Starting server at ", full_server_address)
return http.ListenAndServe(full_server_address, nil) // Serve until error.
}
// # The Main Event Processor Loop
//
// This function is the main loop for processing incoming events.
// It waits for incoming events and processes them using the registered event handlers.
func (tpb *TaipeionBot) EventProcessorLoop(ctx context.Context) error {
log.Println("[EvLoop] Starting event processor loop.")
for {
select {
case <-ctx.Done(): // Check if the context is cancelled.
log.Println("[EvLoop] Context cancelled. Exiting event processor loop.")
return nil
case event := <-tpb.eventQueue: // Wait for incoming events.
log.Printf("[EvProcessor] Processing event: %#v\n", event)
for _, event_handler := range tpb.eventHandlers { // Iterate over the event handlers.
log.Printf("[EvProcessor] Processing event with handler: %#v\n", event_handler.Callback)
go tpb.eventProcessorInternalCallbackWrapper(ctx, event_handler, event) // Call the handler in a goroutine.
}
}
}
}
// # Event Processor Callback Wrapper
//
// Since we've simplified the callback to a single function, we can use this wrapper to handle the semaphore.
// So there's no need to deal with the semaphore or context in the callback function.
//
// The function is for internal use only.
func (tpb *TaipeionBot) eventProcessorInternalCallbackWrapper(ctx context.Context, event_handler_entry eventHandlerEntry, event ChatbotWebhookEvent) error {
ctx = context.WithoutCancel(ctx)
if event_handler_entry.IsPriority {
return event_handler_entry.Callback(tpb, event) // Directly call the event handler.
} else {
tpb.eventSemaphore.Acquire(ctx, 1) // Acquire the semaphore, wait until available.
err := event_handler_entry.Callback(tpb, event) // Call the event handler.
tpb.eventSemaphore.Release(1) // Release the semaphore if callback is done.
return err
}
}
// # Webhook Event Registration
//
// Register a webhook event callback.
// All registered callbacks will be called when an event is received.
func (tpb *TaipeionBot) RegisterWebhookEventCallback(ev_handler_entry eventHandlerEntry) {
tpb.eventHandlers = append(tpb.eventHandlers, ev_handler_entry)
}
// # Main loop
//
// The main loop of the chatbot.
func (tpb *TaipeionBot) mainLoop(ctx context.Context) error {
// Create a child context.
ctx_child, cancel := context.WithCancel(ctx)
defer cancel() // All child coroutines will be cancelled upon main loop termination.
// Create a channel for errors.
subroutine_err := make(chan error)
// Create a semaphore for concurrency control.
tpb.eventSemaphore = semaphore.NewWeighted(int64(tpb.maxConcurrent))
// Start all child coroutines.
log.Println("[Daemon] Starting all child coroutines.")
// Start the event processor loop.
go func(ctx context.Context, err_chan chan error) {
select {
case <-ctx.Done(): // Check if the context is cancelled.
log.Println("[EvListener] Received cancel signal.")
return
default:
err := tpb.EventProcessorLoop(ctx_child) // Start the event processor loop.
if err != nil { // The subroutine has returned an error.
err_chan <- err
}
}
}(ctx_child, subroutine_err)
// Start the webhook event listener.
go func(ctx context.Context, err_chan chan error) {
select {
case <-ctx.Done(): // Check if the context is cancelled.
log.Println("[EvListener] Received cancel signal.")
return
default:
err := tpb.webhookEventListener()
if err != nil { // The subroutine has returned an error.
err_chan <- err
}
}
}(ctx_child, subroutine_err)
// Wait for signals.
select {
case <-ctx.Done(): // Check if the context is cancelled.
log.Println("[Daemon] Received cancelling signal, terminating all subroutines.")
return nil
default:
// Wait for the first error to occur.
err := <-subroutine_err
if err != nil {
return err
}
}
return nil
}
func (tpb *TaipeionBot) Start() error {
// Create the event queue.
// NOTE: Consider design a queue flushing machanism.
tpb.eventQueue = make(chan ChatbotWebhookEvent, 100)
for {
// Create a new context.
ctx, cancel := context.WithCancel(context.Background())
// Start the main loop.
err := tpb.mainLoop(ctx)
if err != nil { // The main loop has returned an error.
log.Println("[Daemon] Main loop returned an error:", err)
cancel()
}
continue // Restart the main loop.
}
}
// # New Chatbot Instance
//
// Create a new chatbot instance.
func NewChatbotInstance(
endpoint string,
channels map[int]Channel,
serverAddress string,
serverPort int16,
apiPlatformEndpoint string,
apiPlatformClientId string,
apiPlatformClientToken string,
maxConcurrentEvent int) *TaipeionBot {
return &TaipeionBot{
Endpoint: endpoint,
Channels: channels,
ServerAddress: serverAddress,
ServerPort: serverPort,
maxConcurrent: maxConcurrentEvent,
api_client: api_platform.NewApiPlatformClient(apiPlatformEndpoint, apiPlatformClientId, apiPlatformClientToken),
}
}
// # New Chatbot Instance from Configuration
//
// Create a new chatbot instance from a configuration.
func NewChatbotFromConfig(config ServerConfig) *TaipeionBot {
return NewChatbotInstance(
config.Endpoint,
config.Channels,
config.Address,
config.Port,
config.ApiPlatformEndpoint,
config.ApiPlatformClientId,
config.ApiPlatformClientToken,
config.MaxConcurrentEvent)
}