chirpnest/main.go

127 lines
2.5 KiB
Go
Raw Normal View History

2019-04-15 13:21:16 +02:00
package main
import (
2019-05-02 12:11:27 +02:00
"database/sql"
2019-05-03 05:58:36 +02:00
"flag"
"fmt"
2019-05-02 12:11:27 +02:00
_ "github.com/go-sql-driver/mysql"
2019-05-02 12:14:07 +02:00
"gopkg.in/gcfg.v1"
2019-04-15 13:21:16 +02:00
tb "gopkg.in/tucnak/telebot.v2"
2019-05-03 05:58:36 +02:00
"log"
"time"
2019-04-15 13:21:16 +02:00
)
2019-05-02 12:11:27 +02:00
type Config struct {
Telegram struct {
2019-05-03 05:58:36 +02:00
URL string
Token string
2019-05-02 12:11:27 +02:00
}
SQL struct {
2019-05-03 05:58:36 +02:00
Driver string
Type string
Address string
Username string
Password string
Database string
2019-05-02 12:11:27 +02:00
}
Rabbit struct {
2019-05-03 05:58:36 +02:00
User string
Password string
Host string
Queue string
2019-05-02 12:11:27 +02:00
}
2019-05-03 05:00:09 +02:00
Bot struct {
2019-05-03 05:58:36 +02:00
Admin uint64
Guildname string
Guild string
2019-05-03 05:00:09 +02:00
}
2019-05-02 12:11:27 +02:00
}
var (
2019-05-03 05:58:36 +02:00
config = flag.String("config", "chirpnest.cfg", "config file path")
initdb = flag.Bool("initdb", false, "initialize bot database")
2019-05-02 12:11:27 +02:00
2019-05-03 05:58:36 +02:00
db *sql.DB
2019-05-11 05:29:09 +02:00
b *tb.Bot
2019-05-03 05:58:36 +02:00
2019-05-06 09:25:57 +02:00
cfg Config
MQCWMsgQueue chan ChatWarsMessage
SQLMsgIdentifyQueue chan int64
2019-05-08 12:13:47 +02:00
msgParsingRules map[int]MessageParsingRule
2019-05-02 12:11:27 +02:00
)
2019-04-15 13:21:16 +02:00
func PrintText(m *tb.Message) {
fmt.Printf("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
return
}
func main() {
2019-05-02 12:11:27 +02:00
log.Println("Starting Chirpnest...")
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Parsing config
2019-05-03 05:58:36 +02:00
flag.Parse()
2019-05-02 12:14:07 +02:00
err := gcfg.ReadFileInto(&cfg, *config)
2019-05-11 05:29:09 +02:00
failOnError(err, "Parsing config")
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Connecting to DB
switch cfg.SQL.Driver {
2019-05-03 05:58:36 +02:00
case "mysql":
2019-05-09 12:36:45 +02:00
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true")
2019-05-11 05:29:09 +02:00
failOnError(err, "Opening MySQL connection")
2019-05-03 05:58:36 +02:00
defer db.Close()
2019-05-02 12:11:27 +02:00
}
2019-05-03 05:58:36 +02:00
2019-05-02 12:11:27 +02:00
// Check if any issue
err = db.Ping()
2019-05-11 05:29:09 +02:00
failOnError(err, "Checking MySQL connection")
log.Println("SQL connection initialized")
2019-05-03 05:58:36 +02:00
if *initdb {
2019-05-02 12:16:05 +02:00
initDB()
}
2019-05-03 05:58:36 +02:00
2019-05-08 12:35:43 +02:00
msgParsingRules, err = loadMsgParsingRules()
2019-05-08 13:15:17 +02:00
logOnError(err, "Message parsing rules")
2019-05-08 12:13:47 +02:00
2019-05-11 05:29:09 +02:00
// 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")
2019-05-11 05:37:33 +02:00
go BotHandlers(b)
2019-05-11 05:36:17 +02:00
u := tb.User{
2019-05-11 05:36:38 +02:00
ID: int(cfg.Bot.Admin),
2019-05-11 05:36:17 +02:00
}
b.Send(&u, "Bot restarted")
2019-05-04 11:01:57 +02:00
MQCWMsgQueue = make(chan ChatWarsMessage, 100)
2019-05-06 09:25:57 +02:00
SQLMsgIdentifyQueue = make(chan int64, 100)
2019-05-06 04:54:24 +02:00
2019-05-06 09:25:57 +02:00
for w := 1; w <= MQGetMsgWorkers; w++ {
2019-05-04 11:15:33 +02:00
go MQGetMsgWorker(w, MQCWMsgQueue)
}
2019-05-06 09:25:57 +02:00
for w := 1; w <= SQLCWMsgWorkers; w++ {
go SQLCWMsgWorker(w, MQCWMsgQueue, SQLMsgIdentifyQueue)
2019-05-04 11:01:57 +02:00
}
2019-05-06 09:25:57 +02:00
for w := 1; w <= SQLIdentifyMsgWorkers; w++ {
go SQLIdentifyMsgWorker(w, SQLMsgIdentifyQueue)
2019-05-05 13:29:28 +02:00
}
2019-05-10 11:23:47 +02:00
for w := 1; w <= SQLJobWorkers; w++ {
go SQLJobWorker(w)
}
2019-04-15 13:21:16 +02:00
2019-05-11 06:54:12 +02:00
log.Println("Bot started !")
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
// Main loop
for {
time.Sleep(1 * time.Second)
}
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
}