add ability to send images

This commit is contained in:
Aaron Bieber 2020-03-09 20:47:11 -06:00
parent a53fde7d09
commit e09d5853ac

View File

@ -3,6 +3,9 @@ package plugins
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"image"
"image/png"
"io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -148,6 +151,28 @@ func SendMD(c *gomatrix.Client, roomID, message string) error {
return nil return nil
} }
// SendImage takes an image and sends it!.
func SendImage(c *gomatrix.Client, roomID string, img *image.RGBA) error {
r, w := io.Pipe()
go func() {
defer w.Close()
png.Encode(w, img)
}()
mediaURL, err := c.UploadToContentRepo(r, "image/png", 0)
if err != nil {
return err
}
_, err = c.SendImage(roomID, "", mediaURL.ContentURI)
if err != nil {
return err
}
return nil
}
// Plugins is a collection of our plugins. An instance of this is iterated // Plugins is a collection of our plugins. An instance of this is iterated
// over for each message the bot receives. // over for each message the bot receives.
type Plugins []Plugin type Plugins []Plugin