From cd18d89894903a92ac12e1a2e78582b543a4ab6e Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 15 Nov 2016 23:15:57 +0100 Subject: Add initial telegram support --- bridge/bridge.go | 4 +++ bridge/config/config.go | 1 + bridge/telegram/telegram.go | 74 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 bridge/telegram/telegram.go (limited to 'bridge') diff --git a/bridge/bridge.go b/bridge/bridge.go index 4a422c75..8c715369 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -7,6 +7,7 @@ import ( "github.com/42wim/matterbridge/bridge/irc" "github.com/42wim/matterbridge/bridge/mattermost" "github.com/42wim/matterbridge/bridge/slack" + "github.com/42wim/matterbridge/bridge/telegram" "github.com/42wim/matterbridge/bridge/xmpp" "strings" ) @@ -55,6 +56,9 @@ func New(cfg *config.Config, bridge *config.Bridge, c chan config.Message) *Brid case "discord": b.Config = cfg.Discord[name] b.Bridger = bdiscord.New(cfg.Discord[name], bridge.Account, c) + case "telegram": + b.Config = cfg.Telegram[name] + b.Bridger = btelegram.New(cfg.Telegram[name], bridge.Account, c) } return b } diff --git a/bridge/config/config.go b/bridge/config/config.go index d3d88115..5fed2f0a 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -78,6 +78,7 @@ type Config struct { Gitter map[string]Protocol Xmpp map[string]Protocol Discord map[string]Protocol + Telegram map[string]Protocol Gateway []Gateway SameChannelGateway []SameChannelGateway } diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go new file mode 100644 index 00000000..aaffe839 --- /dev/null +++ b/bridge/telegram/telegram.go @@ -0,0 +1,74 @@ +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 + } + m := tgbotapi.NewMessage(chatid, msg.Text) + _, 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} + + } +} -- cgit v1.2.3