summaryrefslogtreecommitdiffstats
path: root/bridge/keybase/handlers.go
blob: a29208d40188324ab0f21f5a72fd3d7b7ee353d1 (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/types/chat1"
)

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.TypeName != "text" {
				continue
			}

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

			b.handleMessage(msg.Message)

		}
	}()
}

func (b *Bkeybase) handleMessage(msg chat1.MsgSummary) {
	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: string(msg.Sender.Uid), Channel: msg.Channel.TopicName, ID: strconv.Itoa(int(msg.Id)), Account: b.Account}

		// Text must be a string
		if msg.Content.TypeName != "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
	}
}