Show untranslated text in attachment when using Slack.

This commit is contained in:
Patrick Connolly
2018-10-20 18:09:59 +08:00
parent 1ba7385175
commit 21c8dcb1ba
3 changed files with 49 additions and 13 deletions

View File

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

View File

@@ -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(&params)
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 {

View File

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