chirpnest/main.go

115 lines
1.9 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
}
2019-05-03 09:15:16 +02:00
type MsgDirection string
const (
2019-05-03 10:05:58 +02:00
Incoming MsgDirection = `incoming`
Outgoing MsgDirection = `outgoing`
2019-05-03 09:15:16 +02:00
)
type ChatWarsMessage struct {
2019-05-03 10:00:46 +02:00
MsgID64 int64 `json:"msg_id"`
2019-05-03 13:33:12 +02:00
ChatID64 int64 `json:"chat_id"`
2019-05-03 10:00:46 +02:00
UserID64 int64 `json:"user_id"`
Direction MsgDirection `json:"direction"`
MsgText string `json:"msg"`
MsgDate int32 `json:"date"`
2019-05-03 09:15:16 +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
cfg Config
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-02 12:11:27 +02:00
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
}
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":
db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database)
if err != nil {
log.Fatal(err)
}
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()
if err != nil {
log.Fatal(err)
} else {
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-03 05:23:08 +02:00
go StartBot()
2019-05-03 06:00:13 +02:00
go MQMainReceive()
2019-04-15 13:21:16 +02:00
2019-05-03 05:58:36 +02:00
fmt.Println("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
}