diff options
author | Wim <wim@42.be> | 2020-01-17 00:46:11 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2020-03-01 22:19:33 +0100 |
commit | 3f06a40bd55b242c3c2540e8262b8b13e3e8afcf (patch) | |
tree | 7b656b5b65a819c3c729c74a5cde883c82d06b57 /bridge/msteams/handler.go | |
parent | 68f43985ade115f1c0de01d1b3f21a2e2c3ef239 (diff) | |
download | matterbridge-msglm-3f06a40bd55b242c3c2540e8262b8b13e3e8afcf.tar.gz matterbridge-msglm-3f06a40bd55b242c3c2540e8262b8b13e3e8afcf.tar.bz2 matterbridge-msglm-3f06a40bd55b242c3c2540e8262b8b13e3e8afcf.zip |
Support code snippets from msteams
Diffstat (limited to 'bridge/msteams/handler.go')
-rw-r--r-- | bridge/msteams/handler.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/bridge/msteams/handler.go b/bridge/msteams/handler.go index 8cccfe0c..c8f0c468 100644 --- a/bridge/msteams/handler.go +++ b/bridge/msteams/handler.go @@ -1,10 +1,14 @@ 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" ) @@ -50,6 +54,13 @@ 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 { @@ -57,3 +68,34 @@ 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" +} |