mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-12-24 18:25:40 +00:00
Support code snippets from msteams
This commit is contained in:
parent
68f43985ad
commit
3f06a40bd5
@ -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"
|
||||
}
|
||||
|
@ -120,6 +120,7 @@ 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")
|
||||
|
Loading…
Reference in New Issue
Block a user