2019-04-15 13:21:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"log"
|
|
|
|
"fmt"
|
2019-05-02 12:11:27 +02:00
|
|
|
"database/sql"
|
|
|
|
_ "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-02 12:11:27 +02:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
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...")
|
|
|
|
|
|
|
|
// Parsing config
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-04-15 13:21:16 +02:00
|
|
|
b, err := tb.NewBot(tb.Settings{
|
2019-05-02 12:11:27 +02:00
|
|
|
Token: cfg.Telegram.Token,
|
|
|
|
URL: cfg.Telegram.URL,
|
2019-04-15 13:21:16 +02:00
|
|
|
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Handle("/hello", func(m *tb.Message) {
|
|
|
|
if !m.Private() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
|
|
|
|
PrintText(m)
|
|
|
|
b.Send(m.Sender, "hello world")
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Handle(tb.OnPhoto, func(m *tb.Message) {
|
|
|
|
fmt.Println("OnPhoto :", m.Text)
|
|
|
|
// photos only
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Handle(tb.OnChannelPost, func (m *tb.Message) {
|
|
|
|
fmt.Println("OnChannelPost :", m.Text)
|
|
|
|
PrintText(m)
|
|
|
|
// channel posts only
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Handle(tb.OnQuery, func (q *tb.Query) {
|
|
|
|
fmt.Println("Query ?")
|
|
|
|
// incoming inline queries
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Handle(tb.OnText, func(m *tb.Message) {
|
|
|
|
PrintText(m)
|
|
|
|
// all the text messages that weren't
|
|
|
|
// captured by existing handlers
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Start()
|
|
|
|
}
|