From a3dd0f1345fae3d7828ed0c82d6a7c4b610d0983 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 6 Nov 2016 00:46:32 +0100 Subject: Add support for using avatars from discord,slack and gitter in slack --- bridge/config/config.go | 1 + bridge/discord/discord.go | 2 +- bridge/gitter/gitter.go | 17 ++++++++++++++++- bridge/slack/slack.go | 19 ++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) (limited to 'bridge') diff --git a/bridge/config/config.go b/bridge/config/config.go index 32c8c74f..1575bff4 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -15,6 +15,7 @@ type Message struct { Origin string FullOrigin string Protocol string + Avatar string } type Protocol struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 7995a06d..62bc85ce 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -127,7 +127,7 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat channelName = "ID:" + m.ChannelID } b.Remote <- config.Message{Username: m.Author.Username, Text: m.ContentWithMentionsReplaced(), Channel: channelName, - Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg"} } func (b *bdiscord) getChannelID(name string) string { diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index 5daf7773..176b6cef 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -13,6 +13,7 @@ type Bgitter struct { Remote chan config.Message protocol string origin string + Users []gitter.User Rooms []gitter.Room } @@ -64,6 +65,8 @@ func (b *Bgitter) JoinChannel(channel string) error { if err != nil { return err } + users, _ := b.c.GetUsersInRoom(roomID) + b.Users = append(b.Users, users...) stream := b.c.Stream(roomID) go b.c.Listen(stream) @@ -76,7 +79,7 @@ func (b *Bgitter) JoinChannel(channel string) error { if !strings.HasSuffix(ev.Message.Text, "​") { flog.Debugf("Sending message from %s on %s to gateway", ev.Message.From.Username, b.FullOrigin()) b.Remote <- config.Message{Username: ev.Message.From.Username, Text: ev.Message.Text, Channel: room, - Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: b.getAvatar(ev.Message.From.Username)} } case *gitter.GitterConnectionClosed: flog.Errorf("connection with gitter closed for room %s", room) @@ -118,3 +121,15 @@ func (b *Bgitter) getRoomID(channel string) string { } return "" } + +func (b *Bgitter) getAvatar(user string) string { + var avatar string + if b.Users != nil { + for _, u := range b.Users { + if user == u.Username { + return u.AvatarURLSmall + } + } + } + return avatar +} diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 412c925b..f700aee8 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -24,6 +24,7 @@ type Bslack struct { rtm *slack.RTM Plus bool Remote chan config.Message + Users []slack.User protocol string origin string si *slack.Info @@ -126,6 +127,9 @@ func (b *Bslack) Send(msg config.Message) error { } np.Username = nick np.IconURL = config.GetIconURL(&msg, b.Config) + if msg.Avatar != "" { + np.IconURL = msg.Avatar + } b.sc.PostMessage(schannel.ID, message, np) /* @@ -136,6 +140,18 @@ func (b *Bslack) Send(msg config.Message) error { return nil } +func (b *Bslack) getAvatar(user string) string { + var avatar string + if b.Users != nil { + for _, u := range b.Users { + if user == u.Name { + return u.Profile.Image48 + } + } + } + return avatar +} + func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) { if b.channels == nil { return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.FullOrigin(), name) @@ -166,7 +182,7 @@ func (b *Bslack) handleSlack() { texts := strings.Split(message.Text, "\n") for _, text := range texts { flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.FullOrigin()) - b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} + b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin(), Avatar: b.getAvatar(message.Username)} } } } @@ -201,6 +217,7 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) { case *slack.ConnectedEvent: b.channels = ev.Info.Channels b.si = ev.Info + b.Users, _ = b.sc.GetUsers() case *slack.InvalidAuthEvent: flog.Fatalf("Invalid Token %#v", ev) default: -- cgit v1.2.3