diff --git a/chirpnest.sample.cfg b/chirpnest.sample.cfg new file mode 100644 index 0000000..b20a949 --- /dev/null +++ b/chirpnest.sample.cfg @@ -0,0 +1,17 @@ +[telegram] +url = https://api.telegram.org +token = null + +[SQL] +driver = mysql # mysql +type = tcp # tcp, unix +address = 127.0.0.1:3306 # ip:port, path +username = chirpnest +password = chirpnest +database = chirpnest + +[rabbit] +user = chirpnest +password = chirpnest +address = localhost:5672 +queue = chirpnest \ No newline at end of file diff --git a/main.go b/main.go index 48ccaaf..cbc88bf 100644 --- a/main.go +++ b/main.go @@ -4,21 +4,81 @@ import ( "time" "log" "fmt" + "database/sql" + _ "github.com/go-sql-driver/mysql" tb "gopkg.in/tucnak/telebot.v2" ) +type Config struct { + Telegram struct { + URL string + Token string + } + SQL struct { + Driver string + Type string + Address string + Username string + Password string + Database string + } + Rabbit struct { + User string + Password string + Host string + Queue string + } +} + +var ( + config = flag.String("config", "chirpnest.cfg", "config file path") + initdb = flag.Bool("initdb", false, "initialize bot database") + + db *sql.DB + + cfg Config +) + 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() { + log.Println("Starting Chirpnest...") + + // Parsing config + flag.Parse() + + err = gcfg.ReadFileInto(&cfg, *config) + if err != nil { + log.Fatalf("Failed to parse gcfg data: %s", err) + } + + // 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) + if err != nil { + log.Fatal(err) + } + defer db.Close() + + } + + // Check if any issue + err = db.Ping() + if err != nil { + log.Fatal(err) + } else { + log.Println("SQL connection initialized") + } + + // Registering bot b, err := tb.NewBot(tb.Settings{ - Token: "725809138:AAGp5hGQ7KQHxWtXZH93JUAxaHlJKtbXjlE", - // You can also set custom API URL. If field is empty it equals to "https://api.telegram.org" - // URL: "http://195.129.111.17:8012", - URL: "https://api.telegram.org", + Token: cfg.Telegram.Token, + URL: cfg.Telegram.URL, Poller: &tb.LongPoller{Timeout: 10 * time.Second}, }) diff --git a/sql.go b/sql.go new file mode 100644 index 0000000..bc562f4 --- /dev/null +++ b/sql.go @@ -0,0 +1,79 @@ +package main + +import ( + "log" + +) + + +func initDB() { + log.Println("Setting up database...") + + tx, err := db.Begin() + if err != nil { + log.Fatal(err) + } + defer tx.Rollback() + + _, err = tx.Exec("set foreign_key_checks = 0") + if err != nil { + log.Fatal(err) + } + + var name string + rows, err := db.Query("show tables") + if err != nil { + log.Fatal(err) + } + for rows.Next() { + err = rows.Scan(&name) + if err != nil { + log.Fatal(err) + } + _, err = tx.Exec("drop table " + name) + if err != nil { + log.Fatal(err) + } + } + err = rows.Err() + if err != nil { + log.Fatal(err) + } + rows.Close() + + _, err = tx.Exec("set foreign_key_checks = 1") + if err != nil { + log.Fatal(err) + } + + err = tx.Commit() + if err != nil { + log.Fatal(err) + } + + log.Println("Database cleaned up") + _, err = db.Exec( `CREATE TABLE user ( + id BIGINT UNSIGNED NOT NULL, + user_id VARCHAR(32) NOT NULL, + name VARCHAR(80) NOT NULL, + guild_id SMALLINT(5), + PRIMARY KEY (id), + ) ENGINE = InnoDB ROW_FORMAT = DEFAULT CHARSET=utf8mb4;`) + if err != nil { + log.Fatal(err) + } + + _, err = db.Exec( `CREATE TABLE msg ( + id BIGINT UNSIGNED NOT NULL, + user_id BIGINT UNSIGNED NOT NULL, + direction ENUM('incoming', 'outgoing'), + text VARCHAR(4096) NOT NULL, + FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE + ) ENGINE = InnoDB ROW_FORMAT = DEFAULT CHARSET=utf8mb4;`) + if err != nil { + log.Fatal(err) + } + + log.Println("Database set up") +} +