This commit is contained in:
shoopea 2019-05-03 11:58:36 +08:00
parent 09439ce697
commit a465276246
5 changed files with 249 additions and 192 deletions

12
bot.go
View File

@ -1,10 +1,10 @@
package main
import (
"time"
"log"
"fmt"
tb "gopkg.in/tucnak/telebot.v2"
"log"
"time"
)
var (
@ -35,14 +35,14 @@ func StartBot() {
func botPhoto(m *tb.Message) {
fmt.Println("OnPhoto :", m.Text)
// photos only
// photos only
}
func botHello(m *tb.Message) {
if !m.Private() {
return
}
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
// fmt.Println("Hello payload :", m.Payload) // <PAYLOAD>
PrintText(m)
b.Send(m.Sender, "hello world")
}
@ -50,12 +50,12 @@ func botHello(m *tb.Message) {
func botChannelPost(m *tb.Message) {
fmt.Println("OnChannelPost :", m.Text)
PrintText(m)
// channel posts only
// channel posts only
}
func botQuery(q *tb.Query) {
fmt.Println("Query ?")
// incoming inline queries
// incoming inline queries
}
func botText(m *tb.Message) {

10
main.go
View File

@ -1,14 +1,14 @@
package main
import (
"time"
"log"
"database/sql"
"flag"
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/gcfg.v1"
tb "gopkg.in/tucnak/telebot.v2"
"log"
"time"
)
type Config struct {
@ -65,7 +65,7 @@ func main() {
// 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)
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)
}
@ -81,7 +81,7 @@ func main() {
log.Println("SQL connection initialized")
}
if (*initdb) {
if *initdb {
initDB()
}

49
mq.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"log"
"github.com/streadway/amqp"
)
func MQMainReceive() {
conn, err := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars")
failOnError(err, "MQMainReceive : Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "MQMainReceive : Failed to open a channel")
defer ch.Close()
q, err := ch.QueueDeclare(
"msg", // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "MQMainReceive : Failed to declare a queue")
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "MQMainReceive : Failed to register a consumer")
forever := make(chan bool)
go func() {
for d := range msgs {
log.Printf("MQMainReceive : Received a message: %s", d.Body)
}
}()
<-forever
}

7
sql.go
View File

@ -2,10 +2,8 @@ package main
import (
"log"
)
func initDB() {
log.Println("Setting up database...")
@ -52,7 +50,7 @@ func initDB() {
}
log.Println("Database cleaned up")
_, err = db.Exec( `CREATE TABLE user (
_, err = db.Exec(`CREATE TABLE user (
id BIGINT UNSIGNED NOT NULL,
user_id VARCHAR(32) NOT NULL,
name VARCHAR(80) NOT NULL,
@ -66,7 +64,7 @@ func initDB() {
log.Fatal(err)
}
_, err = db.Exec( `CREATE TABLE msg (
_, err = db.Exec(`CREATE TABLE msg (
id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
direction ENUM('incoming', 'outgoing'),
@ -79,4 +77,3 @@ func initDB() {
log.Println("Database set up")
}

11
utils.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"log"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}