-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (47 loc) · 1.38 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
package main
import (
"fmt"
"strconv"
/*
* env
*/
"log"
"os"
"time"
taipower "github.com/MinFengLin/taipower_tgbot/taipower"
bot "github.com/MinFengLin/taipower_tgbot/bot"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
chat_id, _ := strconv.ParseInt(os.Getenv("CHAT_ID"), 10, 64)
Token := os.Getenv("TOKEN")
more_info, _ := strconv.ParseBool(os.Getenv("MORE_INFO"))
Env_Time, _ := strconv.ParseInt(os.Getenv("Timer_Minutes"), 10, 64)
crontab_time := os.Getenv("Crontime")
if len(crontab_time) > 0 {
fmt.Printf("chat_id: %d, Token: %s, more_info: %t, Use crontab: %s \n", chat_id, Token, more_info, crontab_time)
c := cron.New()
_, _ = c.AddFunc(crontab_time, func() {
data := taipower.Parser_Taipower(&more_info)
bot.Telegram_bot_run(&chat_id, &Token, data)
})
data := taipower.Parser_Taipower(&more_info)
bot.Telegram_bot_run(&chat_id, &Token, data)
c.Start()
select {}
} else {
fmt.Printf("chat_id: %d, Token: %s, more_info: %t, How often to run: %d (Minutes)\n", chat_id, Token, more_info, Env_Time)
tChannel := time.NewTimer(time.Duration(Env_Time) * time.Minute)
for {
data := taipower.Parser_Taipower(&more_info)
bot.Telegram_bot_run(&chat_id, &Token, data)
tChannel.Reset(time.Duration(Env_Time) * time.Minute)
<-tChannel.C
}
}
}