-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingalert.go
78 lines (72 loc) · 1.75 KB
/
pingalert.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
package main
import (
"context"
"fmt"
"os"
"strconv"
"github.com/asendia/salmonping/db"
)
func sendAlerts(ctx context.Context, queries *db.Queries) error {
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
if botToken == "" {
return fmt.Errorf("TELEGRAM_BOT_TOKEN is not set")
}
chatID, err := strconv.ParseInt(os.Getenv("TELEGRAM_CHAT_ID"), 10, 64)
if err != nil {
return err
}
schedules, err := getTodaySchedules(ctx, queries)
if err != nil {
return err
}
listingPings, err := getTodayPings(ctx, queries)
if err != nil {
return err
}
anomalies := getPingAnomalies(schedules, listingPings)
totalAnomalies := len(anomalies)
if totalAnomalies == 0 {
return nil
}
text := createTextMessage(anomalies)
if text == "" {
return nil
}
err = sendTelegramMessage(botToken, chatID, text)
if err != nil {
return err
}
return nil
}
func createTextMessage(anomalies []db.SelectOnlineListingPingsRow) string {
var text string
if len(anomalies) == 0 {
return ""
}
if len(anomalies) == 1 {
row := anomalies[0]
text = fmt.Sprintf("%s [%s - %s](%s) is %s\n", storeStatusToEmoji(row.Status), row.Name, row.Platform, row.Url, row.Status)
} else {
text = ""
// Check if current ping status is different from previous ping status
// If different, send message to Telegram
for num, row := range anomalies {
// Check if row.Name key exists in currentListingPingMap
text += fmt.Sprintf("%d. %s [%s - %s](%s) is %s\n", num+1, storeStatusToEmoji(row.Status), row.Name, row.Platform, row.Url, row.Status)
}
}
text += "\nsalmonfit.com/status"
return text
}
func storeStatusToEmoji(status string) string {
switch status {
case "open":
return "✅"
case "closed":
return "🚫"
case "unknown":
return "❓"
default:
return "❓"
}
}