This commit is contained in:
shoopea 2019-12-29 11:52:50 +08:00
parent b787218d08
commit d0c8dc15c2
2 changed files with 41 additions and 3 deletions

4
def.go
View File

@ -4,6 +4,10 @@ import (
"time" "time"
) )
type DataBackup struct {
Messages []ChatWarsMessage `json:"messages"`
}
type ChatWarsMessage struct { type ChatWarsMessage struct {
TGUserID64 int64 `json:"tg_user_id"` TGUserID64 int64 `json:"tg_user_id"`
TGSenderUserID64 int64 `json:"tg_sender_user_id"` TGSenderUserID64 int64 `json:"tg_sender_user_id"`

40
main.go
View File

@ -1,11 +1,14 @@
package main package main
import ( import (
// "encoding/json" "bytes"
"encoding/json"
"fmt" "fmt"
"log" "log"
"math" "math"
"os"
"time" "time"
"zip"
"github.com/Arman92/go-tdlib" "github.com/Arman92/go-tdlib"
) )
@ -72,7 +75,7 @@ func main() {
fmt.Println("Got ChatWars message") fmt.Println("Got ChatWars message")
time.Sleep(11 * time.Second) time.Sleep(1 * time.Second)
fmt.Println("Now getting history...") fmt.Println("Now getting history...")
@ -97,6 +100,10 @@ func getHistory(c *tdlib.Client, chatID64 *int64) {
failOnError(err, "getHistory : GetChat") failOnError(err, "getHistory : GetChat")
fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title) fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title)
bkp := DataBackup{}
s := new([]ChatWarsMessage)
msgs := *s
for lastParsedID64 >= 0 { for lastParsedID64 >= 0 {
prevLastParsedID64 := lastParsedID64 prevLastParsedID64 := lastParsedID64
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false) msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false)
@ -143,7 +150,7 @@ func getHistory(c *tdlib.Client, chatID64 *int64) {
} }
if m.ID64 != 0 { if m.ID64 != 0 {
// export msg here msgs = append(msgs, m)
msgCount = msgCount + 1 msgCount = msgCount + 1
} }
default: default:
@ -177,6 +184,33 @@ func getHistory(c *tdlib.Client, chatID64 *int64) {
} }
log.Printf("Exported %d messages.\n", msgCount) log.Printf("Exported %d messages.\n", msgCount)
fmt.Printf("Exported %d messages.\n", msgCount) fmt.Printf("Exported %d messages.\n", msgCount)
bkp.Messages = msgs
b, err := json.Marshal(bkp)
logOnError(err, "getHistory : Marshal")
zbuf := new(bytes.Buffer)
zw := zip.NewWriter(zbuf)
zf, err := zw.Create(`backup.json`)
logOnError(err, "getHistory : Create")
_, err = zf.Write(b)
logOnError(err, "getHistory : Write")
err = zw.Close()
logOnError(err, "getHistory : Close")
f, err := os.Create("./backup.zip")
logOnError(err, "getHistory : Create")
defer f.Close()
w := bufio.NewWriter(f)
zbuf.WriteTo(w)
w.Flush()
return
} }
func OwnUserID(c *tdlib.Client) int32 { func OwnUserID(c *tdlib.Client) int32 {