summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-11-15 23:15:57 +0100
committerWim <wim@42.be>2016-11-15 23:15:57 +0100
commitcd18d89894903a92ac12e1a2e78582b543a4ab6e (patch)
treede70d2f4abb65992170b2d07e187ef4cb58e3c8c /bridge
parent449ed31e25f2c21ac9db258999a5a893889983eb (diff)
downloadmatterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.tar.gz
matterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.tar.bz2
matterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.zip
Add initial telegram support
Diffstat (limited to 'bridge')
-rw-r--r--bridge/bridge.go4
-rw-r--r--bridge/config/config.go1
-rw-r--r--bridge/telegram/telegram.go74
3 files changed, 79 insertions, 0 deletions
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}
+
+ }
+}