2016-11-15 14:15:57 -08:00
|
|
|
package btelegram
|
|
|
|
|
|
|
|
import (
|
2017-01-06 14:32:17 -08:00
|
|
|
"strconv"
|
|
|
|
|
2016-11-15 14:15:57 -08: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 12:12:02 -08:00
|
|
|
func (b *Btelegram) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-15 14:15:57 -08:00
|
|
|
func (b *Btelegram) JoinChannel(channel string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-01-06 14:32:17 -08:00
|
|
|
|
2017-02-24 09:49:52 -08: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 14:15:57 -08:00
|
|
|
_, err = b.c.Send(m)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
2017-01-27 14:26:06 -08:00
|
|
|
username := ""
|
|
|
|
text := ""
|
|
|
|
channel := ""
|
2016-11-15 14:15:57 -08:00
|
|
|
for update := range updates {
|
2017-04-01 09:18:38 -07:00
|
|
|
var message *tgbotapi.Message
|
2017-01-27 14:26:06 -08:00
|
|
|
// handle channels
|
|
|
|
if update.ChannelPost != nil {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.ChannelPost
|
|
|
|
}
|
2017-04-15 10:07:35 -07:00
|
|
|
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.EditedChannelPost
|
2017-04-15 10:07:35 -07:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
2017-01-27 14:26:06 -08:00
|
|
|
// handle groups
|
|
|
|
if update.Message != nil {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.Message
|
|
|
|
}
|
2017-04-15 10:07:35 -07:00
|
|
|
if update.EditedMessage != nil && !b.Config.EditDisable {
|
2017-04-01 09:18:38 -07:00
|
|
|
message = update.EditedMessage
|
2017-04-15 10:07:35 -07:00
|
|
|
message.Text = message.Text + b.Config.EditSuffix
|
2017-04-01 09:18:38 -07:00
|
|
|
}
|
|
|
|
if message.From != nil {
|
|
|
|
username = message.From.FirstName
|
|
|
|
if username == "" {
|
|
|
|
username = message.From.UserName
|
2017-01-27 14:26:06 -08:00
|
|
|
}
|
2017-04-01 09:18:38 -07:00
|
|
|
text = message.Text
|
|
|
|
channel = strconv.FormatInt(message.Chat.ID, 10)
|
2017-01-27 14:59:24 -08:00
|
|
|
}
|
2017-04-01 09:18:38 -07:00
|
|
|
|
2017-01-27 14:59:24 -08:00
|
|
|
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}
|
2017-01-27 14:26:06 -08:00
|
|
|
}
|
2016-11-15 14:15:57 -08:00
|
|
|
}
|
|
|
|
}
|