Add support for using avatars from discord,slack and gitter in slack

This commit is contained in:
Wim 2016-11-06 00:46:32 +01:00
parent 37873acfcd
commit a3dd0f1345
5 changed files with 54 additions and 3 deletions

View File

@ -15,6 +15,7 @@ type Message struct {
Origin string
FullOrigin string
Protocol string
Avatar string
}
type Protocol struct {

View File

@ -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 {

View File

@ -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
}

View File

@ -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:

View File

@ -125,6 +125,24 @@ func (gitter *Gitter) GetRooms() ([]Room, error) {
return rooms, nil
}
// GetUsersInRoom returns the users in the room with the passed id
func (gitter *Gitter) GetUsersInRoom(roomID string) ([]User, error) {
var users []User
response, err := gitter.get(gitter.config.apiBaseURL + "rooms/" + roomID + "/users")
if err != nil {
gitter.log(err)
return nil, err
}
err = json.Unmarshal(response, &users)
if err != nil {
gitter.log(err)
return nil, err
}
return users, nil
}
// GetRoom returns a room with the passed id
func (gitter *Gitter) GetRoom(roomID string) (*Room, error) {