mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-14 03:50:26 +00:00
Support receiving attachments from msteams
This commit is contained in:
parent
795a8705c3
commit
8eb6ed5639
46
bridge/msteams/handler.go
Normal file
46
bridge/msteams/handler.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package bmsteams
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
|
"github.com/42wim/matterbridge/bridge/helper"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Bmsteams) findFile(weburl string) (string, error) {
|
||||||
|
itemRB, err := b.gc.GetDriveItemByURL(b.ctx, weburl)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
itemRB.Workbook().Worksheets()
|
||||||
|
b.gc.Workbooks()
|
||||||
|
item, err := itemRB.Request().Get(b.ctx)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if url, ok := item.GetAdditionalData("@microsoft.graph.downloadUrl"); ok {
|
||||||
|
return url.(string), nil
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleDownloadFile handles file download
|
||||||
|
func (b *Bmsteams) handleDownloadFile(rmsg *config.Message, filename, weburl string) error {
|
||||||
|
realURL, err := b.findFile(weburl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Actually download the file.
|
||||||
|
data, err := helper.DownloadFile(realURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("download %s failed %#v", weburl, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a comment is attached to the file(s) it is in the 'Text' field of the teams messge event
|
||||||
|
// and should be added as comment to only one of the files. We reset the 'Text' field to ensure
|
||||||
|
// that the comment is not duplicated.
|
||||||
|
comment := rmsg.Text
|
||||||
|
rmsg.Text = ""
|
||||||
|
helper.HandleDownloadData(b.Log, rmsg, filename, comment, weburl, data, b.General)
|
||||||
|
return nil
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
package bgitter
|
package bmsteams
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -96,6 +97,7 @@ func (b *Bmsteams) getMessages(channel string) ([]msgraph.ChatMessage, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmsteams) poll(channelName string) {
|
func (b *Bmsteams) poll(channelName string) {
|
||||||
|
re := regexp.MustCompile(`<attachment id=.*?attachment>`)
|
||||||
msgmap := make(map[string]time.Time)
|
msgmap := make(map[string]time.Time)
|
||||||
b.Log.Debug("getting initial messages")
|
b.Log.Debug("getting initial messages")
|
||||||
res, err := b.getMessages(channelName)
|
res, err := b.getMessages(channelName)
|
||||||
@ -136,8 +138,28 @@ func (b *Bmsteams) poll(channelName string) {
|
|||||||
}
|
}
|
||||||
b.Log.Debugf("<= Sending message from %s on %s to gateway", *msg.From.User.DisplayName, b.Account)
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", *msg.From.User.DisplayName, b.Account)
|
||||||
text := b.convertToMD(*msg.Body.Content)
|
text := b.convertToMD(*msg.Body.Content)
|
||||||
rmsg := config.Message{Username: *msg.From.User.DisplayName, Text: text, Channel: channelName,
|
rmsg := config.Message{
|
||||||
Account: b.Account, Avatar: "", UserID: *msg.From.User.ID, ID: *msg.ID}
|
Username: *msg.From.User.DisplayName,
|
||||||
|
Text: text,
|
||||||
|
Channel: channelName,
|
||||||
|
Account: b.Account,
|
||||||
|
Avatar: "",
|
||||||
|
UserID: *msg.From.User.ID,
|
||||||
|
ID: *msg.ID,
|
||||||
|
Extra: make(map[string][]interface{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(msg.Attachments) > 0 {
|
||||||
|
for _, a := range msg.Attachments {
|
||||||
|
//remove the attachment tags from the text
|
||||||
|
rmsg.Text = re.ReplaceAllString(rmsg.Text, "")
|
||||||
|
//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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
b.Log.Debugf("<= Message is %#v", rmsg)
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
||||||
b.Remote <- rmsg
|
b.Remote <- rmsg
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user