This commit is contained in:
shoopea 2019-05-14 17:34:15 +08:00
parent 121ecf0ffc
commit 27dba642f7
5 changed files with 88 additions and 11 deletions

1
def.go Normal file
View File

@ -0,0 +1 @@
package main

View File

@ -1,5 +1,6 @@
[rabbit]
user = guest
password = guest
host = localhost:5672
queue = chatwars
user = guest
password = guest
host = localhost:5672
sendqueue = chatwars
receivequeue = guest

41
main.go
View File

@ -20,14 +20,39 @@ type ChatWarsMessage struct {
Text string `json:"text"`
}
const user_chtwrsbot = 408101137
type ChatWarsCommand struct {
Type int64 `json:"type"`
FromChatID64 int64 `json:"from_chat_id"`
FromUserID64 int64 `json:"from_user_id"`
FromMsgID64 int64 `json:"from_msg_id"`
ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"`
Text int64 `json:"text"`
}
const (
commandForwardMsg = 1
commandReplyMsg = 2
commandSendMsg = 3
commandDeleteMsg = 4
commandRefreshMsg = 5
)
const (
user_chtwrsbot = 408101137
chat_darkwing = -1001080526540
chat_war = -1001108112459
chat_war_mini = -1001277259728
chat_auction = -1001209424945
)
type Config struct {
Rabbit struct {
User string
Password string
Host string
Queue string
User string
Password string
Host string
SendQueue string
ReceiveQueue string
}
}
@ -42,6 +67,7 @@ var (
ownUserID64 = int64(0)
ownUserID32 = int32(0)
MQCWMsgQueue chan ChatWarsMessage
MQCWCmdQueue chan ChatWarsCommand
)
func main() {
@ -119,6 +145,11 @@ func main() {
return
}
MQCWCmdQueue = make(chan ChatWarsCommand, 100)
for w := 1; w <= 3; w++ {
go MQReceiveMsgWorker(w, MQCWCmdQueue)
}
go ListenCW(client)
fmt.Println("Started !")

45
mq.go
View File

@ -9,7 +9,7 @@ import (
)
func MQSendMsgWorker(id int, msgs <-chan ChatWarsMessage) {
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.SendQueue)
failOnError(err, "MQSendMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
defer conn.Close()
@ -50,3 +50,46 @@ func MQSendMsgWorker(id int, msgs <-chan ChatWarsMessage) {
log.Printf("MQSendMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}
func MQReceiveMsgWorker(id int, cmd <-chan ChatWarsCommand) {
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.ReceiveQueue)
failOnError(err, "MQReceiveMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "MQReceiveMsgWorker["+strconv.Itoa(id)+"] : 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, "MQReceiveMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
m, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "MQReceiveMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a consumer")
for d := range m {
// log.Printf("MQReceiveMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQReceiveMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
if err == nil {
cmd <- x
}
}
log.Printf("MQReceiveMsgWorker[" + strconv.Itoa(id) + "] : Closing.")
}

3
td.go
View File

@ -14,9 +14,10 @@ func ListenCW(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID
forwardInfo := updateMsg.Message.ForwardInfo
if chatID == user_chtwrsbot && forwardInfo == nil {
if (chatID == user_chtwrsbot || chatID == chat_war || chatID == chat_war_mini || chatID == chat_auction) && forwardInfo == nil {
return true
}
return false
}