forked from Rhymen/go-whatsapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
79 lines (66 loc) · 1.94 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"log"
"time"
"github.com/do3t/go-whatsapp"
)
// historyHandler for acquiring chat history
type historyHandler struct {
c *whatsapp.Conn
messages []string
}
func (h *historyHandler) ShouldCallSynchronously() bool {
return true
}
// handles and accumulates history's text messages.
// To handle images/documents/videos add corresponding handle functions
func (h *historyHandler) HandleTextMessage(message whatsapp.TextMessage) {
authorID := "-"
screenName := "-"
if message.Info.FromMe {
authorID = h.c.Info.Wid
screenName = ""
} else {
if message.Info.Source.Participant != nil {
authorID = *message.Info.Source.Participant
} else {
authorID = message.Info.RemoteJid
}
if message.Info.Source.PushName != nil {
screenName = *message.Info.Source.PushName
}
}
date := time.Unix(int64(message.Info.Timestamp), 0)
h.messages = append(h.messages, fmt.Sprintf("%s %s (%s): %s", date,
authorID, screenName, message.Text))
}
func (h *historyHandler) HandleError(err error) {
log.Printf("Error occured while retrieving chat history: %s", err)
}
func GetHistory(jid string, wac *whatsapp.Conn) []string {
// create out history handler
handler := &historyHandler{c: wac}
// load chat history and pass messages to the history handler to accumulate
wac.LoadFullChatHistory(jid, 300, time.Millisecond*300, handler)
return handler.messages
}
func GetAnyHistory(wac *whatsapp.Conn, chats map[string]struct{}) {
// show list of chats
var chatSlice []string
for chat := range chats {
chatSlice = append(chatSlice, chat)
fmt.Printf("%d %s\n", len(chatSlice), chat)
}
fmt.Println("Select chat number to get history for:")
var index = 0
for index < 1 || index > len(chatSlice) {
fmt.Scanf("%d", &index)
}
// get history for the selected chat
fmt.Println("Gathering chat history...")
messages := GetHistory(chatSlice[index-1], wac)
for _, message := range messages {
fmt.Println(message)
}
}