diff --git a/bot.go b/bot.go index 6f72994..db0265d 100644 --- a/bot.go +++ b/bot.go @@ -186,7 +186,7 @@ func botMsgExportAll(m *tb.Message) { return } - f, err := exportMessages() + b, err := zipMessages() logOnError(err, "botMsgExportAll : exportMessages") if err != nil { c := TGCommand{ @@ -200,9 +200,11 @@ func botMsgExportAll(m *tb.Message) { return } + d := &tb.Document{File: tb.FromReader(bytes.NewReader(b))} + c := TGCommand{ Type: commandReplyFile, - File: f, + Document: d, FromMsgID64: int64(m.ID), FromChatID64: m.Chat.ID, } diff --git a/def.go b/def.go index c8a46ba..fe70322 100644 --- a/def.go +++ b/def.go @@ -36,14 +36,14 @@ type ChatWarsClient struct { } type TGCommand 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 string `json:"text"` - File tb.File `json:"file"` + 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 string `json:"text"` + Document tb.Document `json:"document"` } type ChatWarsCastle struct { @@ -244,7 +244,8 @@ const ( commandSendMsg = 3 commandDeleteMsg = 4 commandRefreshMsg = 5 - commandSendFile = 6 + commandSendDoc = 6 + commandReplyDoc = 7 objTypeUser = 1 objTypeGuild = 2 diff --git a/msg.go b/msg.go index 9217636..6d10717 100644 --- a/msg.go +++ b/msg.go @@ -1,7 +1,9 @@ package main import ( + "archive/zip" "bytes" + "encoding/json" "fmt" "log" "regexp" @@ -271,13 +273,15 @@ func parseSubTypeMessagePillageInc(m *ChatWarsMessage, r *regexp.Regexp) (*ChatW return &cwm, nil } -func exportMessages() (tb.File, error) { - var f tb.File +func zipMessages() ([]byte, error) { bkp := DataBackup{} ids := getSQLListID64(`SELECT om.obj_id id FROM obj_msg om;`) for _, id := range ids { - m := getMsg(id) - append(bkp.Messages, m) + m, err := getMsg(id) + logOnError(err, "zipMessages : getMsg") + if err == nil { + append(bkp.Messages, m) + } } b, err := json.Marshal(bkp) logOnError(err, "exportMessages : Marshal") @@ -305,7 +309,6 @@ func exportMessages() (tb.File, error) { return nil, err } - f = f.FromReader(bytes.NewReader(zbuf.Bytes())) - return f, nil + return zbuf.Bytes(), nil } diff --git a/workers.go b/workers.go index 658eb16..27886cf 100644 --- a/workers.go +++ b/workers.go @@ -256,34 +256,44 @@ func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) { Chat: &ch, } _, err := b.Reply(&m, c.Text) - logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Reply") + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : ReplyMsg") + case commandReplyDocument: + ch := tb.Chat{ + ID: c.FromChatID64, + } + m := tb.Message{ + ID: int(c.FromMsgID64), + Chat: &ch, + } + _, err := b.Reply(&m, c.Document) + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : ReplyDocument") case commandSendMsg: if c.ToChatID64 != 0 { ch := tb.Chat{ ID: c.ToChatID64, } _, err := b.Send(&ch, c.Text) - logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Send") + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg Chat") } else if c.ToUserID64 != 0 { ch := tb.Chat{ ID: c.ToUserID64, } _, err := b.Send(&ch, c.Text) - logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : Send") + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendMsg User") } - case commandSendFile: + case commandSendDocument: if c.ToChatID64 != 0 { ch := tb.Chat{ ID: c.ToChatID64, } - _, err := b.Send(&ch, c.File) - logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File") + _, err := b.Send(&ch, c.Document) + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat") } else if c.ToUserID64 != 0 { ch := tb.Chat{ ID: c.ToUserID64, } - _, err := b.Send(&ch, c.File) - logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File") + _, err := b.Send(&ch, c.Document) + logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat") } default: