-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (66 loc) · 1.7 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
log.Printf("Error loading .env file: %v", err)
return
}
apiURL := os.Getenv("API_URL")
// Replace with your Telegram Bot Token
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
if botToken == "" {
fmt.Errorf("Telegram bot token not found in environment variables")
return
}
channelID := os.Getenv("CHANNEL_ID")
if channelID == "" {
log.Fatal("CHANNEL_ID not set in environment")
}
retryInterval := 1 * time.Minute
seenQuestIDs, err := initSeenQuest(apiURL, botToken, channelID)
if err != nil {
log.Fatal(err)
return
}
round := 0
for {
fmt.Printf("Running round %d...\n", round)
body, err := fetchQuestData(apiURL)
if err != nil {
log.Printf("Request failed: %v", err)
time.Sleep(retryInterval)
continue
}
var questResponse QuestResponse
err = json.Unmarshal(body, &questResponse)
if err != nil {
log.Printf("Unmarshal failed: %v", err)
time.Sleep(retryInterval)
continue
}
for _, quest := range questResponse.Data.Quests {
questID := quest.Article.ID
_, seen := seenQuestIDs[questID]
if !seen {
// New quest found, send message to channel
xp := quest.Article.Quest.UnitXP
textButton := fmt.Sprintf("View Quest %d XP", xp)
message := fmt.Sprintf("New Quest: %s\n", quest.Article.Quest.Name)
if err := SendMessage(botToken, message, textButton, createQuestURL(questID), channelID); err != nil {
log.Printf("Failed to send message: %v", err)
}
seenQuestIDs[questID] = struct{}{}
}
}
time.Sleep(1 * time.Minute)
round++
}
}