forked from infracloudio/msbotbuilder-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (50 loc) · 1.47 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/infracloudio/msbotbuilder-go/core"
"github.com/infracloudio/msbotbuilder-go/core/activity"
"github.com/infracloudio/msbotbuilder-go/schema"
)
var customHandler = activity.HandlerFuncs{
OnMessageFunc: func(turn *activity.TurnContext) (schema.Activity, error) {
return turn.SendActivity(activity.MsgOptionText("Echo: " + turn.Activity.Text))
},
}
// HTTPHandler handles the HTTP requests from then connector service
type HTTPHandler struct {
core.Adapter
}
func (ht *HTTPHandler) processMessage(w http.ResponseWriter, req *http.Request) {
ctx := context.Background()
activity, err := ht.Adapter.ParseRequest(ctx, req)
if err != nil {
fmt.Println("Failed to parse request.", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = ht.Adapter.ProcessActivity(ctx, activity, customHandler)
if err != nil {
fmt.Println("Failed to process request", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("Request processed successfully.")
}
func main() {
setting := core.AdapterSetting{
AppID: os.Getenv("APP_ID"),
AppPassword: os.Getenv("APP_PASSWORD"),
}
adapter, err := core.NewBotAdapter(setting)
if err != nil {
log.Fatal("Error creating adapter: ", err)
}
httpHandler := &HTTPHandler{adapter}
http.HandleFunc("/api/messages", httpHandler.processMessage)
fmt.Println("Starting server on port:3978...")
http.ListenAndServe(":3978", nil)
}