This commit is contained in:
shoopea 2019-05-11 11:29:09 +08:00
parent b3247eaa0a
commit 17352c8bfb
2 changed files with 17 additions and 31 deletions

21
bot.go
View File

@ -8,20 +8,9 @@ import (
"time"
)
var (
b *tb.Bot
)
func StartBot() {
// Registering bot
b, err := tb.NewBot(tb.Settings{
Token: cfg.Telegram.Token,
URL: cfg.Telegram.URL,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
failOnError(err, "StartBot")
var ()
func BotHandlers() {
b.Handle("/hello", func(m *tb.Message) {
s, err := botHello(m)
logOnError(err, "/hello")
@ -43,12 +32,6 @@ func StartBot() {
b.Handle(tb.OnQuery, botQuery)
b.Handle(tb.OnText, botText)
u := tb.User{
ID: 32173684,
}
b.Send(&u, "test")
b.Start()
}

27
main.go
View File

@ -42,6 +42,7 @@ var (
initdb = flag.Bool("initdb", false, "initialize bot database")
db *sql.DB
b *tb.Bot
cfg Config
MQCWMsgQueue chan ChatWarsMessage
@ -61,28 +62,21 @@ func main() {
flag.Parse()
err := gcfg.ReadFileInto(&cfg, *config)
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
failOnError(err, "Parsing config")
// Connecting to DB
switch cfg.SQL.Driver {
case "mysql":
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true")
if err != nil {
log.Fatal(err)
}
failOnError(err, "Opening MySQL connection")
defer db.Close()
}
// Check if any issue
err = db.Ping()
if err != nil {
log.Fatal(err)
} else {
log.Println("SQL connection initialized")
}
failOnError(err, "Checking MySQL connection")
log.Println("SQL connection initialized")
if *initdb {
initDB()
@ -91,7 +85,16 @@ func main() {
msgParsingRules, err = loadMsgParsingRules()
logOnError(err, "Message parsing rules")
go StartBot()
// Registering bot
b, err := tb.NewBot(tb.Settings{
Token: cfg.Telegram.Token,
URL: cfg.Telegram.URL,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
failOnError(err, "Registering bot")
log.Println("Bot registered")
go BotHandlers()
MQCWMsgQueue = make(chan ChatWarsMessage, 100)
SQLMsgIdentifyQueue = make(chan int64, 100)