summaryrefslogtreecommitdiffstats
path: root/bridge/telegram
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-02-24 18:49:52 +0100
committerWim <wim@42.be>2017-02-24 18:50:16 +0100
commit32bbab8518a03ad04953986f21eeb4832e7331e5 (patch)
tree0d97e9d1a51ff7837f4f61712b1618adc053a7cc /bridge/telegram
parent84c0b745af8349f6dff185c21751f51b1f7d3101 (diff)
downloadmatterbridge-msglm-32bbab8518a03ad04953986f21eeb4832e7331e5.tar.gz
matterbridge-msglm-32bbab8518a03ad04953986f21eeb4832e7331e5.tar.bz2
matterbridge-msglm-32bbab8518a03ad04953986f21eeb4832e7331e5.zip
Do not use HTML parsemode by default. Set MessageFormat="HTML" to use it. (telegram) Closes #126
Diffstat (limited to 'bridge/telegram')
-rw-r--r--bridge/telegram/html.go64
-rw-r--r--bridge/telegram/telegram.go67
2 files changed, 71 insertions, 60 deletions
diff --git a/bridge/telegram/html.go b/bridge/telegram/html.go
new file mode 100644
index 00000000..3eb84bab
--- /dev/null
+++ b/bridge/telegram/html.go
@@ -0,0 +1,64 @@
+package btelegram
+
+import (
+ "bytes"
+ "github.com/russross/blackfriday"
+ "html"
+)
+
+type customHtml struct {
+ blackfriday.Renderer
+}
+
+func (options *customHtml) Paragraph(out *bytes.Buffer, text func() bool) {
+ marker := out.Len()
+
+ if !text() {
+ out.Truncate(marker)
+ return
+ }
+ out.WriteString("\n")
+}
+
+func (options *customHtml) BlockCode(out *bytes.Buffer, text []byte, lang string) {
+ out.WriteString("<pre>")
+
+ out.WriteString(html.EscapeString(string(text)))
+ out.WriteString("</pre>\n")
+}
+
+func (options *customHtml) Header(out *bytes.Buffer, text func() bool, level int, id string) {
+ options.Paragraph(out, text)
+}
+
+func (options *customHtml) HRule(out *bytes.Buffer) {
+ out.WriteByte('\n')
+}
+
+func (options *customHtml) BlockQuote(out *bytes.Buffer, text []byte) {
+ out.WriteString("> ")
+ out.Write(text)
+ out.WriteByte('\n')
+}
+
+func (options *customHtml) List(out *bytes.Buffer, text func() bool, flags int) {
+ options.Paragraph(out, text)
+}
+
+func (options *customHtml) ListItem(out *bytes.Buffer, text []byte, flags int) {
+ out.WriteString("- ")
+ out.Write(text)
+ out.WriteByte('\n')
+}
+
+func makeHTML(input string) string {
+ return string(blackfriday.Markdown([]byte(input),
+ &customHtml{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")},
+ blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
+ blackfriday.EXTENSION_FENCED_CODE|
+ blackfriday.EXTENSION_AUTOLINK|
+ blackfriday.EXTENSION_SPACE_HEADERS|
+ blackfriday.EXTENSION_HEADER_IDS|
+ blackfriday.EXTENSION_BACKSLASH_LINE_BREAK|
+ blackfriday.EXTENSION_DEFINITION_LISTS))
+}
diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go
index aa637457..85486f76 100644
--- a/bridge/telegram/telegram.go
+++ b/bridge/telegram/telegram.go
@@ -1,14 +1,11 @@
package btelegram
import (
- "bytes"
- "html"
"strconv"
"github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus"
"github.com/go-telegram-bot-api/telegram-bot-api"
- "github.com/russross/blackfriday"
)
type Btelegram struct {
@@ -60,51 +57,6 @@ func (b *Btelegram) JoinChannel(channel string) error {
return nil
}
-type customHtml struct {
- blackfriday.Renderer
-}
-
-func (options *customHtml) Paragraph(out *bytes.Buffer, text func() bool) {
- marker := out.Len()
-
- if !text() {
- out.Truncate(marker)
- return
- }
- out.WriteString("\n")
-}
-
-func (options *customHtml) BlockCode(out *bytes.Buffer, text []byte, lang string) {
- out.WriteString("<pre>")
-
- out.WriteString(html.EscapeString(string(text)))
- out.WriteString("</pre>\n")
-}
-
-func (options *customHtml) Header(out *bytes.Buffer, text func() bool, level int, id string) {
- options.Paragraph(out, text)
-}
-
-func (options *customHtml) HRule(out *bytes.Buffer) {
- out.WriteByte('\n')
-}
-
-func (options *customHtml) BlockQuote(out *bytes.Buffer, text []byte) {
- out.WriteString("> ")
- out.Write(text)
- out.WriteByte('\n')
-}
-
-func (options *customHtml) List(out *bytes.Buffer, text func() bool, flags int) {
- options.Paragraph(out, text)
-}
-
-func (options *customHtml) ListItem(out *bytes.Buffer, text []byte, flags int) {
- out.WriteString("- ")
- out.Write(text)
- out.WriteByte('\n')
-}
-
func (b *Btelegram) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg)
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
@@ -112,18 +64,13 @@ func (b *Btelegram) Send(msg config.Message) error {
return err
}
- parsed := blackfriday.Markdown([]byte(msg.Text),
- &customHtml{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")},
- blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
- blackfriday.EXTENSION_FENCED_CODE|
- blackfriday.EXTENSION_AUTOLINK|
- blackfriday.EXTENSION_SPACE_HEADERS|
- blackfriday.EXTENSION_HEADER_IDS|
- blackfriday.EXTENSION_BACKSLASH_LINE_BREAK|
- blackfriday.EXTENSION_DEFINITION_LISTS)
-
- m := tgbotapi.NewMessage(chatid, html.EscapeString(msg.Username)+string(parsed))
- m.ParseMode = "HTML"
+ if b.Config.MessageFormat == "HTML" {
+ msg.Text = makeHTML(msg.Text)
+ }
+ m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text)
+ if b.Config.MessageFormat == "HTML" {
+ m.ParseMode = tgbotapi.ModeHTML
+ }
_, err = b.c.Send(m)
return err
}