-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
160 lines (133 loc) · 4.1 KB
/
bot.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
package bot
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/mrmarble/minecraft-update-go/internal/try"
"github.com/mrmarble/minecraft-update-go/pkg/changelog"
"github.com/mrmarble/minecraft-update-go/pkg/manifest"
"github.com/mrmarble/minecraft-update-go/pkg/version"
"github.com/rs/zerolog/log"
)
// Bot holds telegram information
type Bot struct {
ChannelID string
LogID string
Token string
}
type Context struct {
manifest *manifest.Manifest
latestVersion version.Version
localVersion version.Version
}
// Start runs the job once
func (b *Bot) Start(workingDir string) {
ctx := &Context{}
fetchManifest(ctx)
loadLocalVersion(ctx, workingDir)
compareVersion(ctx, b)
updateChangelog(ctx, b)
ctx.latestVersion.Save(workingDir)
}
func fetchManifest(ctx *Context) {
// Fetch latest manifest from minecraft api
var latestManifest *manifest.Manifest
err := try.Do(func(attempt int) (bool, error) {
var err error
latestManifest, err = manifest.GetLatest(http.DefaultClient)
time.Sleep(30 * time.Second)
return attempt < 3, err
})
if err != nil {
log.Fatal().AnErr("Err", err).Msg("Error getting manifest from server.")
}
ctx.manifest = latestManifest
}
func loadLocalVersion(ctx *Context, workingDir string) {
ctx.latestVersion = version.FromManifest(*ctx.manifest)
localVersion, err := version.Load(workingDir)
if err != nil {
log.Info().Msg("Local version not found.")
log.Info().Interface("Remote Version", ctx.latestVersion).Msg("Saving remote and exiting.")
ctx.latestVersion.Changelog = true
ctx.latestVersion.Save(workingDir)
os.Exit(0)
}
if ctx.latestVersion.ID == localVersion.ID && localVersion.Changelog {
log.Info().Interface("Version", localVersion).Msg("Remote version same as local. Exiting.")
os.Exit(0)
}
ctx.localVersion = *localVersion
}
func compareVersion(ctx *Context, b *Bot) {
// New version
if ctx.latestVersion.ID != ctx.localVersion.ID {
log.Info().Str("Version", ctx.latestVersion.ID).Msg("New version.")
if b.LogID != "" {
b.sendMessage(b.LogID, fmt.Sprintf("New Minecraft version: %s\nChangelog: %s", ctx.latestVersion.ID, changelog.URL(ctx.latestVersion.ToURL())))
}
ctx.localVersion = ctx.latestVersion
}
}
func updateChangelog(ctx *Context, b *Bot) {
// Update Changelog
if !ctx.localVersion.Changelog {
log.Info().Str("Version", ctx.latestVersion.ID).Msg("Fetching changelog.")
var chlog *changelog.Changelog
err := try.Do(func(attempt int) (bool, error) {
var err error
chlog, err = changelog.FromURL(ctx.latestVersion.ToURL())
time.Sleep(5 * time.Minute)
return attempt < 3, err
})
if err != nil {
log.Info().AnErr("Err", err).Msg("Changelog is not published. Exiting")
os.Exit(0)
} else {
log.Info().Str("Title", chlog.Title).Msg("Changelog found.")
b.sendMessage(b.ChannelID, chlog.String())
ctx.latestVersion.Changelog = true
}
}
}
// Parse runs the bot against a provided url
func (b *Bot) Parse(url string) {
log.Info().Str("url", url).Msg("Fetching changelog from url")
changelog, err := changelog.FromURL(url)
if err != nil {
log.Info().AnErr("Err", err).Msg("Changelog could not be reached. Exiting")
os.Exit(0)
} else {
log.Info().Str("Title", changelog.Title).Msg("Changelog found.")
changelog.URL = url
b.sendMessage(b.ChannelID, changelog.String())
}
}
func (b *Bot) sendMessage(chatID, message string) {
values := map[string]string{
"chat_id": chatID,
"parse_mode": "HTML",
"text": message,
}
jsonData, err := json.Marshal(values)
if err != nil {
log.Fatal().AnErr("Err", err).Msg("Error marshalling message")
}
resp, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", b.Token), "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal().AnErr("Err", err).Msg("Could not reach telegram.")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warn().AnErr("Err", err).Msg("Error reading HTTP response from Telegram")
return
}
log.Warn().Str("response", string(body)).Msg("Telegram error.")
}
}