Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit c516254cacc9726d87a6175145699f2cf16bf4a1 Author: Wim <wim@42.be> Date: Fri Jan 17 00:46:11 2020 +0000 Support code snippets from msteams diff --git a/bridge/msteams/handler.go b/bridge/msteams/handler.go index 8cccfe0..c8f0c46 100644 --- a/bridge/msteams/handler.go +++ b/bridge/msteams/handler.go @@ -110 +114 @@  package bmsteams    import ( + "encoding/json"   "fmt" + "io/ioutil" + "strings"     "github.com/42wim/matterbridge/bridge/config"   "github.com/42wim/matterbridge/bridge/helper" +   msgraph "github.com/yaegashi/msgraph.go/beta"  )   @@ -506 +5413 @@ func (b *Bmsteams) handleAttachments(rmsg *config.Message, msg msgraph.ChatMessa   for _, a := range msg.Attachments {   //remove the attachment tags from the text   rmsg.Text = attachRE.ReplaceAllString(rmsg.Text, "") + + //handle a code snippet (code block) + if *a.ContentType == "application/vnd.microsoft.card.codesnippet" { + b.handleCodeSnippet(rmsg, a) + continue + } +   //handle the download   err := b.handleDownloadFile(rmsg, *a.Name, *a.ContentURL)   if err != nil { @@ -573 +6834 @@ func (b *Bmsteams) handleAttachments(rmsg *config.Message, msg msgraph.ChatMessa   }   }  } + +type AttachContent struct { + Language string `json:"language"` + CodeSnippetURL string `json:"codeSnippetUrl"` +} + +func (b *Bmsteams) handleCodeSnippet(rmsg *config.Message, attach msgraph.ChatMessageAttachment) { + var content AttachContent + err := json.Unmarshal([]byte(*attach.Content), &content) + if err != nil { + b.Log.Errorf("unmarshal codesnippet failed: %s", err) + return + } + s := strings.Split(content.CodeSnippetURL, "/") + if len(s) != 13 { + b.Log.Errorf("codesnippetUrl has unexpected size: %s", content.CodeSnippetURL) + return + } + resp, err := b.gc.Teams().Request().Client().Get(content.CodeSnippetURL) + if err != nil { + b.Log.Errorf("retrieving snippet content failed:%s", err) + return + } + defer resp.Body.Close() + res, err := ioutil.ReadAll(resp.Body) + if err != nil { + b.Log.Errorf("reading snippet data failed: %s", err) + return + } + rmsg.Text = rmsg.Text + "\n```" + content.Language + "\n" + string(res) + "\n```\n" +} diff --git a/bridge/msteams/msteams.go b/bridge/msteams/msteams.go index da0e31f..3633924 100644 --- a/bridge/msteams/msteams.go +++ b/bridge/msteams/msteams.go @@ -1206 +1207 @@ func (b *Bmsteams) getMessages(channel string) ([]msgraph.ChatMessage, error) {   return rct, nil  }   +//nolint:gocognit  func (b *Bmsteams) poll(channelName string) {   msgmap := make(map[string]time.Time)   b.Log.Debug("getting initial messages")