diff options
author | Tatsuyuki Ishi <ishitatsuyuki@gmail.com> | 2017-01-07 07:32:17 +0900 |
---|---|---|
committer | Wim <wim@42.be> | 2017-01-06 23:32:17 +0100 |
commit | e3dda0e812606e8d051567c401c139dc392706e1 (patch) | |
tree | f9bcc595065d245ad49fcd5a063efe4557b060c8 /bridge | |
parent | 38103d36b4e119669c4892c7fe78836bee14aa43 (diff) | |
download | matterbridge-msglm-e3dda0e812606e8d051567c401c139dc392706e1.tar.gz matterbridge-msglm-e3dda0e812606e8d051567c401c139dc392706e1.tar.bz2 matterbridge-msglm-e3dda0e812606e8d051567c401c139dc392706e1.zip |
Telegram: add markdown (#103)
* Add support for markdown (telegram)
Close #98
* Telegram: add more Markdown Render blacklist
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/telegram/telegram.go | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index db3ab323..b179dd6d 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -1,10 +1,14 @@ 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" - "strconv" + "github.com/russross/blackfriday" ) type Btelegram struct { @@ -51,13 +55,70 @@ 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) if err != nil { return err } - m := tgbotapi.NewMessage(chatid, msg.Username + msg.Text) + + 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, msg.Username+string(parsed)) + m.ParseMode = "HTML" _, err = b.c.Send(m) return err } @@ -69,6 +130,5 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { } flog.Debugf("Sending message from %s on %s to gateway", update.Message.From.UserName, b.Account) b.Remote <- config.Message{Username: update.Message.From.UserName, Text: update.Message.Text, Channel: strconv.FormatInt(update.Message.Chat.ID, 10), Account: b.Account} - } } |