This commit is contained in:
shoopea 2019-05-02 18:11:27 +08:00
parent f07f365409
commit 50950c0bef
3 changed files with 160 additions and 4 deletions

17
chirpnest.sample.cfg Normal file
View File

@ -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

68
main.go
View File

@ -4,21 +4,81 @@ import (
"time" "time"
"log" "log"
"fmt" "fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
tb "gopkg.in/tucnak/telebot.v2" 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) { 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) 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 return
} }
func main() { 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{ b, err := tb.NewBot(tb.Settings{
Token: "725809138:AAGp5hGQ7KQHxWtXZH93JUAxaHlJKtbXjlE", Token: cfg.Telegram.Token,
// You can also set custom API URL. If field is empty it equals to "https://api.telegram.org" URL: cfg.Telegram.URL,
// URL: "http://195.129.111.17:8012",
URL: "https://api.telegram.org",
Poller: &tb.LongPoller{Timeout: 10 * time.Second}, Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}) })

79
sql.go Normal file
View File

@ -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")
}