-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.43 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
63
64
65
66
67
package main
import (
"log"
"net/http"
"github.com/ncaak/pifiabot/actions"
"github.com/ncaak/pifiabot/client"
"github.com/ncaak/pifiabot/config"
"github.com/ncaak/pifiabot/models"
"github.com/ncaak/pifiabot/utils"
)
func handleUpdate(w http.ResponseWriter, r *http.Request) {
log.Println("INFO :: Request received")
var input, err = utils.GetRequestInput(r.Body)
if err != nil {
log.Println("ERROR :: Reading Telegram request : " + err.Error())
return
}
if input.IsCommand {
var command = actions.Factory(input.Text)
var response, err = command.Resolve()
if err != nil {
response = config.Get().Message(err.Error())
}
var output = models.Output{
ChatId: input.ChatId,
MessageId: input.MessageId,
Text: response,
}
client.Get().SendMessage(output)
}
}
func main() {
log.Println("INFO :: Setting up Configuration")
if err := config.Setup(); err != nil {
log.Fatal(err)
}
log.Println("INFO :: Setting up Webhook")
client.Setup(config.Get().BotToken)
var webhook = models.SetWebhook{
Url: config.Get().GetEndpoint(),
Certificate: config.Get().CertBytes,
}
if err := client.Get().SetWebhook(webhook); err != nil {
log.Fatal(err)
}
log.Println("INFO :: Starting the server...")
http.HandleFunc("/"+config.Get().Url.Path, handleUpdate)
log.Fatal(
http.ListenAndServeTLS(
":"+config.Get().Url.Port,
config.Get().File.Certificate,
config.Get().File.PrivateKey,
nil),
)
}