summaryrefslogtreecommitdiffstats
path: root/bridge/telegram/telegram.go
blob: aa6374578ab0224d50a45c89847585d2028b7503 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 {
	c       *tgbotapi.BotAPI
	Config  *config.Protocol
	Remote  chan config.Message
	Account string
}

var flog *log.Entry
var protocol = "telegram"

func init() {
	flog = log.WithFields(log.Fields{"module": protocol})
}

func New(cfg config.Protocol, account string, c chan config.Message) *Btelegram {
	b := &Btelegram{}
	b.Config = &cfg
	b.Remote = c
	b.Account = account
	return b
}

func (b *Btelegram) Connect() error {
	var err error
	flog.Info("Connecting")
	b.c, err = tgbotapi.NewBotAPI(b.Config.Token)
	if err != nil {
		flog.Debugf("%#v", err)
		return err
	}
	updates, err := b.c.GetUpdatesChan(tgbotapi.NewUpdate(0))
	if err != nil {
		flog.Debugf("%#v", err)
		return err
	}
	flog.Info("Connection succeeded")
	go b.handleRecv(updates)
	return nil
}

func (b *Btelegram) Disconnect() error {
	return nil

}

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
	}

	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"
	_, err = b.c.Send(m)
	return err
}

func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
	username := ""
	text := ""
	channel := ""
	for update := range updates {
		// handle channels
		if update.ChannelPost != nil {
			if update.ChannelPost.From != nil {
				username = update.ChannelPost.From.FirstName
				if username == "" {
					username = update.ChannelPost.From.UserName
				}
			}
			text = update.ChannelPost.Text
			channel = strconv.FormatInt(update.ChannelPost.Chat.ID, 10)
		}
		// handle groups
		if update.Message != nil {
			if update.Message.From != nil {
				username = update.Message.From.FirstName
				if username == "" {
					username = update.Message.From.UserName
				}
			}
			text = update.Message.Text
			channel = strconv.FormatInt(update.Message.Chat.ID, 10)
		}
		if username == "" {
			username = "unknown"
		}
		if text != "" {
			flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
			b.Remote <- config.Message{Username: username, Text: text, Channel: channel, Account: b.Account}
		}
	}
}