From a68d6746251d316f0fcdde8e2462cc558c0bcabf Mon Sep 17 00:00:00 2001 From: Krzysztof Madejski Date: Tue, 12 Feb 2019 11:54:51 +0100 Subject: [PATCH] #475 usernames from WhatsApp --- bridge/whatsapp/whatsapp.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/bridge/whatsapp/whatsapp.go b/bridge/whatsapp/whatsapp.go index 5cd637d2..88cb308e 100644 --- a/bridge/whatsapp/whatsapp.go +++ b/bridge/whatsapp/whatsapp.go @@ -32,6 +32,8 @@ type Bwhatsapp struct { // connExt *whatsappExt.ExtendedConn // https://github.com/tulir/mautrix-whatsapp/blob/master/whatsapp-ext/whatsapp.go conn *whatsapp.Conn startedAt uint64 + + users map[string]whatsapp.Contact } func New(cfg *bridge.Config) bridge.Bridger { @@ -48,6 +50,8 @@ func New(cfg *bridge.Config) bridge.Bridger { b := &Bwhatsapp{ Config: cfg, + users: make(map[string]whatsapp.Contact), + //uuid: xid.New().String(), //users: map[string]*slack.User{}, //channelsByID: map[string]*slack.Channel{}, @@ -123,6 +127,13 @@ func (b *Bwhatsapp) Connect() error { return nil } + for id, contact := range b.conn.Store.Contacts { + if !isGroupJid(id) && id != "status@broadcast" { + // it is user + b.users[id] = contact + } + } + b.Log.Debugln("Importing all contacts done") return nil @@ -271,9 +282,9 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) { // Id: "", // TODO id // TODO Timestamp RemoteJid: msg.Channel, // which equals to group id - PushName: "pushname", + }, - Text: msg.Text, + Text: msg.Username + msg.Text, } // TODO adapt gitter code @@ -380,10 +391,24 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) { senderJid = *message.Info.Source.Participant } + // translate sender's Jid to the nicest username we can get + senderName := senderJid + if sender, exists := b.users[senderJid]; exists { + if sender.Name != "" { + senderName = sender.Name + + } else { + // if user is not in phone contacts + // it is the most obvious scenario unless you sync your phone contacts with some remote updated source + // users can change it in their WhatsApp settings -> profile -> click on Avatar + senderName = sender.Notify + } + } + b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJid, b.Account) rmsg := config.Message{ UserID: senderJid, - Username: senderJid, // TODO mapping + Username: senderName, Text: message.Text, Timestamp: messageTime, Channel: groupJid,