Nick Groenen f8537c63a5
Allow PrefixMessagesWithNick on Telegram
This makes the PrefixMessagesWithNick setting work for messages relayed
to Telegram, similar to how they work for messages relayed to mattermost
(it is in fact copied from the mattermost bridge implementation).

I believe this logic should be moved up the stack and not left to
individual bridge implementations, but adding it here like this made it
quickly work for my use-case (I don't have the time to dig in deeper and
refactor everything).
2016-11-25 20:44:01 +01:00

80 lines
1.8 KiB
Go

package btelegram
import (
"github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus"
"github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
)
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
}
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
}
message := msg.Text
if b.Config.PrefixMessagesWithNick {
message = msg.Username + " " + message
}
m := tgbotapi.NewMessage(chatid, message)
_, err = b.c.Send(m)
return err
}
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
for update := range updates {
if update.Message == nil {
continue
}
flog.Debugf("Sending message from %s on %s to gateway", update.Message.From.UserName, b.Account)
b.Remote <- config.Message{Username: update.Message.From.UserName, Text: update.Message.Text, Channel: strconv.FormatInt(update.Message.Chat.ID, 10), Account: b.Account}
}
}