forked from jqs7/Jqs7Bot
/
init.go
117 lines (99 loc) · 2.46 KB
/
init.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
package main
import (
"net/http"
"github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/papertrail"
"github.com/Syfaro/telegram-bot-api"
"github.com/carlescere/scheduler"
"github.com/fatih/set"
"github.com/kylelemons/go-gypsy/yaml"
"gopkg.in/mgo.v2"
"gopkg.in/redis.v3"
)
var (
rc *redis.Client
mc *mgo.Session
mgoURL string
loge = logrus.New()
runMode string
bot *tgbotapi.BotAPI
conf *yaml.File
categories []string
categoriesSet set.Interface
vimtips chan Tips
hitokoto chan Hitokoto
sticker chan string
turingAPI string
msID string
msSecret string
)
func init() {
var err error
// Init categories
categories = []string{
"Linux", "Programming", "Software",
"影音", "科幻", "ACG", "IT", "社区",
"闲聊", "资源", "同城", "Others",
}
categoriesSet = set.New(set.NonThreadSafe)
for _, v := range categories {
categoriesSet.Add(v)
}
loge.Level = logrus.DebugLevel
LoadConf()
botapi, _ := conf.Get("botapi")
redisPass, _ := conf.Get("redisPass")
vimTipsCache, _ := conf.GetInt("vimTipsCache")
hitokotoCache, _ := conf.GetInt("hitokotoCache")
vimtips = new(Tips).GetChan(int(vimTipsCache))
hitokoto = new(Hitokoto).GetChan(int(hitokotoCache))
papertrailURL, _ := conf.Get("papertrailUrl")
papertrailPort, _ := conf.GetInt("papertrailPort")
rc = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: redisPass,
})
sticker = RandSticker(rc)
//logger
hook, err := logrus_papertrail.NewPapertrailHook(
papertrailURL, int(papertrailPort), "nyan")
if err != nil {
loge.Println(err)
} else {
loge.Hooks.Add(hook)
}
//bot init
bot, err = tgbotapi.NewBotAPI(botapi)
if err != nil {
loge.Panic(err)
}
if runMode == "debug" {
hook := tgbotapi.NewWebhook("")
bot.SetWebhook(hook)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
bot.UpdatesChan(u)
} else {
hook := tgbotapi.NewWebhookWithCert("https://jqs7.com:8443/"+bot.Token, "crt.pem")
bot.SetWebhook(hook)
bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS(":8443", "crt.pem", "key.pem", nil)
}
initRss()
MIndex()
dailySave()
scheduler.Every().Day().At("00:05").Run(dailySave)
go GinServer()
}
func LoadConf() {
var err error
conf, err = yaml.ReadFile("botconf.yaml")
if err != nil {
loge.Panic(err)
}
runMode, _ = conf.Get("runMode")
turingAPI, _ = conf.Get("turingBotKey")
mgoURL, _ = conf.Get("mgoUrl")
msID, _ = conf.Get("msTransId")
msSecret, _ = conf.Get("msTransSecret")
}