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
}
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,
}

5
def.go
View File

@ -43,7 +43,7 @@ type TGCommand struct {
ToChatID64 int64 `json:"to_chat_id"`
ToUserID64 int64 `json:"to_user_id"`
Text string `json:"text"`
File tb.File `json:"file"`
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

13
msg.go
View File

@ -1,7 +1,9 @@
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"log"
"regexp"
@ -271,14 +273,16 @@ 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)
m, err := getMsg(id)
logOnError(err, "zipMessages : getMsg")
if err == nil {
append(bkp.Messages, m)
}
}
b, err := json.Marshal(bkp)
logOnError(err, "exportMessages : Marshal")
if err != nil {
@ -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
}

View File

@ -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: