-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (176 loc) · 5.11 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"fmt"
"time"
"log"
"github.com/gin-gonic/gin"
"html/template"
"flag"
"gopkg.in/yaml.v2"
"io/ioutil"
"regexp"
)
var (
cfg = Config{}
bot *tgbotapi.BotAPI
tmpH *template.Template
globalChan = make(chan *tgbotapi.Update)
UserStates = []UserState{}
)
func handleTextMessage(update *tgbotapi.Update){
if update.Message == nil {
return
}
state := findUserState(update.Message.Chat.ID)
m := state.CurMenu
m.RunAction(state.Action, &ActionParams{bot: bot, update: update})
}
func telegramBot(bot *tgbotapi.BotAPI) {
go userStateChanged()
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err)
}
introduce := func(update tgbotapi.Update) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Hi, your chat id is '%d'", update.Message.Chat.ID))
bot.Send(msg)
}
time.Sleep(time.Millisecond * 500)
for len(updates) != 0 {
<-updates
}
for update := range updates {
log.Printf("update tick")
if update.Message != nil && update.Message.IsCommand() {
if checkAuthorized(update.Message.Chat.ID) == false {
continue
}
} else if update.InlineQuery != nil {
log.Printf("inline query")
} else if update.Message != nil && update.Message.Text != "" {
if checkAuthorized(update.Message.Chat.ID) == false {
continue
}
if update.Message.Text == "introduce" {
introduce(update)
continue
}
if update.Message.Text == "callback" {
var silenceKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("test1", "1"),
tgbotapi.NewInlineKeyboardButtonData("test2", "2"),
tgbotapi.NewInlineKeyboardButtonData("test3", "3"),
),
)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Test: ' '%d'", update.Message.Chat.ID))
msg.ReplyMarkup = silenceKeyboard
bot.Send(msg)
continue
}
handleTextMessage(&update)
} else if update.CallbackQuery != nil && update.Message == nil {
if checkAuthorized(int64(update.CallbackQuery.From.ID)) == false {
continue
}
log.Println("callback message received")
re := regexp.MustCompile("([^,]*),([^,]*),(.*)")
match := re.FindStringSubmatch(update.CallbackQuery.Data)
if len(match) < 3 {
log.Printf("Can't find any targets for callback")
continue
}
target := match[1]
silenceTime := match[2]
alertname := match[3]
userName := update.CallbackQuery.From.UserName
switch target {
case "am":
silenceId, err := silentAlert(silenceTime, alertname, userName)
if err != nil {
msg := tgbotapi.NewMessage(int64(update.CallbackQuery.From.ID), fmt.Sprintf("Error: %s", err))
bot.Send(msg)
} else {
callbackConfig := tgbotapi.CallbackConfig{
CallbackQueryID: update.CallbackQuery.ID,
Text: fmt.Sprintf("Silence successfully set, id: %s", silenceId),
ShowAlert: true,
}
bot.AnswerCallbackQuery(callbackConfig)
msg := tgbotapi.NewMessage(int64(update.CallbackQuery.From.ID), fmt.Sprintf("Silence successfully set, id: %s", silenceId))
bot.Send(msg)
}
default:
msg := tgbotapi.NewMessage(int64(update.CallbackQuery.From.ID), fmt.Sprintf("Unknown target: %s", target))
bot.Send(msg)
}
}
}
}
// Global
var config_path = flag.String("c", "config.yaml", "Path to a config file")
var listen_addr = flag.String("l", ":9088", "Listen address")
var template_path = flag.String("t", "", "Path to a template file")
var debug = flag.Bool("d", false, "Debug template")
func main() {
flag.Parse()
content, err := ioutil.ReadFile(*config_path)
if err != nil {
log.Fatalf("Problem reading configuration file: %v", err)
}
err = yaml.Unmarshal(content, &cfg)
if err != nil {
log.Fatalf("Error parsing configuration file: %v", err)
}
fmt.Printf("%+v", cfg)
if *template_path != "" {
cfg.TemplatePath = *template_path
}
UserStates = initUserStates()
bot_tmp, err := tgbotapi.NewBotAPI(cfg.TelegramToken)
if err != nil {
log.Fatal(err)
}
bot = bot_tmp
if cfg.TemplatePath != "" {
tmpH = loadTemplate(cfg.TemplatePath)
if cfg.TimeZone == "" {
log.Fatalf("You must define time_zone of your bot")
panic(-1)
}
} else {
*debug = false
tmpH = nil
}
if !(*debug) {
gin.SetMode(gin.ReleaseMode)
}
log.Printf("Authorized on account %s", bot.Self.UserName)
go telegramBot(bot)
router := gin.Default()
router.GET("/ping/:chatid", GET_Handling)
router.POST("/alert/:chatid", POST_Handling)
router.Run(*listen_addr)
}
func checkAuthorized(chatId int64) bool {
// if authorized chats property exists and it's value exists
if len(cfg.AuthorizedChatIds) > 0 {
for _, iter := range cfg.AuthorizedChatIds {
//if chatId found in authorized list, return true
if iter == chatId {
log.Printf("%d authorized", chatId)
return true
}
}
}else{
// if no chat ids defined
log.Printf("%d authorized, there is no authorized_chat_ids property, allow all", chatId)
return true
}
// if not found any elem
log.Printf("%d not authorized, breaking requests", chatId)
return false
}