-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp.go
56 lines (45 loc) · 1.04 KB
/
xmpp.go
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
package announcebot
import (
"crypto/tls"
"time"
"github.com/mattn/go-xmpp"
log "github.com/Sirupsen/logrus"
)
func (bot *AnnounceBot) getXMPPClient() (*xmpp.Client, error) {
options := xmpp.Options{
Host: bot.Config.HipchatXMPPHost,
User: bot.Config.HipchatUser,
Password: bot.Config.HipchatPassword,
NoTLS: true,
}
options.TLSConfig = new(tls.Config)
options.TLSConfig.ServerName = "chat.hipchat.com"
return options.NewClient()
}
func (bot *AnnounceBot) serveXMPP() {
talk, err := bot.getXMPPClient()
if err != nil {
log.Fatal(err)
}
log.Info("XMPP client connected successfully")
go func() {
for {
talk.PingC2S(bot.Config.HipchatUser, bot.Config.HipchatXMPPHost)
log.Debug("Sent XMPP ping")
time.Sleep(30 * time.Second)
}
}()
go func() {
for {
chat, err := talk.Recv()
if err != nil {
log.Fatal(err)
}
log.WithField("chat_message", chat).Debug("Received XMPP message")
switch v := chat.(type) {
case xmpp.Chat:
bot.handleChatMessage(talk, v.Remote, v.Text)
}
}
}()
}