add the ability to send html and markdown-to-html messages

This commit is contained in:
Aaron Bieber 2020-02-26 16:15:02 -07:00
parent 8c8deeda97
commit d6184a1957
3 changed files with 30 additions and 0 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module suah.dev/mcchunkie
go 1.13
require (
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497
github.com/google/btree v1.0.0 // indirect
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d
github.com/peterbourgon/diskv v2.0.1+incompatible

3
go.sum
View File

@ -1,9 +1,12 @@
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497 h1:wJkj+x9gPYlDyM34C6r3SXPs270coWeh85wu1CsusDo=
github.com/gomarkdown/markdown v0.0.0-20200127000047-1813ea067497/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d h1:Vf/EQgAfg8/CBUQv9te7UJreZ9iKKouB2gb8UIRM4jQ=
github.com/matrix-org/gomatrix v0.0.0-20200128155335-9e7906b6766d/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d h1:9FCpayM9Egr1baVnV1SX0H87m+XB0B8S0hAMi99X/3U=
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

View File

@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/gomarkdown/markdown"
"github.com/matrix-org/gomatrix"
)
@ -122,6 +123,31 @@ func SendText(c *gomatrix.Client, roomID, message string) error {
return nil
}
// SendHTML sends an html message to a given room. It pretends to be
// "typing" by calling UserTyping for the caller.
func SendHTML(c *gomatrix.Client, roomID, message string) error {
_, err := c.UserTyping(roomID, true, 3)
if err != nil {
return err
}
c.SendMessageEvent(roomID, "m.room.message", gomatrix.GetHTMLMessage("m.text", message))
_, err = c.UserTyping(roomID, false, 0)
if err != nil {
return err
}
return nil
}
// SendMD takes markdown and converts it to an html message.
func SendMD(c *gomatrix.Client, roomID, message string) error {
md := []byte(message)
html := markdown.ToHTML(md, nil, nil)
SendHTML(c, roomID, string(html))
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