-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (88 loc) · 2.41 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
package main
import (
"encoding/json"
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/spf13/viper"
"log"
"net/http"
"order-monitor/models"
"strings"
"time"
)
type Config struct {
BotAPIKey string `mapstructure:"bot_api_key"`
TargetURL string `mapstructure:"target_url"`
ChatID int64 `mapstructure:"chat_id"`
MessageURL string `mapstructure:"message_url"`
}
func main() {
config := ParseConfig()
FetchPeriodically(FetchDeliverySlots, config.TargetURL, config.BotAPIKey, config.ChatID, config.MessageURL)
}
type fetchFunc func(string) ([]models.DeliverySlot, error)
func FetchDeliverySlots(targetURL string) ([]models.DeliverySlot, error) {
var slots []models.DeliverySlot
//resolve claim by link
resp, err := http.Get(targetURL)
if err != nil {
return nil, fmt.Errorf("couldn't fetch delivery slots % s %v ", targetURL, err)
}
err = json.NewDecoder(resp.Body).Decode(&slots)
if err != nil {
return nil, fmt.Errorf("unexpected model %s ", err)
}
return slots, nil
}
func FetchPeriodically(f fetchFunc, targetURL string, key string, chatId int64, messageURL string) {
bot, err := tgbotapi.NewBotAPI(key)
if err != nil {
panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
for {
res, err := f(targetURL)
if err != nil {
fmt.Errorf(err.Error())
} else {
//show open slots
slotFound := false
for _, day := range res {
for _, s := range day.Items {
if s.IsOpen {
p := fmt.Sprintf("time : %s free slot : %+v ", time.Now().Format("15:04:05"), s)
fmt.Println(p)
slotFound = true
msg := tgbotapi.NewMessage(chatId, p)
bot.Send(msg)
msg2 := tgbotapi.NewMessage(chatId, fmt.Sprintf("go go go : %s", messageURL))
bot.Send(msg2)
}
}
}
if !slotFound {
fmt.Printf("time : %s no free slots found \n", time.Now().Format("15:04:05"))
}
}
time.Sleep(10 * time.Minute)
}
}
func ParseConfig() Config {
var config Config
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "__"))
viper.AutomaticEnv()
viper.AddConfigPath("./")
viper.SetConfigName("config")
viper.SetConfigType("json")
// Find and read the config file
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error while loading config file: %s\n", err))
}
err = viper.Unmarshal(&config)
if err != nil {
panic(fmt.Errorf("Fatal error while unmarshaling config: %s\n", err))
}
return config
}