Add JSONMessage parsing to whatsapp extensions

This commit is contained in:
Tulir Asokan
2018-08-24 00:51:40 +03:00
parent 329bc9d8ce
commit d13bf4ae64
7 changed files with 414 additions and 3 deletions

View File

@ -26,14 +26,23 @@ import (
"strings"
)
const (
OldUserSuffix = "@c.us"
NewUserSuffix = "@s.whatsapp.net"
)
type ExtendedConn struct {
*whatsapp.Conn
handlers []whatsapp.Handler
}
func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
return &ExtendedConn{
ext := &ExtendedConn{
Conn: conn,
}
ext.Conn.AddHandler(ext)
return ext
}
type GroupInfo struct {
@ -64,7 +73,7 @@ func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) {
return nil, fmt.Errorf("failed to get group metadata: %v", err)
}
content := <-data
fmt.Println("GROUP METADATA", content)
info := &GroupInfo{}
err = json.Unmarshal([]byte(content), info)
if err != nil {
@ -72,7 +81,7 @@ func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) {
}
for index, participant := range info.Participants {
info.Participants[index].JID = strings.Replace(participant.JID, "@c.us", "@s.whatsapp.net", 1)
info.Participants[index].JID = strings.Replace(participant.JID, OldUserSuffix, NewUserSuffix, 1)
}
return info, nil