2016-11-15 22:15:57 +00:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2018-06-18 21:38:52 +00:00
|
|
|
"html"
|
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
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
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
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
2018-11-08 21:20:03 +00:00
|
|
|
const (
|
|
|
|
unknownUser = "unknown"
|
|
|
|
HTMLFormat = "HTML"
|
|
|
|
HTMLNick = "htmlnick"
|
|
|
|
)
|
|
|
|
|
2016-11-15 22:15:57 +00:00
|
|
|
type Btelegram struct {
|
2017-12-19 22:15:03 +00:00
|
|
|
c *tgbotapi.BotAPI
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2018-02-19 23:54:35 +00:00
|
|
|
avatarMap map[string]string // keep cache of userid and avatar sha
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
return &Btelegram{Config: cfg, avatarMap: make(map[string]string)}
|
2016-11-15 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) Connect() error {
|
|
|
|
var err error
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.c, err = tgbotapi.NewBotAPI(b.GetString("Token"))
|
2016-11-15 22:15:57 +00:00
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("%#v", err)
|
2016-11-15 22:15:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-29 11:07:26 +00:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := b.c.GetUpdatesChan(u)
|
2016-11-15 22:15:57 +00:00
|
|
|
if err != nil {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("%#v", err)
|
2016-11-15 22:15:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connection succeeded")
|
2016-11-15 22:15:57 +00:00
|
|
|
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) {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-25 22:54:20 +00:00
|
|
|
|
|
|
|
// get the chatid
|
2016-11-15 22:15:57 +00:00
|
|
|
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
|
|
|
|
2018-02-19 23:54:35 +00:00
|
|
|
// map the file SHA to our user (caches the avatar)
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventAvatarDownload {
|
2018-02-25 22:54:20 +00:00
|
|
|
return b.cacheAvatar(&msg)
|
2018-02-19 23:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 21:20:03 +00:00
|
|
|
if b.GetString("MessageFormat") == HTMLFormat {
|
2017-02-24 17:49:52 +00:00
|
|
|
msg.Text = makeHTML(msg.Text)
|
|
|
|
}
|
2017-08-28 18:38:37 +00:00
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
// Delete message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2018-12-12 22:50:08 +00:00
|
|
|
return b.handleDelete(&msg, chatid)
|
2017-09-11 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
// Upload a file if it exists
|
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-11-28 09:57:59 +00:00
|
|
|
if _, err := b.sendMessage(chatid, rmsg.Username, rmsg.Text); err != nil {
|
|
|
|
b.Log.Errorf("sendMessage failed: %s", err)
|
|
|
|
}
|
2018-02-25 22:54:20 +00:00
|
|
|
}
|
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
b.handleUploadFile(&msg, chatid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:38:37 +00:00
|
|
|
// edit the message if we have a msg ID
|
|
|
|
if msg.ID != "" {
|
2018-12-12 22:50:08 +00:00
|
|
|
return b.handleEdit(&msg, chatid)
|
2017-08-28 18:38:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 22:54:20 +00:00
|
|
|
// Post normal message
|
2018-02-28 22:25:00 +00:00
|
|
|
return b.sendMessage(chatid, msg.Username, msg.Text)
|
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
|
|
|
|
2018-02-28 22:25:00 +00:00
|
|
|
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
|
|
|
m := tgbotapi.NewMessage(chatid, "")
|
|
|
|
m.Text = username + text
|
2018-11-08 21:20:03 +00:00
|
|
|
if b.GetString("MessageFormat") == HTMLFormat {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debug("Using mode HTML")
|
2017-11-12 23:20:31 +00:00
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("MessageFormat") == "Markdown" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debug("Using mode markdown")
|
2018-02-03 22:31:21 +00:00
|
|
|
m.ParseMode = tgbotapi.ModeMarkdown
|
|
|
|
}
|
2018-11-08 21:20:03 +00:00
|
|
|
if strings.ToLower(b.GetString("MessageFormat")) == HTMLNick {
|
2018-06-18 21:38:52 +00:00
|
|
|
b.Log.Debug("Using mode HTML - nick only")
|
|
|
|
m.Text = username + html.EscapeString(text)
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2017-11-12 23:20:31 +00:00
|
|
|
res, err := b.c.Send(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.Itoa(res.MessageID), nil
|
|
|
|
}
|
2018-02-25 22:54:20 +00:00
|
|
|
|
|
|
|
func (b *Btelegram) cacheAvatar(msg *config.Message) (string, error) {
|
|
|
|
fi := msg.Extra["file"][0].(config.FileInfo)
|
|
|
|
/* if we have a sha we have successfully uploaded the file to the media server,
|
|
|
|
so we can now cache the sha */
|
|
|
|
if fi.SHA != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Added %s to %s in avatarMap", fi.SHA, msg.UserID)
|
2018-02-25 22:54:20 +00:00
|
|
|
b.avatarMap[msg.UserID] = fi.SHA
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|