/
Webhook.go
91 lines (82 loc) 路 2.67 KB
/
Webhook.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
package tba
import (
"encoding/json"
"net/http"
"strconv"
"strings"
cfg "github.com/SakoDroid/telego/v2/configs"
log "github.com/SakoDroid/telego/v2/logger"
objs "github.com/SakoDroid/telego/v2/objects"
up "github.com/SakoDroid/telego/v2/parser"
)
var configs *cfg.BotConfigs
// var interfaceUpdateChannel *chan *objs.Update
// var chatUpdateChannel *chan *objs.ChatUpdate
var isSecretTokenSet bool
// StartWebHook starts the webhook.
func StartWebHook(cfg *cfg.BotConfigs) error {
configs = cfg
// interfaceUpdateChannel = iuc
// chatUpdateChannel = cuc
isSecretTokenSet = cfg.WebHookConfigs.SecretToken != ""
startTheServer()
return nil
}
func startTheServer() {
mux := http.NewServeMux()
mux.HandleFunc("/", mainHandler)
mux.HandleFunc("/"+configs.APIKey, handleReq)
go func() {
err := http.ListenAndServeTLS(":"+strconv.Itoa(configs.WebHookConfigs.Port), configs.WebHookConfigs.CertFile, configs.WebHookConfigs.KeyFile, mux)
if err != nil {
log.Logger.Fatalln("Webhook : Failed to start the HTTPS server.", err)
}
}()
}
func mainHandler(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(404)
wr.Write([]byte{})
}
func handleReq(wr http.ResponseWriter, req *http.Request) {
if isSecretTokenSet {
token := req.Header.Get("X-Telegram-Bot-Api-Secret-Token")
if token != configs.WebHookConfigs.SecretToken {
wr.WriteHeader(403)
wr.Write([]byte{})
return
}
}
contentType := req.Header.Get("Content-Type")
if contentType != "" && strings.HasSuffix(contentType, "json") {
if req.ContentLength > 0 {
body := make([]byte, req.ContentLength)
_, err := req.Body.Read(body)
if err == nil {
update := &objs.Update{}
jsonErr := json.Unmarshal(body, update)
if jsonErr == nil {
// up.ParseSingleUpdate(update, interfaceUpdateChannel, chatUpdateChannel, configs)
up.ExecuteChain(update)
} else {
log.Logger.Println("Webhook : Error parsing the update. Address :", req.RemoteAddr, ". Error :", jsonErr)
}
} else {
log.Logger.Println("Webhook : Error reading the body. Address :", req.RemoteAddr, ". Error :", err)
}
wr.WriteHeader(200)
wr.Write([]byte{})
} else {
log.Logger.Println("Webhook : Request has no body. Address :", req.RemoteAddr)
send400(&wr, "Request has no body")
}
} else {
log.Logger.Println("Webhook : \"Content-Type\" header is not json or it's missing. Address :", req.RemoteAddr)
send400(&wr, " \"Content-Type\" header is not json or it's missing")
}
}
func send400(wr *http.ResponseWriter, reason string) {
(*wr).Header().Add("Content-Type", "text/plain")
(*wr).Header().Add("Content-Length", strconv.Itoa(len(reason)))
(*wr).WriteHeader(400)
(*wr).Write([]byte(reason))
}