Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 90156c2d0532b43430190b1834de72d147dec765 Author: Wim <wim@42.be> Date: Tue Nov 15 23:15:57 2016 +0000 Add initial telegram support diff --git a/bridge/bridge.go b/bridge/bridge.go index 4a422c7..8c71536 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -76 +77 @@ 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"  ) @@ -556 +569 @@ 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 d3d8811..5fed2f0 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -786 +787 @@ 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 0000000..aaffe83 --- /dev/null +++ b/bridge/telegram/telegram.go @@ -00 +174 @@ +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} + + } +}