Do not use HTML parsemode by default. Set MessageFormat="HTML" to use it. (telegram) Closes #126

This commit is contained in:
Wim 2017-02-24 18:49:52 +01:00
parent 84c0b745af
commit 32bbab8518
5 changed files with 86 additions and 63 deletions

View File

@ -31,7 +31,7 @@ type Protocol struct {
IconURL string // mattermost, slack IconURL string // mattermost, slack
IgnoreNicks string // all protocols IgnoreNicks string // all protocols
Jid string // xmpp Jid string // xmpp
Login string // mattermost Login string // mattermost, matrix
Muc string // xmpp Muc string // xmpp
Name string // all protocols Name string // all protocols
Nick string // all protocols Nick string // all protocols
@ -40,18 +40,19 @@ type Protocol struct {
NickServPassword string // IRC NickServPassword string // IRC
NicksPerRow int // mattermost, slack NicksPerRow int // mattermost, slack
NoTLS bool // mattermost NoTLS bool // mattermost
Password string // IRC,mattermost,XMPP Password string // IRC,mattermost,XMPP,matrix
PrefixMessagesWithNick bool // mattemost, slack PrefixMessagesWithNick bool // mattemost, slack
Protocol string //all protocols Protocol string //all protocols
MessageQueue int // IRC, size of message queue for flood control MessageQueue int // IRC, size of message queue for flood control
MessageDelay int // IRC, time in millisecond to wait between messages MessageDelay int // IRC, time in millisecond to wait between messages
MessageFormat string // telegram
RemoteNickFormat string // all protocols RemoteNickFormat string // all protocols
Server string // IRC,mattermost,XMPP,discord Server string // IRC,mattermost,XMPP,discord
ShowJoinPart bool // all protocols ShowJoinPart bool // all protocols
SkipTLSVerify bool // IRC, mattermost SkipTLSVerify bool // IRC, mattermost
Team string // mattermost Team string // mattermost
Token string // gitter, slack, discord Token string // gitter, slack, discord
URL string // mattermost, slack URL string // mattermost, slack, matrix
UseAPI bool // mattermost, slack UseAPI bool // mattermost, slack
UseSASL bool // IRC UseSASL bool // IRC
UseTLS bool // IRC UseTLS bool // IRC

64
bridge/telegram/html.go Normal file
View File

@ -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))
}

View File

@ -1,14 +1,11 @@
package btelegram package btelegram
import ( import (
"bytes"
"html"
"strconv" "strconv"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/russross/blackfriday"
) )
type Btelegram struct { type Btelegram struct {
@ -60,51 +57,6 @@ func (b *Btelegram) JoinChannel(channel string) error {
return nil 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 { func (b *Btelegram) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
chatid, err := strconv.ParseInt(msg.Channel, 10, 64) chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
@ -112,18 +64,13 @@ func (b *Btelegram) Send(msg config.Message) error {
return err return err
} }
parsed := blackfriday.Markdown([]byte(msg.Text), if b.Config.MessageFormat == "HTML" {
&customHtml{blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML|blackfriday.HTML_SKIP_IMAGES, "", "")}, msg.Text = makeHTML(msg.Text)
blackfriday.EXTENSION_NO_INTRA_EMPHASIS| }
blackfriday.EXTENSION_FENCED_CODE| m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text)
blackfriday.EXTENSION_AUTOLINK| if b.Config.MessageFormat == "HTML" {
blackfriday.EXTENSION_SPACE_HEADERS| m.ParseMode = tgbotapi.ModeHTML
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"
_, err = b.c.Send(m) _, err = b.c.Send(m)
return err return err
} }

View File

@ -2,6 +2,12 @@
## New features ## New features
* matrix: New protocol support added (https://matrix.org) * matrix: New protocol support added (https://matrix.org)
## Bugfix
* mattermost: Add ReadTimeout to close lingering connections (mattermost). See #125
## Changes
* telegram: Do not use HTML parsemode by default. Set ```MessageFormat="HTML"``` to use it. Closes #126
# v0.9.3 # v0.9.3
## New features ## New features
* API: rest interface to read / post messages (see API section in matterbridge.toml.sample) * API: rest interface to read / post messages (see API section in matterbridge.toml.sample)

View File

@ -413,6 +413,11 @@ ShowJoinPart=false
#REQUIRED #REQUIRED
Token="Yourtokenhere" Token="Yourtokenhere"
#OPTIONAL (default empty)
#Only supported format is "HTML", messages will be sent in html parsemode.
#See https://core.telegram.org/bots/api#html-style
MessageFormat=""
#Nicks you want to ignore. #Nicks you want to ignore.
#Messages from those users will not be sent to other bridges. #Messages from those users will not be sent to other bridges.
#OPTIONAL #OPTIONAL