2016-11-15 23:15:57 +01:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2017-11-04 14:50:01 +01:00
|
|
|
"regexp"
|
2017-01-06 23:32:17 +01:00
|
|
|
"strconv"
|
2017-11-12 22:04:35 +01:00
|
|
|
"strings"
|
2017-01-06 23:32:17 +01:00
|
|
|
|
2016-11-15 23:15:57 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-04 14:50:01 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-11-15 23:15:57 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Btelegram struct {
|
2017-12-19 23:15:03 +01:00
|
|
|
c *tgbotapi.BotAPI
|
|
|
|
*config.BridgeConfig
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "telegram"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:15:03 +01:00
|
|
|
func New(cfg *config.BridgeConfig) *Btelegram {
|
|
|
|
return &Btelegram{BridgeConfig: cfg}
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-01-29 12:07:26 +01:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := b.c.GetUpdatesChan(u)
|
2016-11-15 23:15:57 +01:00
|
|
|
if err != nil {
|
|
|
|
flog.Debugf("%#v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
flog.Info("Connection succeeded")
|
|
|
|
go b.handleRecv(updates)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:12:02 +01:00
|
|
|
func (b *Btelegram) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:51:41 +02:00
|
|
|
func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
2016-11-15 23:15:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:59:37 +02:00
|
|
|
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
2016-11-15 23:15:57 +01:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
|
|
|
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
|
|
|
|
if err != nil {
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", err
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
2017-01-06 23:32:17 +01:00
|
|
|
|
2017-02-24 18:49:52 +01:00
|
|
|
if b.Config.MessageFormat == "HTML" {
|
|
|
|
msg.Text = makeHTML(msg.Text)
|
|
|
|
}
|
2017-08-28 20:38:37 +02:00
|
|
|
|
2017-09-11 23:12:33 +02:00
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = b.c.DeleteMessage(tgbotapi.DeleteMessageConfig{ChatID: chatid, MessageID: msgid})
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:38:37 +02:00
|
|
|
// edit the message if we have a msg ID
|
|
|
|
if msg.ID != "" {
|
|
|
|
msgid, err := strconv.Atoi(msg.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
2017-12-10 15:16:17 +01:00
|
|
|
if b.Config.MessageFormat == "HTML" {
|
2018-02-04 17:55:20 +01:00
|
|
|
flog.Debug("Using mode HTML")
|
2017-12-10 15:16:17 +01:00
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2018-02-03 23:31:21 +01:00
|
|
|
if b.Config.MessageFormat == "Markdown" {
|
2018-02-04 17:55:20 +01:00
|
|
|
flog.Debug("Using mode markdown")
|
2018-02-03 23:31:21 +01:00
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
|
|
|
}
|
2017-08-28 20:38:37 +02:00
|
|
|
_, err = b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:50:01 +01:00
|
|
|
if msg.Extra != nil {
|
2018-02-03 01:11:11 +01:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
|
|
|
b.sendMessage(chatid, rmsg.Username+rmsg.Text)
|
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
var c tgbotapi.Chattable
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
file := tgbotapi.FileBytes{fi.Name, *fi.Data}
|
|
|
|
re := regexp.MustCompile(".(jpg|png)$")
|
|
|
|
if re.MatchString(fi.Name) {
|
|
|
|
c = tgbotapi.NewPhotoUpload(chatid, file)
|
|
|
|
} else {
|
|
|
|
c = tgbotapi.NewDocumentUpload(chatid, file)
|
|
|
|
}
|
|
|
|
_, err := b.c.Send(c)
|
|
|
|
if err != nil {
|
2017-11-04 15:01:03 +01:00
|
|
|
log.Errorf("file upload failed: %#v", err)
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
2017-11-13 00:20:31 +01:00
|
|
|
if fi.Comment != "" {
|
|
|
|
b.sendMessage(chatid, msg.Username+fi.Comment)
|
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
2017-11-13 00:20:31 +01:00
|
|
|
return "", nil
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-13 00:20:31 +01:00
|
|
|
return b.sendMessage(chatid, msg.Username+msg.Text)
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|
|
|
for update := range updates {
|
2017-07-30 17:48:23 +02:00
|
|
|
flog.Debugf("Receiving from telegram: %#v", update.Message)
|
2018-02-07 14:28:48 +01:00
|
|
|
if update.Message == nil {
|
|
|
|
flog.Error("Getting nil messages, this shouldn't happen.")
|
|
|
|
continue
|
|
|
|
}
|
2017-04-01 18:18:38 +02:00
|
|
|
var message *tgbotapi.Message
|
2017-05-30 21:14:03 +02:00
|
|
|
username := ""
|
|
|
|
channel := ""
|
|
|
|
text := ""
|
2017-11-04 14:50:01 +01:00
|
|
|
|
|
|
|
fmsg := config.Message{Extra: make(map[string][]interface{})}
|
|
|
|
|
2017-01-27 23:26:06 +01:00
|
|
|
// handle channels
|
|
|
|
if update.ChannelPost != nil {
|
2017-04-01 18:18:38 +02:00
|
|
|
message = update.ChannelPost
|
|
|
|
}
|
2017-04-15 19:07:35 +02:00
|
|
|
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
2017-04-01 18:18:38 +02:00
|
|
|
message = update.EditedChannelPost
|
2017-04-15 19:07:35 +02:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
2017-01-27 23:26:06 +01:00
|
|
|
// handle groups
|
|
|
|
if update.Message != nil {
|
2017-04-01 18:18:38 +02:00
|
|
|
message = update.Message
|
|
|
|
}
|
2017-04-15 19:07:35 +02:00
|
|
|
if update.EditedMessage != nil && !b.Config.EditDisable {
|
2017-04-01 18:18:38 +02:00
|
|
|
message = update.EditedMessage
|
2017-04-15 19:07:35 +02:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2017-04-01 18:18:38 +02:00
|
|
|
}
|
|
|
|
if message.From != nil {
|
2017-05-15 23:23:10 +02:00
|
|
|
if b.Config.UseFirstName {
|
|
|
|
username = message.From.FirstName
|
|
|
|
}
|
2017-04-01 18:18:38 +02:00
|
|
|
if username == "" {
|
|
|
|
username = message.From.UserName
|
2017-05-15 23:23:10 +02:00
|
|
|
if username == "" {
|
|
|
|
username = message.From.FirstName
|
|
|
|
}
|
2017-01-27 23:26:06 +01:00
|
|
|
}
|
2017-04-01 18:18:38 +02:00
|
|
|
text = message.Text
|
|
|
|
channel = strconv.FormatInt(message.Chat.ID, 10)
|
2017-01-27 23:59:24 +01:00
|
|
|
}
|
2017-04-01 18:18:38 +02:00
|
|
|
|
2017-01-27 23:59:24 +01:00
|
|
|
if username == "" {
|
|
|
|
username = "unknown"
|
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
if message.Sticker != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Sticker, message.Caption, &fmsg)
|
2017-06-17 18:25:17 +02:00
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
if message.Video != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Video, message.Caption, &fmsg)
|
2017-06-17 18:25:17 +02:00
|
|
|
}
|
2017-11-12 11:46:32 +01:00
|
|
|
if message.Photo != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Photo, message.Caption, &fmsg)
|
2017-06-17 18:25:17 +02:00
|
|
|
}
|
2017-11-12 11:46:32 +01:00
|
|
|
if message.Document != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Document, message.Caption, &fmsg)
|
2017-06-17 18:25:17 +02:00
|
|
|
}
|
2017-12-10 15:08:23 +01:00
|
|
|
if message.Voice != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Voice, message.Caption, &fmsg)
|
2017-12-10 15:08:23 +01:00
|
|
|
}
|
|
|
|
if message.Audio != nil {
|
2018-02-01 00:41:09 +01:00
|
|
|
b.handleDownload(message.Audio, message.Caption, &fmsg)
|
2017-12-10 15:08:23 +01:00
|
|
|
}
|
2017-09-19 23:58:05 +02:00
|
|
|
|
2017-12-10 14:52:29 +01:00
|
|
|
if message.ForwardFrom != nil {
|
|
|
|
usernameForward := ""
|
|
|
|
if b.Config.UseFirstName {
|
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
|
|
|
}
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = message.ForwardFrom.UserName
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = message.ForwardFrom.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usernameForward == "" {
|
|
|
|
usernameForward = "unknown"
|
|
|
|
}
|
|
|
|
text = "Forwarded from " + usernameForward + ": " + text
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:58:05 +02:00
|
|
|
// quote the previous message
|
|
|
|
if message.ReplyToMessage != nil {
|
|
|
|
usernameReply := ""
|
|
|
|
if message.ReplyToMessage.From != nil {
|
|
|
|
if b.Config.UseFirstName {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.UserName
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = message.ReplyToMessage.From.FirstName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usernameReply == "" {
|
|
|
|
usernameReply = "unknown"
|
|
|
|
}
|
|
|
|
text = text + " (re @" + usernameReply + ":" + message.ReplyToMessage.Text + ")"
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:50:01 +01:00
|
|
|
if text != "" || len(fmsg.Extra) > 0 {
|
2017-01-27 23:59:24 +01:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
|
2017-11-12 18:09:38 +01:00
|
|
|
msg := config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: strconv.Itoa(message.From.ID), ID: strconv.Itoa(message.MessageID), Extra: fmsg.Extra}
|
2017-09-19 21:41:35 +02:00
|
|
|
flog.Debugf("Message is %#v", msg)
|
|
|
|
b.Remote <- msg
|
2017-01-27 23:26:06 +01:00
|
|
|
}
|
2016-11-15 23:15:57 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 18:25:17 +02:00
|
|
|
|
|
|
|
func (b *Btelegram) getFileDirectURL(id string) string {
|
|
|
|
res, err := b.c.GetFileDirectURL(id)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
|
2018-02-01 00:41:09 +01:00
|
|
|
func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config.Message) {
|
2017-11-04 14:50:01 +01:00
|
|
|
size := 0
|
|
|
|
url := ""
|
|
|
|
name := ""
|
|
|
|
text := ""
|
2017-11-12 17:46:44 +01:00
|
|
|
fileid := ""
|
2017-11-04 14:50:01 +01:00
|
|
|
switch v := file.(type) {
|
2017-12-10 15:08:23 +01:00
|
|
|
case *tgbotapi.Audio:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
|
|
|
text = " " + url
|
|
|
|
fileid = v.FileID
|
|
|
|
case *tgbotapi.Voice:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
|
|
|
text = " " + url
|
|
|
|
if !strings.HasSuffix(name, ".ogg") {
|
|
|
|
name = name + ".ogg"
|
|
|
|
}
|
|
|
|
fileid = v.FileID
|
2017-11-04 14:50:01 +01:00
|
|
|
case *tgbotapi.Sticker:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 22:04:35 +01:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-20 22:12:51 +01:00
|
|
|
if !strings.HasSuffix(name, ".webp") {
|
|
|
|
name = name + ".webp"
|
|
|
|
}
|
2017-11-04 14:50:01 +01:00
|
|
|
text = " " + url
|
2017-11-12 17:46:44 +01:00
|
|
|
fileid = v.FileID
|
2017-11-04 14:50:01 +01:00
|
|
|
case *tgbotapi.Video:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 22:04:35 +01:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 14:50:01 +01:00
|
|
|
text = " " + url
|
2017-11-12 17:46:44 +01:00
|
|
|
fileid = v.FileID
|
2017-11-04 14:50:01 +01:00
|
|
|
case *[]tgbotapi.PhotoSize:
|
|
|
|
photos := *v
|
|
|
|
size = photos[len(photos)-1].FileSize
|
|
|
|
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
|
2017-11-12 22:04:35 +01:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 14:50:01 +01:00
|
|
|
text = " " + url
|
|
|
|
case *tgbotapi.Document:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
|
|
|
name = v.FileName
|
|
|
|
text = " " + v.FileName + " : " + url
|
2017-11-12 17:46:44 +01:00
|
|
|
fileid = v.FileID
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
|
|
|
if b.Config.UseInsecureURL {
|
|
|
|
msg.Text = text
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
|
|
|
// limit to 1MB for now
|
2017-11-12 17:46:44 +01:00
|
|
|
flog.Debugf("trying to download %#v fileid %#v with size %#v", name, fileid, size)
|
2017-12-19 23:44:13 +01:00
|
|
|
if size <= b.General.MediaDownloadSize {
|
2017-11-04 14:50:01 +01:00
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("download %s failed %#v", url, err)
|
|
|
|
} else {
|
2017-11-12 17:49:10 +01:00
|
|
|
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url))
|
2018-02-01 00:41:09 +01:00
|
|
|
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, Comment: comment})
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
2018-02-03 01:11:11 +01:00
|
|
|
} else {
|
|
|
|
flog.Errorf("File %#v to large to download (%#v). MediaDownloadSize is %#v", name, size, b.General.MediaDownloadSize)
|
|
|
|
msg.Event = config.EVENT_FILE_FAILURE_SIZE
|
|
|
|
msg.Extra[msg.Event] = append(msg.Extra[msg.Event], config.FileInfo{Name: name, Comment: comment, Size: int64(size)})
|
2017-11-04 14:50:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-13 00:20:31 +01:00
|
|
|
|
|
|
|
func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) {
|
|
|
|
m := tgbotapi.NewMessage(chatid, text)
|
|
|
|
if b.Config.MessageFormat == "HTML" {
|
2018-02-04 17:55:20 +01:00
|
|
|
flog.Debug("Using mode HTML")
|
2017-11-13 00:20:31 +01:00
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2018-02-03 23:31:21 +01:00
|
|
|
if b.Config.MessageFormat == "Markdown" {
|
2018-02-04 17:55:20 +01:00
|
|
|
flog.Debug("Using mode markdown")
|
2018-02-03 23:31:21 +01:00
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
|
|
|
}
|
2017-11-13 00:20:31 +01:00
|
|
|
res, err := b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.Itoa(res.MessageID), nil
|
|
|
|
}
|