2016-11-15 23:15:57 +01:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2017-01-06 23:32:17 +01:00
|
|
|
"strconv"
|
|
|
|
|
2016-11-15 23:15:57 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text)
|
|
|
|
if b.Config.MessageFormat == "HTML" {
|
|
|
|
m.ParseMode = tgbotapi.ModeHTML
|
|
|
|
}
|
2016-11-15 23:15:57 +01:00
|
|
|
_, err = b.c.Send(m)
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", err
|
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)
|
2017-04-01 18:18:38 +02:00
|
|
|
var message *tgbotapi.Message
|
2017-05-30 21:14:03 +02:00
|
|
|
username := ""
|
|
|
|
channel := ""
|
|
|
|
text := ""
|
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-07-04 01:34:02 +02:00
|
|
|
if message.Sticker != nil && b.Config.UseInsecureURL {
|
2017-06-17 18:25:17 +02:00
|
|
|
text = text + " " + b.getFileDirectURL(message.Sticker.FileID)
|
|
|
|
}
|
2017-07-04 01:34:02 +02:00
|
|
|
if message.Video != nil && b.Config.UseInsecureURL {
|
2017-06-17 18:25:17 +02:00
|
|
|
text = text + " " + b.getFileDirectURL(message.Video.FileID)
|
|
|
|
}
|
2017-07-04 01:34:02 +02:00
|
|
|
if message.Photo != nil && b.Config.UseInsecureURL {
|
2017-06-18 01:23:15 +02:00
|
|
|
photos := *message.Photo
|
2017-06-18 23:59:52 +02:00
|
|
|
// last photo is the biggest
|
|
|
|
text = text + " " + b.getFileDirectURL(photos[len(photos)-1].FileID)
|
2017-06-17 18:25:17 +02:00
|
|
|
}
|
2017-07-04 01:34:02 +02:00
|
|
|
if message.Document != nil && b.Config.UseInsecureURL {
|
2017-06-17 18:25:17 +02:00
|
|
|
text = text + " " + message.Document.FileName + " : " + b.getFileDirectURL(message.Document.FileID)
|
|
|
|
}
|
2017-01-27 23:59:24 +01:00
|
|
|
if text != "" {
|
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", username, b.Account)
|
2017-06-18 15:44:54 +02:00
|
|
|
b.Remote <- config.Message{Username: username, Text: text, Channel: channel, Account: b.Account, UserID: strconv.Itoa(message.From.ID)}
|
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
|
|
|
|
}
|