2019-05-03 11:19:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-05-03 11:22:17 +02:00
|
|
|
"math"
|
2019-05-03 11:19:20 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Arman92/go-tdlib"
|
|
|
|
"github.com/streadway/amqp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ListenCW(c *tdlib.Client) {
|
|
|
|
eventFilter := func(msg *tdlib.TdMessage) bool {
|
|
|
|
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
|
|
|
|
chatID := updateMsg.Message.ChatID
|
|
|
|
if chatID == user_chtwrsbot {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
ch, err := conn.Channel()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ch.Close()
|
|
|
|
|
|
|
|
q, err := ch.QueueDeclare(
|
|
|
|
"msg", // name
|
|
|
|
false, // durable
|
|
|
|
false, // delete when unused
|
|
|
|
false, // exclusive
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
|
|
|
|
for newMsg := range receiver.Chan {
|
|
|
|
updateMsg := (newMsg).(*tdlib.UpdateNewMessage)
|
|
|
|
senderUserID := updateMsg.Message.SenderUserID
|
|
|
|
mType := updateMsg.Message.Content.GetMessageContentEnum()
|
|
|
|
if mType == "messageText" {
|
|
|
|
user, err := c.GetUser(senderUserID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("ListenCW:", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txt := updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
m := ChatWarsMessage{
|
|
|
|
UserID64: ownUserID64,
|
2019-05-03 13:36:50 +02:00
|
|
|
ChatID64: updateMsg.Message.ChatID,
|
2019-05-03 11:19:20 +02:00
|
|
|
MsgDate: updateMsg.Message.Date,
|
|
|
|
MsgText: string(txt),
|
|
|
|
MsgID64: updateMsg.Message.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
|
|
|
|
_ = ch.Publish(
|
|
|
|
"", // exchange
|
|
|
|
q.Name, // routing key
|
|
|
|
false, // mandatory
|
|
|
|
false, // immediate
|
|
|
|
amqp.Publishing{
|
|
|
|
ContentType: "application/json",
|
|
|
|
Body: []byte(b),
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
|
|
|
fmt.Println(" === CHATWARS (", user.Username, ") =====================================================================")
|
|
|
|
fmt.Println(txt, "\n")
|
|
|
|
fmt.Println("************ DETAILS ************")
|
|
|
|
fmt.Println("ID : ", updateMsg.Message.ID)
|
|
|
|
fmt.Println("Date : ", updateMsg.Message.Date)
|
|
|
|
fmt.Println("SenderUserID : ", updateMsg.Message.SenderUserID)
|
|
|
|
fmt.Println("ChatID : ", updateMsg.Message.ChatID)
|
|
|
|
fmt.Println("SendingState : ", updateMsg.Message.SendingState)
|
|
|
|
fmt.Println("IsOutgoing : ", updateMsg.Message.IsOutgoing)
|
|
|
|
fmt.Println("CanBeEdited : ", updateMsg.Message.CanBeEdited)
|
|
|
|
fmt.Println("CanBeForwarded : ", updateMsg.Message.CanBeForwarded)
|
|
|
|
fmt.Println("IsChannelPost : ", updateMsg.Message.IsChannelPost)
|
|
|
|
fmt.Println("ContainsUnreadMention : ", updateMsg.Message.ContainsUnreadMention)
|
|
|
|
fmt.Println("ForwardInfo : ", updateMsg.Message.ForwardInfo)
|
|
|
|
fmt.Println("ReplyToMessageID : ", updateMsg.Message.ReplyToMessageID)
|
|
|
|
fmt.Println("ViaBotUserID : ", updateMsg.Message.ViaBotUserID)
|
|
|
|
fmt.Println("================================================================================================================")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHistory(c *tdlib.Client) {
|
|
|
|
var lastMsgID64 int64 = int64(math.MaxInt64)
|
2019-05-03 12:31:57 +02:00
|
|
|
var msgCount int32 = 0
|
2019-05-03 13:13:30 +02:00
|
|
|
var lastMsgTime time.Time = time.Now()
|
2019-05-03 11:19:20 +02:00
|
|
|
|
2019-05-03 13:02:41 +02:00
|
|
|
fmt.Printf("Retrieving chat.\n")
|
2019-05-03 13:01:50 +02:00
|
|
|
|
2019-05-03 11:19:20 +02:00
|
|
|
conn, err := amqp.Dial("amqp://" + cfg.Rabbit.User + ":" + cfg.Rabbit.Password + "@" + cfg.Rabbit.Host + "/" + cfg.Rabbit.Queue)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
ch, err := conn.Channel()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ch.Close()
|
|
|
|
|
|
|
|
q, err := ch.QueueDeclare(
|
|
|
|
"msg", // name
|
|
|
|
false, // durable
|
|
|
|
false, // delete when unused
|
|
|
|
false, // exclusive
|
|
|
|
false, // no-wait
|
|
|
|
nil, // arguments
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for lastMsgID64 >= 0 {
|
2019-05-03 13:52:07 +02:00
|
|
|
msgs, err := c.GetChatHistory(user_chtwrsbot, lastMsgID64, 0, 10, false)
|
2019-05-03 11:19:20 +02:00
|
|
|
if err != nil {
|
2019-05-03 12:56:13 +02:00
|
|
|
if err.Error() == "timeout" {
|
|
|
|
logOnError(err, "Waiting...")
|
2019-05-03 13:10:03 +02:00
|
|
|
fmt.Printf("Waiting : %d messages retrieved from %s ...\n", msgCount, lastMsgTime.Format(time.RFC3339))
|
2019-05-03 12:56:13 +02:00
|
|
|
} else {
|
|
|
|
logOnError(err, "Cannot get history")
|
|
|
|
lastMsgID64 = -1
|
|
|
|
}
|
2019-05-03 13:01:50 +02:00
|
|
|
} else if msgs.TotalCount > 0 {
|
2019-05-03 11:22:17 +02:00
|
|
|
for _, msg := range msgs.Messages {
|
2019-05-03 12:29:07 +02:00
|
|
|
switch msg.Content.GetMessageContentEnum() {
|
2019-05-03 12:17:05 +02:00
|
|
|
case tdlib.MessageTextType:
|
|
|
|
m := ChatWarsMessage{
|
|
|
|
UserID64: int64(msg.SenderUserID),
|
2019-05-03 13:35:41 +02:00
|
|
|
ChatID64: msg.ChatID,
|
2019-05-03 12:17:05 +02:00
|
|
|
MsgDate: msg.Date,
|
|
|
|
MsgText: msg.Content.(*tdlib.MessageText).Text.Text,
|
|
|
|
MsgID64: msg.ID,
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:01:39 +02:00
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
logOnError(err, "getHistory : Marshaling message.")
|
|
|
|
}
|
2019-05-03 12:17:05 +02:00
|
|
|
|
2019-05-03 18:01:39 +02:00
|
|
|
err = ch.Publish(
|
2019-05-03 12:17:05 +02:00
|
|
|
"", // exchange
|
|
|
|
q.Name, // routing key
|
|
|
|
false, // mandatory
|
|
|
|
false, // immediate
|
|
|
|
amqp.Publishing{
|
|
|
|
ContentType: "application/json",
|
|
|
|
Body: []byte(b),
|
|
|
|
})
|
2019-05-03 18:01:39 +02:00
|
|
|
if err != nil {
|
|
|
|
logOnError(err, "getHistory : Publishing message.")
|
|
|
|
}
|
2019-05-03 12:17:05 +02:00
|
|
|
if msg.ID < lastMsgID64 {
|
|
|
|
lastMsgID64 = msg.ID
|
2019-05-03 13:13:04 +02:00
|
|
|
lastMsgTime = time.Unix(int64(msg.Date), 0)
|
2019-05-03 12:17:05 +02:00
|
|
|
}
|
2019-05-03 12:31:57 +02:00
|
|
|
msgCount = msgCount + 1
|
2019-05-03 12:17:05 +02:00
|
|
|
default:
|
2019-05-03 12:39:13 +02:00
|
|
|
log.Printf("getHistory : no handler for %s\n", msg.Content.GetMessageContentEnum())
|
2019-05-03 11:19:20 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 13:01:50 +02:00
|
|
|
} else {
|
|
|
|
lastMsgID64 = -1
|
2019-05-03 11:19:20 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 12:31:57 +02:00
|
|
|
log.Printf("Exported %d messages.\n", msgCount)
|
2019-05-03 12:39:40 +02:00
|
|
|
fmt.Printf("Exported %d messages.\n", msgCount)
|
2019-05-03 11:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ForwardMsg(c *tdlib.Client, msgID int64, fromChatID int64, toChatID int64) int64 {
|
|
|
|
msgIDs := make([]int64, 1)
|
|
|
|
msgIDs[0] = msgID
|
|
|
|
msgs, _ := c.ForwardMessages(toChatID, fromChatID, msgIDs, false, false, false)
|
|
|
|
if msgs != nil {
|
|
|
|
return msgs.Messages[0].ID
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteMsg(c *tdlib.Client, chatID int64, msgID int64) {
|
|
|
|
msgIDs := make([]int64, 1)
|
|
|
|
msgIDs[0] = msgID
|
|
|
|
c.DeleteMessages(chatID, msgIDs, false)
|
|
|
|
fmt.Println("Deleting message ", msgID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OwnUserID(c *tdlib.Client) int32 {
|
|
|
|
user, _ := c.GetMe()
|
|
|
|
return user.ID
|
|
|
|
}
|