summaryrefslogtreecommitdiffstats
path: root/bridge/telegram/telegram.go
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/telegram/telegram.go
parent449ed31e25f2c21ac9db258999a5a893889983eb (diff)
downloadmatterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.tar.gz
matterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.tar.bz2
matterbridge-msglm-cd18d89894903a92ac12e1a2e78582b543a4ab6e.zip
Add initial telegram support
Diffstat (limited to 'bridge/telegram/telegram.go')
-rw-r--r--bridge/telegram/telegram.go74
1 files changed, 74 insertions, 0 deletions
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}
+
+ }
+}