From 21c8dcb1bab5eaa819601931b144d9a5b34b6d3d Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Sat, 20 Oct 2018 18:09:59 +0800 Subject: [PATCH] Show untranslated text in attachment when using Slack. --- bridge/config/config.go | 25 +++++++++++++------------ bridge/slack/slack.go | 29 +++++++++++++++++++++++++++++ gateway/gateway.go | 8 +++++++- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index 7d2808be..944a1833 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -26,18 +26,19 @@ const ( ) type Message struct { - Text string `json:"text"` - Channel string `json:"channel"` - Username string `json:"username"` - UserID string `json:"userid"` // userid on the bridge - Avatar string `json:"avatar"` - Account string `json:"account"` - Event string `json:"event"` - Protocol string `json:"protocol"` - Gateway string `json:"gateway"` - Timestamp time.Time `json:"timestamp"` - ID string `json:"id"` - Extra map[string][]interface{} + Text string `json:"text"` + Channel string `json:"channel"` + Username string `json:"username"` + UserID string `json:"userid"` // userid on the bridge + Avatar string `json:"avatar"` + Account string `json:"account"` + Event string `json:"event"` + Protocol string `json:"protocol"` + Gateway string `json:"gateway"` + Timestamp time.Time `json:"timestamp"` + ID string `json:"id"` + Extra map[string][]interface{} + TranslationSrcMsg *Message } type FileInfo struct { diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index c8254225..0c1e672d 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -230,6 +230,11 @@ func (b *Bslack) Send(msg config.Message) (string, error) { np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid}) // add file attachments np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...) + // add translation attachment + if msg.TranslationSrcMsg != nil { + // If source, then we're doing a translation + np.Attachments = append(np.Attachments, b.createTranslationAttach(msg)) + } // add slack attachments (from another slack bridge) if msg.Extra != nil { for _, attach := range msg.Extra[sSlackAttachment] { @@ -285,6 +290,30 @@ func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment return attachements } +func (b *Bslack) createTranslationAttach(msg config.Message) slack.Attachment { + untranslatedTextPreview := msg.TranslationSrcMsg.Text[:100]+"..." + ch, err := b.getChannelByName(msg.TranslationSrcMsg.Channel) + time := strings.Split(msg.TranslationSrcMsg.ID, " ")[1] + params := slack.PermalinkParameters{ + Channel: ch.ID, + Ts: time, + } + b.Log.Debugf("Generating permalink...") + permalink, err := b.sc.GetPermalink(¶ms) + if err != nil { + b.Log.Println(err) + } + + attach := slack.Attachment{ + Fallback: trimmed, + Text: fmt.Sprintf("<%s|%s>", permalink, untranslatedTextPreview), + Footer: "g0v Translation Bridge"+b.Config.General.TranslationAttribution, + FooterIcon: "https://emoji.slack-edge.com/T02G2SXKM/g0v/541e38dfc833f04b.png", + } + + return attach +} + func extractStringField(data map[string]interface{}, field string) string { if rawValue, found := data[field]; found { if value, ok := rawValue.(string); ok { diff --git a/gateway/gateway.go b/gateway/gateway.go index 0fca9db5..fe0bd714 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -334,7 +334,9 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM msg.ID = "" msg.Text = origmsg.Text + // Translation if (gw.Router.GTClient != nil) && (channel.Options.Locale != "") && (msg.Text != "") { + msg.TranslationSrcMsg = &origmsg ctx := context.Background() @@ -489,7 +491,11 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM text = html.UnescapeString(text) flog.Debugf("post-unescaped:"+text) - text = text + gw.Router.General.TranslationAttribution + if dest.Protocol == "slack" { + // Attribution will be in attachment for Slack + } else { + text = text + gw.Router.General.TranslationAttribution + } msg.Text = text }