This commit is contained in:
shoopea 2019-06-08 23:41:50 +08:00
parent 53ea74fae9
commit 0ea4664d3f
4 changed files with 41 additions and 25 deletions

6
bot.go
View File

@ -186,7 +186,7 @@ func botMsgExportAll(m *tb.Message) {
return return
} }
f, err := exportMessages() b, err := zipMessages()
logOnError(err, "botMsgExportAll : exportMessages") logOnError(err, "botMsgExportAll : exportMessages")
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{
@ -200,9 +200,11 @@ func botMsgExportAll(m *tb.Message) {
return return
} }
d := &tb.Document{File: tb.FromReader(bytes.NewReader(b))}
c := TGCommand{ c := TGCommand{
Type: commandReplyFile, Type: commandReplyFile,
File: f, Document: d,
FromMsgID64: int64(m.ID), FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID, FromChatID64: m.Chat.ID,
} }

5
def.go
View File

@ -43,7 +43,7 @@ type TGCommand struct {
ToChatID64 int64 `json:"to_chat_id"` ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"` ToUserID64 int64 `json:"to_user_id"`
Text string `json:"text"` Text string `json:"text"`
File tb.File `json:"file"` Document tb.Document `json:"document"`
} }
type ChatWarsCastle struct { type ChatWarsCastle struct {
@ -244,7 +244,8 @@ const (
commandSendMsg = 3 commandSendMsg = 3
commandDeleteMsg = 4 commandDeleteMsg = 4
commandRefreshMsg = 5 commandRefreshMsg = 5
commandSendFile = 6 commandSendDoc = 6
commandReplyDoc = 7
objTypeUser = 1 objTypeUser = 1
objTypeGuild = 2 objTypeGuild = 2

13
msg.go
View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"archive/zip"
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"log" "log"
"regexp" "regexp"
@ -271,14 +273,16 @@ func parseSubTypeMessagePillageInc(m *ChatWarsMessage, r *regexp.Regexp) (*ChatW
return &cwm, nil return &cwm, nil
} }
func exportMessages() (tb.File, error) { func zipMessages() ([]byte, error) {
var f tb.File
bkp := DataBackup{} bkp := DataBackup{}
ids := getSQLListID64(`SELECT om.obj_id id FROM obj_msg om;`) ids := getSQLListID64(`SELECT om.obj_id id FROM obj_msg om;`)
for _, id := range ids { for _, id := range ids {
m := getMsg(id) m, err := getMsg(id)
logOnError(err, "zipMessages : getMsg")
if err == nil {
append(bkp.Messages, m) append(bkp.Messages, m)
} }
}
b, err := json.Marshal(bkp) b, err := json.Marshal(bkp)
logOnError(err, "exportMessages : Marshal") logOnError(err, "exportMessages : Marshal")
if err != nil { if err != nil {
@ -305,7 +309,6 @@ func exportMessages() (tb.File, error) {
return nil, err return nil, err
} }
f = f.FromReader(bytes.NewReader(zbuf.Bytes())) return zbuf.Bytes(), nil
return f, nil
} }

View File

@ -256,34 +256,44 @@ func TGCmdWorker(id int, b *tb.Bot, cmds <-chan TGCommand) {
Chat: &ch, Chat: &ch,
} }
_, err := b.Reply(&m, c.Text) _, 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: case commandSendMsg:
if c.ToChatID64 != 0 { if c.ToChatID64 != 0 {
ch := tb.Chat{ ch := tb.Chat{
ID: c.ToChatID64, ID: c.ToChatID64,
} }
_, err := b.Send(&ch, c.Text) _, 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 { } else if c.ToUserID64 != 0 {
ch := tb.Chat{ ch := tb.Chat{
ID: c.ToUserID64, ID: c.ToUserID64,
} }
_, err := b.Send(&ch, c.Text) _, 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 { if c.ToChatID64 != 0 {
ch := tb.Chat{ ch := tb.Chat{
ID: c.ToChatID64, ID: c.ToChatID64,
} }
_, err := b.Send(&ch, c.File) _, err := b.Send(&ch, c.Document)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File") logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
} else if c.ToUserID64 != 0 { } else if c.ToUserID64 != 0 {
ch := tb.Chat{ ch := tb.Chat{
ID: c.ToUserID64, ID: c.ToUserID64,
} }
_, err := b.Send(&ch, c.File) _, err := b.Send(&ch, c.Document)
logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : File") logOnError(err, "TGCmdWorker["+strconv.Itoa(id)+"] : SendDocument Chat")
} }
default: default: