This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
informations.go
125 lines (94 loc) · 2.88 KB
/
informations.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package wabot
import (
"fmt"
"strings"
"github.com/Rhymen/go-whatsapp"
)
// MessageToJid find the right Jid in a message
func MessageToJid(message whatsapp.TextMessage) string {
authorID := ""
if message.Info.Source.Participant != nil {
authorID = *message.Info.Source.Participant
} else {
authorID = message.Info.RemoteJid // Personennamen
}
return authorID
}
// MessageToName Converts and entire message to the corresponding name
// -> allows finding the name in groups
func MessageToName(message whatsapp.TextMessage) string {
// Try to find the right name
participantNumber := strings.Split(MessageToJid(message), "@")[0]
userNickname := GetUserNickname(MessageToJid(message))
authorName := JidToName(message.Info.RemoteJid)
// fmt.Printf("---------\nNumber: %s\nNickname: %s\nAuthor: %s\n", participantNumber, userNickname, authorName)
// Return if desired
if useNicknames && len(userNickname) > 0 {
return userNickname
} else if !useContactName && message.Info.Source.Participant != nil {
return participantNumber
}
// Return the custom name if there is none in the contacts
if len(authorName) < 2 {
return participantNumber
}
return authorName
}
// MessageToGroupID returns the jid of a group. If the message is from outside a group it returns ""
func MessageToGroupID(message whatsapp.TextMessage) string {
jid := MessageToJid(message)
groupID := strings.Split(jid, "-")[1]
fmt.Println("MessageToName: ", MessageToName(message))
if jid != groupID {
return groupID
}
// Message is from outside a group
return ""
}
// GetPhoneNumber returns a bots phone number
func GetPhoneNumber() string {
// Try to find the right name
return strings.Split(conn.Info.Wid, "@")[0]
}
// JidToGroupName returns a chat's name according to the jid (can also check groups)
func JidToGroupName(jid string) string {
// If only a group's jid was given
if !strings.Contains(jid, "-") {
jid = fmt.Sprintf("%s-%s", GetPhoneNumber(), jid)
}
// Return the right name
return JidToName(jid)
}
// JidToGroupID returns a group's id according to the jid
func JidToGroupID(jid string) string {
// If only a group's jid was given
if !strings.Contains(jid, "-") {
return jid
}
return strings.Split(jid, "-")[1]
}
// ----------------------------------------------------------------------------------------------------- //
// JidToName returns a corresponding contact or group's name.
func JidToName(jid string) string {
for _, c := range contacList {
if c.Jid == jid {
return c.Name
}
}
return "{undefined}"
}
// NameToJid returns a corresponding contact's jid. n general, you want to use MessageToJid() instead
func NameToJid(name string) string {
for _, c := range contacList {
if JidToName(c.Jid) == name {
return c.Jid
}
}
return "{undefined}"
}
// PrintContacList is used for debugging
func PrintContacList() {
for _, c := range contacList {
fmt.Println(c)
}
}