diff --git a/def.go b/def.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/def.go @@ -0,0 +1 @@ +package main diff --git a/gocw2.sample.cfg b/gocw2.sample.cfg index 8b33a65..77cbfdf 100644 --- a/gocw2.sample.cfg +++ b/gocw2.sample.cfg @@ -1,5 +1,6 @@ [rabbit] -user = guest -password = guest -host = localhost:5672 -queue = chatwars \ No newline at end of file +user = guest +password = guest +host = localhost:5672 +sendqueue = chatwars +receivequeue = guest \ No newline at end of file diff --git a/main.go b/main.go index 590fca0..5e5e1ca 100644 --- a/main.go +++ b/main.go @@ -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 !") diff --git a/mq.go b/mq.go index 2278f95..3043636 100644 --- a/mq.go +++ b/mq.go @@ -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.") + +} diff --git a/td.go b/td.go index 0cc4e13..f256a3c 100644 --- a/td.go +++ b/td.go @@ -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 }