-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
42 lines (33 loc) · 957 Bytes
/
router.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
package teledisq
import (
"encoding/json"
"fmt"
"net/http"
"os"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
func SetupRouter() {
http.HandleFunc("/", healthHandler)
http.HandleFunc(fmt.Sprintf("/hook/test/"), hookHandler)
http.HandleFunc(fmt.Sprintf("/telegram/%s/", os.Getenv("TELEGRAM_WEBHOOK")), telegramHandler)
}
func healthHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "All your base are belong to us!")
}
func hookHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func telegramHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
u := Update{}
err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
log.Errorf(ctx, "Telegram update decoding error: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
HandleTelegramUpdate(ctx, u)
w.WriteHeader(http.StatusOK)
}