2016-11-15 22:15:57 +00:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2017-11-04 13:50:01 +00:00
|
|
|
"regexp"
|
2017-01-06 22:32:17 +00:00
|
|
|
"strconv"
|
2017-11-12 21:04:35 +00:00
|
|
|
"strings"
|
2017-01-06 22:32:17 +00:00
|
|
|
|
2016-11-15 22:15:57 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2017-11-04 13:50:01 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2016-11-15 22:15:57 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Btelegram struct {
|
2017-12-19 22:15:03 +00:00
|
|
|
c *tgbotapi.BotAPI
|
|
|
|
*config.BridgeConfig
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
|
|
|
var protocol = "telegram"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:15:03 +00:00
|
|
|
func New(cfg *config.BridgeConfig) *Btelegram {
|
|
|
|
return &Btelegram{BridgeConfig: cfg}
|
2016-11-15 22:15:57 +00: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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:12:02 +00:00
|
|
|
func (b *Btelegram) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
|
2016-11-15 22:15:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 20:59:37 +00:00
|
|
|
func (b *Btelegram) Send(msg config.Message) (string, error) {
|
2016-11-15 22:15:57 +00:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
|
|
|
chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
|
|
|
|
if err != nil {
|
2017-08-27 20:59:37 +00:00
|
|
|
return "", err
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
2017-01-06 22:32:17 +00:00
|
|
|
|
2017-02-24 17:49:52 +00:00
|
|
|
if b.Config.MessageFormat == "HTML" {
|
|
|
|
msg.Text = makeHTML(msg.Text)
|
|
|
|
}
|
2017-08-28 18:38:37 +00:00
|
|
|
|
2017-09-11 21:12:33 +00: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 18:38:37 +00: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 14:16:17 +00:00
|
|
|
if b.Config.MessageFormat == "HTML" {
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2017-08-28 18:38:37 +00:00
|
|
|
_, err = b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-11-04 13:50:01 +00:00
|
|
|
if msg.Extra != nil {
|
|
|
|
// 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 14:01:03 +00:00
|
|
|
log.Errorf("file upload failed: %#v", err)
|
2017-11-04 13:50:01 +00:00
|
|
|
}
|
2017-11-12 23:20:31 +00:00
|
|
|
if fi.Comment != "" {
|
|
|
|
b.sendMessage(chatid, msg.Username+fi.Comment)
|
|
|
|
}
|
2017-11-04 13:50:01 +00:00
|
|
|
}
|
2017-11-12 23:20:31 +00:00
|
|
|
return "", nil
|
2017-11-04 13:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-12 23:20:31 +00:00
|
|
|
return b.sendMessage(chatid, msg.Username+msg.Text)
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|
|
|
for update := range updates {
|
2017-07-30 15:48:23 +00:00
|
|
|
flog.Debugf("Receiving from telegram: %#v", update.Message)
|
2017-04-01 16:18:38 +00:00
|
|
|
var message *tgbotapi.Message
|
2017-05-30 19:14:03 +00:00
|
|
|
username := ""
|
|
|
|
channel := ""
|
|
|
|
text := ""
|
2017-11-04 13:50:01 +00:00
|
|
|
|
|
|
|
fmsg := config.Message{Extra: make(map[string][]interface{})}
|
|
|
|
|
2017-01-27 22:26:06 +00:00
|
|
|
// handle channels
|
|
|
|
if update.ChannelPost != nil {
|
2017-04-01 16:18:38 +00:00
|
|
|
message = update.ChannelPost
|
|
|
|
}
|
2017-04-15 17:07:35 +00:00
|
|
|
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
2017-04-01 16:18:38 +00:00
|
|
|
message = update.EditedChannelPost
|
2017-04-15 17:07:35 +00:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
2017-01-27 22:26:06 +00:00
|
|
|
// handle groups
|
|
|
|
if update.Message != nil {
|
2017-04-01 16:18:38 +00:00
|
|
|
message = update.Message
|
|
|
|
}
|
2017-04-15 17:07:35 +00:00
|
|
|
if update.EditedMessage != nil && !b.Config.EditDisable {
|
2017-04-01 16:18:38 +00:00
|
|
|
message = update.EditedMessage
|
2017-04-15 17:07:35 +00:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2017-04-01 16:18:38 +00:00
|
|
|
}
|
|
|
|
if message.From != nil {
|
2017-05-15 21:23:10 +00:00
|
|
|
if b.Config.UseFirstName {
|
|
|
|
username = message.From.FirstName
|
|
|
|
}
|
2017-04-01 16:18:38 +00:00
|
|
|
if username == "" {
|
|
|
|
username = message.From.UserName
|
2017-05-15 21:23:10 +00:00
|
|
|
if username == "" {
|
|
|
|
username = message.From.FirstName
|
|
|
|
}
|
2017-01-27 22:26:06 +00:00
|
|
|
}
|
2017-04-01 16:18:38 +00:00
|
|
|
text = message.Text
|
|
|
|
channel = strconv.FormatInt(message.Chat.ID, 10)
|
2017-01-27 22:59:24 +00:00
|
|
|
}
|
2017-04-01 16:18:38 +00:00
|
|
|
|
2017-01-27 22:59:24 +00:00
|
|
|
if username == "" {
|
|
|
|
username = "unknown"
|
|
|
|
}
|
2017-11-04 13:50:01 +00:00
|
|
|
if message.Sticker != nil {
|
|
|
|
b.handleDownload(message.Sticker, &fmsg)
|
2017-06-17 16:25:17 +00:00
|
|
|
}
|
2017-11-04 13:50:01 +00:00
|
|
|
if message.Video != nil {
|
|
|
|
b.handleDownload(message.Video, &fmsg)
|
2017-06-17 16:25:17 +00:00
|
|
|
}
|
2017-11-12 10:46:32 +00:00
|
|
|
if message.Photo != nil {
|
2017-11-04 13:50:01 +00:00
|
|
|
b.handleDownload(message.Photo, &fmsg)
|
2017-06-17 16:25:17 +00:00
|
|
|
}
|
2017-11-12 10:46:32 +00:00
|
|
|
if message.Document != nil {
|
2017-11-12 16:15:53 +00:00
|
|
|
b.handleDownload(message.Document, &fmsg)
|
2017-06-17 16:25:17 +00:00
|
|
|
}
|
2017-12-10 14:08:23 +00:00
|
|
|
if message.Voice != nil {
|
|
|
|
b.handleDownload(message.Voice, &fmsg)
|
|
|
|
}
|
|
|
|
if message.Audio != nil {
|
|
|
|
b.handleDownload(message.Audio, &fmsg)
|
|
|
|
}
|
2017-09-19 21:58:05 +00:00
|
|
|
|
2017-12-10 13:52:29 +00: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 21:58:05 +00: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 13:50:01 +00:00
|
|
|
if text != "" || len(fmsg.Extra) > 0 {
|
2017-01-27 22:59:24 +00:00
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
|
2017-11-12 17:09:38 +00: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 19:41:35 +00:00
|
|
|
flog.Debugf("Message is %#v", msg)
|
|
|
|
b.Remote <- msg
|
2017-01-27 22:26:06 +00:00
|
|
|
}
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 16:25:17 +00:00
|
|
|
|
|
|
|
func (b *Btelegram) getFileDirectURL(id string) string {
|
|
|
|
res, err := b.c.GetFileDirectURL(id)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2017-11-04 13:50:01 +00:00
|
|
|
|
|
|
|
func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) {
|
|
|
|
size := 0
|
|
|
|
url := ""
|
|
|
|
name := ""
|
|
|
|
text := ""
|
2017-11-12 16:46:44 +00:00
|
|
|
fileid := ""
|
2017-11-04 13:50:01 +00:00
|
|
|
switch v := file.(type) {
|
2017-12-10 14:08:23 +00: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 13:50:01 +00:00
|
|
|
case *tgbotapi.Sticker:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 21:04:35 +00:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-20 21:12:51 +00:00
|
|
|
if !strings.HasSuffix(name, ".webp") {
|
|
|
|
name = name + ".webp"
|
|
|
|
}
|
2017-11-04 13:50:01 +00:00
|
|
|
text = " " + url
|
2017-11-12 16:46:44 +00:00
|
|
|
fileid = v.FileID
|
2017-11-04 13:50:01 +00:00
|
|
|
case *tgbotapi.Video:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
2017-11-12 21:04:35 +00:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 13:50:01 +00:00
|
|
|
text = " " + url
|
2017-11-12 16:46:44 +00:00
|
|
|
fileid = v.FileID
|
2017-11-04 13:50:01 +00:00
|
|
|
case *[]tgbotapi.PhotoSize:
|
|
|
|
photos := *v
|
|
|
|
size = photos[len(photos)-1].FileSize
|
|
|
|
url = b.getFileDirectURL(photos[len(photos)-1].FileID)
|
2017-11-12 21:04:35 +00:00
|
|
|
urlPart := strings.Split(url, "/")
|
|
|
|
name = urlPart[len(urlPart)-1]
|
2017-11-04 13:50:01 +00:00
|
|
|
text = " " + url
|
|
|
|
case *tgbotapi.Document:
|
|
|
|
size = v.FileSize
|
|
|
|
url = b.getFileDirectURL(v.FileID)
|
|
|
|
name = v.FileName
|
|
|
|
text = " " + v.FileName + " : " + url
|
2017-11-12 16:46:44 +00:00
|
|
|
fileid = v.FileID
|
2017-11-04 13:50:01 +00: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 16:46:44 +00:00
|
|
|
flog.Debugf("trying to download %#v fileid %#v with size %#v", name, fileid, size)
|
2017-12-19 22:44:13 +00:00
|
|
|
if size <= b.General.MediaDownloadSize {
|
2017-11-04 13:50:01 +00:00
|
|
|
data, err := helper.DownloadFile(url)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("download %s failed %#v", url, err)
|
|
|
|
} else {
|
2017-11-12 16:49:10 +00:00
|
|
|
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url))
|
2017-11-04 13:50:01 +00:00
|
|
|
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 23:20:31 +00:00
|
|
|
|
|
|
|
func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) {
|
|
|
|
m := tgbotapi.NewMessage(chatid, text)
|
|
|
|
if b.Config.MessageFormat == "HTML" {
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
|
|
|
res, err := b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.Itoa(res.MessageID), nil
|
|
|
|
}
|