This commit is contained in:
shoopea 2019-05-15 10:18:16 +08:00
parent 27dba642f7
commit 5125130724
3 changed files with 52 additions and 2 deletions

View File

@ -4,3 +4,9 @@ password = guest
host = localhost:5672 host = localhost:5672
sendqueue = chatwars sendqueue = chatwars
receivequeue = guest receivequeue = guest
[listen]
war = false
warmini = false
auctions = false

11
main.go
View File

@ -54,6 +54,11 @@ type Config struct {
SendQueue string SendQueue string
ReceiveQueue string ReceiveQueue string
} }
Listen struct {
War bool
WarMini bool
Auctions bool
}
} }
var ( var (
@ -66,6 +71,7 @@ var (
ownUserID64 = int64(0) ownUserID64 = int64(0)
ownUserID32 = int32(0) ownUserID32 = int32(0)
lastOwnTDMsg = time.Time
MQCWMsgQueue chan ChatWarsMessage MQCWMsgQueue chan ChatWarsMessage
MQCWCmdQueue chan ChatWarsCommand MQCWCmdQueue chan ChatWarsCommand
) )
@ -150,7 +156,12 @@ func main() {
go MQReceiveMsgWorker(w, MQCWCmdQueue) go MQReceiveMsgWorker(w, MQCWCmdQueue)
} }
lastOwnTDMsg = time.Now()
go ListenCW(client) go ListenCW(client)
go ListenMQ(client, MQCWCmdQueue)
go ListenMe(client)
fmt.Println("Started !") fmt.Println("Started !")
// Main loop // Main loop

35
td.go
View File

@ -14,13 +14,46 @@ func ListenCW(c *tdlib.Client) {
updateMsg := (*msg).(*tdlib.UpdateNewMessage) updateMsg := (*msg).(*tdlib.UpdateNewMessage)
chatID := updateMsg.Message.ChatID chatID := updateMsg.Message.ChatID
forwardInfo := updateMsg.Message.ForwardInfo forwardInfo := updateMsg.Message.ForwardInfo
if (chatID == user_chtwrsbot || chatID == chat_war || chatID == chat_war_mini || chatID == chat_auction) && forwardInfo == nil { if (chatID == user_chtwrsbot ||
(chatID == chat_war && cfg.Listen.War) ||
(chatID == chat_war_mini && cfg.Listen.WarMini) ||
(chatID == chat_auction && cfg.Listen.Auctions)) &&
forwardInfo == nil {
return true return true
} }
return false return false
} }
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan {
lastOwnTDMsg = time.Now()
}
}
func ListenMQ(c *tdlib.Client, msgs <-chan ChatWarsCommand) {
for m := range msgs {
for now := time.Now(); lastOwnTDMsg.Add(time.Second) > now; {
log.Printf("ListenMQ : channel busy, waiting before acting...\n")
time.Sleep(time.Until(lastOwnTDMsg.Add(time.Second)))
now = time.Now()
}
log.Printf("ListenMQ : Consuming msg\n")
}
}
func ListenMe(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
senderUserID := updateMsg.Message.SenderUserID
if senderUserID == ownUserID32 {
return true
} else {
return false
}
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100) receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for newMsg := range receiver.Chan { for newMsg := range receiver.Chan {
updateMsg := (newMsg).(*tdlib.UpdateNewMessage) updateMsg := (newMsg).(*tdlib.UpdateNewMessage)