diff --git a/bot.go b/bot.go index 65efe8d..ead9f67 100644 --- a/bot.go +++ b/bot.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "net/http" "regexp" "strconv" "time" @@ -264,6 +265,34 @@ func botMsgLoad(m *tb.Message) { if !m.Private() { return } + r := regexp.MustCompile("^(http|https)://[a-z0-9./]+.zip$") // https://dump.siteop.biz/20190609163137.backup.zip + if r.MatchString(m.Payload) { + c = TGCommand{ + Type: commandReplyMsg, + Text: "Got file", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + + resp, err := http.Get(m.Payload) + if err != nil { + return err + } + defer resp.Body.Close() + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + UnzipMessages(buf.Bytes()) + + } else { + c = TGCommand{ + Type: commandReplyMsg, + Text: "No file", + FromMsgID64: int64(m.ID), + FromChatID64: m.Chat.ID, + } + TGCmdQueue <- c + } return } diff --git a/msg.go b/msg.go index c52877d..d544375 100644 --- a/msg.go +++ b/msg.go @@ -278,7 +278,6 @@ func zipMessages() ([]byte, error) { s := new([]ChatWarsMessage) msgs := *s ids := getSQLListID64(`SELECT om.obj_id id FROM obj_msg om;`) - log.Printf("zipMessages : Retrieved %d message ids.\n", len(ids)) i := 0 for _, id := range ids { m, err := getObjMsg(id) @@ -293,16 +292,13 @@ func zipMessages() ([]byte, error) { } - log.Printf("zipMessages : Assigning messages.\n") bkp.Messages = msgs - log.Printf("zipMessages : Marshalling messages.\n") b, err := json.Marshal(bkp) logOnError(err, "exportMessages : Marshal") if err != nil { return nil, err } - log.Printf("zipMessages : Compressing messages.\n") zbuf := new(bytes.Buffer) zw := zip.NewWriter(zbuf) zf, err := zw.Create(`backup.json`) @@ -323,8 +319,11 @@ func zipMessages() ([]byte, error) { return nil, err } - log.Printf("zipMessages : Returning messages.\n") - return zbuf.Bytes(), nil } + +func UnzipMessages(z []byte) error { + log.Printf("UnzipMessages : %d bytes.\n", len(z)) + return nil +}