summaryrefslogtreecommitdiffstats
path: root/bridge/keybase/handlers.go
blob: 165aeb16ecc746b0a57809134b88fb1f627c5ed2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package bkeybase

import (
	"strconv"

	"github.com/42wim/matterbridge/bridge/config"
	"github.com/keybase/go-keybase-chat-bot/kbchat"
)

func (b *Bkeybase) handleKeybase() {
	sub, err := b.kbc.ListenForNewTextMessages()
	if err != nil {
		b.Log.Errorf("Error listening: %s", err.Error())
	}

	go func() {
		for {
			msg, err := sub.Read()
			if err != nil {
				b.Log.Errorf("failed to read message: %s", err.Error())
			}

			if msg.Message.Content.Type != "text" {
				continue
			}

			if msg.Message.Sender.Username == b.kbc.GetUsername() {
				continue
			}

			b.handleMessage(msg.Message)

		}
	}()
}

func (b *Bkeybase) handleMessage(msg kbchat.Message) {
	b.Log.Debugf("== Receiving event: %#v", msg)
	if msg.Channel.TopicName != b.channel || msg.Channel.Name != b.team {
		return
	}

	if msg.Sender.Username != b.kbc.GetUsername() {

		// TODO download avatar

		// Create our message
		rmsg := config.Message{Username: msg.Sender.Username, Text: msg.Content.Text.Body, UserID: msg.Sender.Uid, Channel: msg.Channel.TopicName, ID: strconv.Itoa(msg.MsgID), Account: b.Account}

		// Text must be a string
		if msg.Content.Type != "text" {
			b.Log.Errorf("message is not text")
			return
		}

		b.Log.Debugf("<= Sending message from %s on %s to gateway", msg.Sender.Username, msg.Channel.Name)
		b.Remote <- rmsg
	}
}