-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
280 lines (245 loc) · 9.3 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
const woltParseTimeout = 30 // minutes
const telegramCheckTimeout = 15 // seconds
type WebImage struct {
URL string `json:"url"`
}
// Define the Item struct
type Item struct {
Name string `json:"name"`
PurchasableBalance *int `json:"purchasable_balance"`
Images []WebImage `json:"images"`
}
// Define the AssortmentResponse struct
type AssortmentResponse struct {
Items []Item `json:"items"`
}
// Define the Result struct to store search results
type Result struct {
Venue string `json:"venue"`
Name string `json:"name"`
Founded bool `json:"founded"`
Time string `json:"time"` // using string to store the timestamp in a readable format
Image string `json:"image"`
}
// Define the Config struct to match the structure in config.json
type Config struct {
Endpoint string `json:"endpoint"`
Names []string `json:"names"`
Venue string `json:"venue"`
}
// send test message to telegram from bot
func httpHandler(w http.ResponseWriter, r *http.Request) {
message := "Hello from Telegram Bot!"
err := sendTelegramMessage(message)
if err != nil {
fmt.Fprintf(w, "Failed to send message: %v", err)
return
}
fmt.Fprintf(w, "Message sent to Telegram!")
}
func containsStatus(text string) bool {
// Convert text to lowercase
lowerText := strings.ToLower(text)
// Check if "status" is present
return strings.Contains(lowerText, "status")
}
func formatResultsMessage(results []Result) string {
var messageBuilder strings.Builder
for _, result := range results {
status := "out of stock"
if result.Founded {
status = "available"
}
// Append the formatted string to the message
messageBuilder.WriteString(fmt.Sprintf("%s | *%s* is %s\n", result.Venue, result.Name, status))
}
// Convert the message builder to a string
return messageBuilder.String()
}
// Perform the GET request and parse the JSON response
func fetchItems(endpoint string) (*AssortmentResponse, error) {
response, err := http.Get(endpoint)
if err != nil {
return nil, fmt.Errorf("error making GET request: %v", err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
var assortment AssortmentResponse
err = json.Unmarshal(body, &assortment)
if err != nil {
return nil, fmt.Errorf("error parsing JSON response: %v", err)
}
return &assortment, nil
}
// Search for items that match any of the provided names
func searchItems(assortment *AssortmentResponse, searchNames []string) map[string][]Item {
// assortment - fetched items from api
// searchNames - names from config
matchingItems := make(map[string][]Item)
for _, searchName := range searchNames {
fmt.Printf("[searchItems] Searching for: %s\n", searchName)
for _, item := range assortment.Items {
if strings.Contains(strings.ToLower(item.Name), strings.ToLower(searchName)) {
fmt.Printf("[searchItems] Found matching item: %s\n, PurchasableBalance: %s\n", item.Name, item.PurchasableBalance)
if item.PurchasableBalance != nil && *item.PurchasableBalance == 0 {
continue
}
matchingItems[searchName] = append(matchingItems[searchName], item)
}
}
}
return matchingItems
}
// Compare fetched results with the existing results and send differences to Telegram
func compareResults(newResults []Result, existingResults []Result) {
for _, newResult := range newResults {
if existingResult, exists := existingResults[newResult.Name]; exists {
fmt.Printf("[compareResults] Product: %s\n, old status: %b\n, new status: %b\n", newResult.Name, existingResult.Founded, newResult.Founded)
if existingResult.Founded != newResult.Founded {
fmt.Printf("[compareResults] Status changed for product: %s\n", newResult.Name)
var statusMessage string
if newResult.Founded {
statusMessage = "The product is available!"
} else {
statusMessage = "The product is out of stock..."
}
message := fmt.Sprintf("**Product**: %s\n**Venue**: %s\n**Status**: %s\n![image](%s)", newResult.Name, newResult.Venue, statusMessage, newResult.Image)
err := sendTelegramMessage(message)
if err != nil {
fmt.Printf("Failed to send message: %v\n", err)
}
}
}
}
}
// Process each config entry by fetching items and searching for matches
func processConfig(config Config) ([]Result, error) {
assortment, err := fetchItems(config.Endpoint)
if err != nil {
return nil, err
}
matches := searchItems(assortment, config.Names)
var results []Result
for _, searchName := range config.Names {
items := matches[searchName]
if len(items) == 0 {
result := Result{
Venue: config.Venue,
Name: searchName,
Founded: false,
Time: time.Now().Format(time.RFC3339),
Image: "",
}
results = append(results, result)
continue
}
foundedItem := items[0]
var image string = ""
if foundedItem.Images != nil && len(foundedItem.Images) > 0 {
image = foundedItem.Images[0].URL
}
result := Result{
Venue: config.Venue,
Name: searchName,
Founded: true,
Time: time.Now().Format(time.RFC3339),
Image: image,
}
results = append(results, result)
}
return results, nil
}
func main() {
configs, err := loadConfig("config.json")
if err != nil {
fmt.Println("Error loading config:", err)
return
}
// Run the 30-minute loop in a goroutine
go func() {
for {
fmt.Println("[main] Starting new iteration: ", time.Now().Format("02-01 15:04:05"))
existingResults, err := loadExistingResults()
if err != nil {
fmt.Println("Error loading existing results:", err)
return
}
var allResults []Result
for _, config := range configs {
fmt.Printf("Start processing config for venue: %s\n", config.Venue)
results, err := processConfig(config)
if err != nil {
fmt.Println("Error processing config:", err)
continue
}
fmt.Printf("Config processed successfully for venue: %s\n", config.Venue)
// Compare the new results with existing results and send to Telegram
compareResults(results, existingResults)
allResults = append(allResults, results...)
}
err = writeResultsToFile(allResults, "store.json")
if err != nil {
fmt.Println("Error writing results to file:", err)
} else {
fmt.Println("Results successfully written to store.json")
}
time.Sleep(woltParseTimeout * time.Minute)
}
}()
offset := 0
// Run the 15-seconds loop in another goroutine
go func() {
for {
updates, err := getUpdates(offset)
if err != nil {
fmt.Printf("Error getting updates: %v\n", err)
continue
}
for _, update := range updates {
// Print the received message
fmt.Printf("New message from %s: %s\n", update.Message.From.Username, update.Message.Text)
containsStatus := containsStatus(update.Message.Text)
if containsStatus {
fmt.Println("The message contains the word 'status'")
existingResults, err := loadExistingResults()
if err != nil {
fmt.Println("Error loading existing results:", err)
return
}
resultsMessage := formatResultsMessage(existingResults)
fmt.Println("loading existing results:", resultsMessage)
err = sendTelegramMessage(resultsMessage)
if err != nil {
fmt.Println("Error sending message:", err)
}
}
// Update offset to the latest update ID + 1 to avoid fetching the same message again
offset = update.UpdateID + 1
}
// Your code for the 15-seconds task here
time.Sleep(telegramCheckTimeout * time.Second)
}
}()
// Start the HTTP server in a separate goroutine
go func() {
http.HandleFunc("/send-text", httpHandler)
if err := http.ListenAndServe(":8088", nil); err != nil {
fmt.Println("Error starting server:", err)
return
}
}()
// Keep the main function alive with an infinite loop
select {} // This blocks the main function forever to keep the program running
}