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

4
bot.go
View File

@ -1,10 +1,10 @@
package main package main
import ( import (
"time"
"log"
"fmt" "fmt"
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
"log"
"time"
) )
var ( var (

View File

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

3
sql.go
View File

@ -2,10 +2,8 @@ package main
import ( import (
"log" "log"
) )
func initDB() { func initDB() {
log.Println("Setting up database...") log.Println("Setting up database...")
@ -79,4 +77,3 @@ func initDB() {
log.Println("Database set up") 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)
}
}