-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (83 loc) · 2.23 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
package main
import (
"encoding/json"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const defaultInterval = time.Minute
var config Config
var alarms map[string]*time.Timer
func init() {
configFile := WithoutError(os.Open("config.json"))
defer configFile.Close()
json.NewDecoder(configFile).Decode(&config)
bot := WithoutError(tgbotapi.NewBotAPI(config.Key))
alarms = make(map[string]*time.Timer)
for _, p_ := range config.Projects {
p := p_
println("project: " + p.Id)
if p.Interval == 0 {
p.Interval = defaultInterval
}
p.currentInterval = p.Interval
alarms[p.Id] = time.NewTimer(p.currentInterval)
Execute(func() {
for {
<-alarms[p.Id].C
println(p.Id + " stop response")
msg := tgbotapi.NewMessage(p.RoomId, p.Id+" stop response")
bot.Send(msg)
p.currentInterval *= 2
alarms[p.Id].Reset(p.currentInterval)
}
})
}
}
func main() {
r := gin.Default()
r.GET("/ping/:id", func(c *gin.Context) {
id := c.Params.ByName("id")
p, ok := config.getProject(id)
if ok {
alarms[id].Reset(p.Interval)
c.JSON(http.StatusOK, gin.H{"message": "pong" + id})
} else {
c.JSON(http.StatusOK, gin.H{"message": "invalid"})
}
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
type Project struct {
Id string `json:"id"`
RoomId int64 `json:"roomid"`
Interval time.Duration `json:"interval"`
currentInterval time.Duration
}
type Config struct {
Projects []*Project `json:"projects"`
Key string `json:"key"`
}
func (c *Config) getProject(id string) (*Project, bool) {
for _, p := range c.Projects {
if p.Id == id {
return p, true
}
}
return &Project{}, false
}
// bot.Debug = true
// log.Printf("Authorized on account %s", bot.Self.UserName)
// u := tgbotapi.NewUpdate(0)
// u.Timeout = 60
// updates := bot.GetUpdatesChan(u)
// for update := range updates {
// if update.Message != nil { // If we got a message
// log.Printf("[%v] %s", update.Message.Chat.ID, update.Message.Text)
// msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
// msg.ReplyToMessageID = update.Message.MessageID
// bot.Send(msg)
// }
// }