forked from gpavlidi/go-hangups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (76 loc) · 2.5 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
"time"
"github.com/gpavlidi/go-hangups"
)
func OrDie(e error) {
if e != nil {
log.Fatal(e)
}
}
func main() {
s := &hangups.Session{}
data, err := ioutil.ReadFile("./refreshtoken")
if err == nil {
s.RefreshToken = string(data)
}
OrDie(s.Init())
ioutil.WriteFile("./refreshtoken", []byte(s.RefreshToken), 0644)
c := &hangups.Client{Session: s}
// find whoami and seed the sync timestamp to current time
getSelfInfo, _ := c.GetSelfInfo()
serverNowUsecs := *getSelfInfo.ResponseHeader.CurrentServerTime
ticker := time.NewTicker(time.Second * 5)
for _ = range ticker.C {
newEvents, _ := c.SyncAllNewEvents(serverNowUsecs, 1048576) //1 MB
serverNowUsecs = *newEvents.ResponseHeader.CurrentServerTime
for _, conversation := range newEvents.ConversationState {
//find or generate conversation name
conversationName := ""
if conversation.Conversation.Name != nil {
conversationName = fmt.Sprintf("hangouts-%s-%s", *conversation.Conversation.Name, *conversation.Conversation.ConversationId.Id)
} else {
participants := make([]string, 0)
for _, participant := range conversation.Conversation.ParticipantData {
// skip my name from participants list
if *participant.Id.GaiaId == *getSelfInfo.SelfEntity.Id.GaiaId {
continue
}
participants = append(participants, *participant.FallbackName)
}
conversationName = fmt.Sprintf("hangouts-%s", strings.Join(participants, ","))
}
for _, event := range conversation.Event {
senderId := *event.SenderId.GaiaId
// dont echo my msgs
if senderId == *getSelfInfo.SelfEntity.Id.GaiaId {
continue
}
// find sender name
senderName := "Unknown"
for _, participant := range conversation.Conversation.ParticipantData {
if *participant.Id.GaiaId == senderId {
senderName = *participant.FallbackName
break
}
}
// reconstruct msg text
for _, segment := range event.ChatMessage.MessageContent.Segment {
// if *segment.Type != 1 {
//skip SegmentType_SEGMENT_TYPE_LINE_BREAK
fmt.Println("[", conversationName, "] ", senderName, ":", *segment.Text, " (", *segment.Type, ")")
// }
}
for _, attachment := range event.ChatMessage.MessageContent.Attachment {
fmt.Println("[", conversationName, "] ", senderName, ":", attachment.String())
}
}
// mark all events in this conversation as read
_, _ = c.UpdateWatermark(*conversation.ConversationId.Id, serverNowUsecs)
}
}
}