From e09d5853ac4509f487a8f46bdc9d1b5fac321914 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Mon, 9 Mar 2020 20:47:11 -0600 Subject: [PATCH] add ability to send images --- plugins/plugins.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/plugins/plugins.go b/plugins/plugins.go index abddf04..f9bac62 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -3,6 +3,9 @@ package plugins import ( "bytes" "encoding/json" + "image" + "image/png" + "io" "net/http" "regexp" "strings" @@ -148,6 +151,28 @@ func SendMD(c *gomatrix.Client, roomID, message string) error { 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 // over for each message the bot receives. type Plugins []Plugin