-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructs.go
123 lines (108 loc) · 3.45 KB
/
structs.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
package main
import (
"errors"
"fmt"
"sync"
"time"
"github.com/asdine/storm"
"github.com/fernandezvara/scheduler"
tb "gopkg.in/tucnak/telebot.v2"
)
// errors
var (
errInvalidPayload = errors.New("invalid payload")
errCommandNotFound = errors.New("command not found")
)
// consts
var (
allowedMethods = []string{"GET", "POST", "PUT", "OPTIONS"}
)
// statuses, there is a non-zero status to ensure comparations won't match for an empty entry
const (
statusDown int = iota + 1
statusUp
statusStarted
statusStopped
)
// payload consts
const (
typeString string = "string"
typeInt string = "int"
typeBool string = "bool"
typeTimeExp string = "time"
)
type command struct {
fn func(m *tb.Message, returns []interface{}) // function to execute
noHelp bool // hide on help command
isPrivate bool // command needs to be send privatelly to the bot
forUsers bool // must be user of the bot in order of been able to use it
forAdmins bool // must be admin to use it
helpShort string // short help text description
helpLong string // long help text
payload []payloadPart // payload parts and validations
}
type payloadPart struct {
arg string
typ string
valid []string
help string
}
// urlTester is the main struct that holds the service parts that need to interact
type urlTester struct {
db *storm.DB
bot *tb.Bot
dbpath string
token string
schedules map[int]*scheduler.Job
lastStatus map[int]timeline
commands map[string]command
admins []int64
sync.RWMutex
}
// schedule is the definition of a recurrent monitoring job
type schedule struct {
ID int `json:"id" storm:"id,increment"`
UserID int64 `json:"user_id" storm:"index"`
Private bool `json:"private" storm:"index"`
Method string `json:"method" storm:"index"`
URL string `json:"url" storm:"index"`
ExpectedStatus int `json:"expected_status,omitempty"`
ExpectedText string `json:"expected_text,omitempty"`
ExpectedTimeout string `json:"expected_timeout,omitempty"`
Every string `json:"every"`
Subscriptors []int64 `json:"subscriptors"`
}
type user struct {
ID int64 `json:"id" storm:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
LanguageCode string `json:"language_code"`
IsBot bool `json:"is_bot"`
Authorized bool `json:"authorized"`
}
// history saves the user interactions with the bot by user
type history struct {
ID int `json:"id" storm:"id,increment"`
When time.Time `json:"when"`
UserID int64 `json:"user_id" storm:"index"`
Message string `json:"message"`
}
// timeline stores the status of each monitor
type timeline struct {
ID int `json:"id" storm:"id,increment"`
MonitorID int `json:"monitor_id"`
Timestamp int64 `json:"timestamp"`
Status int `json:"status" storm:"index"`
Downtime int64 `json:"downtime"`
// duration time.Duration
// body string
// headers map[string]string
}
// telegramUser uses the same interface than *tb.User
type telegramUser struct {
id int64
}
func (t telegramUser) Recipient() string {
return fmt.Sprintf("%d", t.id)
}