forked from SakoDroid/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webhook.go
93 lines (85 loc) 路 2.84 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
92
93
package tba
import (
"encoding/json"
"net/http"
"strconv"
"strings"
cfg "github.com/fulviodenza/telego/v2/configs"
log "github.com/fulviodenza/telego/v2/logger"
objs "github.com/fulviodenza/telego/v2/objects"
up "github.com/fulviodenza/telego/v2/parser"
)
type Webhook struct {
configs *cfg.BotConfigs
isSecretTokenSet bool
parser *up.UpdateParser
Logger *log.BotLogger
}
// StartWebHook starts the webhook.
func (w *Webhook) StartWebHook(cfg *cfg.BotConfigs, parser *up.UpdateParser) error {
w.configs = cfg
// interfaceUpdateChannel = iuc
// chatUpdateChannel = cuc
w.isSecretTokenSet = cfg.WebHookConfigs.SecretToken != ""
w.parser = parser
w.startTheServer()
return nil
}
func (w *Webhook) startTheServer() {
mux := http.NewServeMux()
mux.HandleFunc("/", w.mainHandler)
mux.HandleFunc("/"+w.configs.APIKey, w.handleReq)
go func() {
err := http.ListenAndServeTLS(":"+strconv.Itoa(w.configs.WebHookConfigs.Port), w.configs.WebHookConfigs.CertFile, w.configs.WebHookConfigs.KeyFile, mux)
if err != nil {
w.Logger.GetRaw().Fatalln("Webhook : Failed to start the HTTPS server.", err)
}
}()
}
func (w *Webhook) mainHandler(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(404)
wr.Write([]byte{})
}
func (w *Webhook) handleReq(wr http.ResponseWriter, req *http.Request) {
if w.isSecretTokenSet {
token := req.Header.Get("X-Telegram-Bot-Api-Secret-Token")
if token != w.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)
w.parser.ExecuteChain(update)
} else {
w.Logger.GetRaw().Println("Webhook : Error parsing the update. Address :", req.RemoteAddr, ". Error :", jsonErr)
}
} else {
w.Logger.GetRaw().Println("Webhook : Error reading the body. Address :", req.RemoteAddr, ". Error :", err)
}
wr.WriteHeader(200)
wr.Write([]byte{})
} else {
w.Logger.GetRaw().Println("Webhook : Request has no body. Address :", req.RemoteAddr)
w.send400(&wr, "Request has no body")
}
} else {
w.Logger.GetRaw().Println("Webhook : \"Content-Type\" header is not json or it's missing. Address :", req.RemoteAddr)
w.send400(&wr, " \"Content-Type\" header is not json or it's missing")
}
}
func (w *Webhook) 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))
}