Skip to content

Commit

Permalink
Support code snippets from msteams
Browse files Browse the repository at this point in the history
  • Loading branch information
42wim committed Mar 1, 2020
1 parent efca4be commit d1cf6dd
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions bridge/msteams/handler.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -50,10 +54,48 @@ 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 {
b.Log.Errorf("download of %s failed: %s", *a.Name, err)
}
}
}

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

0 comments on commit d1cf6dd

Please sign in to comment.