-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
104 lines (80 loc) · 2.12 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/tidwall/gjson"
"log"
"time"
tele "gopkg.in/tucnak/telebot.v2"
)
const dataFile = "data.json"
const timeout = 5
var isTest = false
func main() {
t := os.Getenv("lgbtcntest")
if t == "" {
isTest = false
}
b, err := strconv.ParseBool(t)
if err == nil {
isTest = b
}
fmt.Printf("[I] TEST MODE: %v\n", isTest)
bot, err := tele.NewBot(
tele.Settings{
Token: os.Getenv("Token"),
Poller: &tele.LongPoller{Timeout: timeout * time.Second,
},
},
)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Println("[I] BOT CREATED WITHOUT ANY ERR.")
month := time.Now().Format("01")
day := time.Now().Format("02")
c := os.Getenv("Chat_ID")
ChatID, errC := strconv.ParseInt(c, 10, 64)
if errC != nil {
fmt.Println("[E] CAN NOT PARSE CHAR_ID TO INT64! EXIT!")
os.Exit(1)
}
fmt.Println("[I] GET CHAT_ID SUCCESSFULLY")
// bot.Start()
_, errS := bot.Send(tele.ChatID(ChatID), historyToday(month, day), tele.NoPreview, "Markdown")
fmt.Println("[I] MSG SENT. EXITING.")
// After sending msg, exit, cuz it boosts by CI/FaaS & Cron
if errS == nil {
os.Exit(0)
}
fmt.Println(err)
os.Exit(1)
}
func historyToday(month, day string) string {
data, _ := ioutil.ReadFile(dataFile)
tip := gjson.Get(string(data), month+".tip")
today := month + "-" + day
if tip.String() != "" {
return tip.String() + "\n=====\n\n" + eventList(month, day) + "\n" + today
}
if isTest {
return "[TEST]\n" + eventList(month, day) + "\n" + today
}
return eventList(month, day) + "\n" + today
}
func eventList(month, day string) string {
data, _ := ioutil.ReadFile(dataFile)
count, _ := strconv.Atoi((gjson.Get(string(data), month+"."+day+".#")).String())
event := ""
if (gjson.Get(string(data), month+"."+day+".0")).String() != "" {
for i := 0; i < count; i++ {
event = event + "\n" + (gjson.Get(string(data), month+"."+day+"."+strconv.Itoa(i))).String()
}
return event
}
event = "暂无历史今天的性少数群体历程\n你可以[前往 GitHub 提交数据](https://github.com/LGBT-CN/HistoryToday/edit/master/data.json)"
return event
}