-
Notifications
You must be signed in to change notification settings - Fork 0
/
garvis.go
138 lines (113 loc) · 2.85 KB
/
garvis.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package garvis
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"golang.org/x/net/context"
"github.com/go-telegram-bot-api/telegram-bot-api"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
)
var config Configuration
func loadConfig(path string) {
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("Config file missing.", err)
}
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("Config parse error: ", err)
}
}
func init() {
loadConfig("./config.json")
http.HandleFunc("/"+config.TgBotKey+"/start", startBot)
http.HandleFunc("/"+config.TgBotKey+"/stop", stopBot)
http.HandleFunc("/"+config.TgBotKey, handler)
}
func startBot(w http.ResponseWriter, r *http.Request) {
url := "https://" + r.URL.Host + "/" + config.TgBotKey
setWebhook(url, r)
w.Write([]byte("The bot has been initialized."))
}
func stopBot(w http.ResponseWriter, r *http.Request) {
setWebhook("", r)
w.Write([]byte("The bot has been disabled."))
}
func getBot(c context.Context, r *http.Request) (*tgbotapi.BotAPI, error) {
client := urlfetch.Client(c)
bot, err := tgbotapi.NewBotAPIWithClient(config.TgBotKey, client)
if err != nil {
return nil, err
}
return bot, nil
}
func setWebhook(link string, r *http.Request) {
c := appengine.NewContext(r)
bot, err := getBot(c, r)
if err != nil {
log.Fatal(err)
}
_, err = bot.SetWebhook(tgbotapi.NewWebhook(link))
if err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
bot, err := getBot(ctx, r)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close()
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
var update tgbotapi.Update
json.Unmarshal(bytes, &update)
if update.Message == nil {
return
}
garvis(bot, ctx, update)
}
func garvis(bot *tgbotapi.BotAPI, ctx context.Context, update tgbotapi.Update) {
var filters []Filter
done := make(chan bool)
filters = append(filters, CapsFilter{bot, ctx, done})
filters = append(filters, TldrFilter{bot, ctx, done})
filters = append(filters, TextFilter{bot, ctx, done})
filterCommands := make(map[string]Filter)
for _, r := range filters {
r.GetCommands(filterCommands)
}
if isCommandForMe(ctx, bot, update.Message) {
cmd := update.Message.Command()
if e, ok := filterCommands[cmd]; ok {
cmdarg := CommandArguments{ctx, update}
e.RunCommand(cmd, cmdarg)
}
} else {
for _, f := range filters {
go f.Run(update)
}
for i := 0; i < len(filters); i++ {
<-done
}
}
}
func isCommandForMe(ctx context.Context, bot *tgbotapi.BotAPI, m *tgbotapi.Message) bool {
if m.IsCommand() {
command := strings.SplitN(m.Text, " ", 2)[0][1:]
if i := strings.Index(command, "@"); i != -1 {
botName := command[i+1:]
if bot.Self.UserName != botName {
return false
}
}
return true
}
return false
}