Simple hipchat bot in go

This is some very simple hipchat bot code in golang. It will tell you the time in a few locations when you mention it.

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
60
61
package main
 
import "github.com/daneharrigan/hipchat"
import "fmt"
import "time"
import "strings"
import "log"
 
func main() {
user := "NNNNN_NNNNNN"
pass := "password"
resource := "bot" //should be bot
roomJid := "NNNNN_text_text@conf.hipchat.com"
fullName := "mr bot" //seems to need to be lowercase
mentionName := "mrbot"
 
client, err := hipchat.NewClient(user, pass, resource)
if err != nil {
  fmt.Printf("client error: %s\n", err)
  return
}
fmt.Printf("Connected\n")
 
rooms := client.Rooms()
for _, element := range rooms {
  fmt.Printf("Room: %s\n", element.Id)
  client.Join(element.Id, fullName)
}
 
client.Status("chat")
// for _,element := range rooms {
// client.Say(element.Id, fullName, "Hello")
// }
 
for message := range client.Messages() {
  if strings.HasPrefix(message.Body, "@"+mentionName) {
    t := time.Now()
    gmt, er1 := time.LoadLocation("Europe/London")
    sf, er2 := time.LoadLocation("America/Los_Angeles")
    if er1 != nil {
      log.Panic(err)
    }
    if er2 != nil {
      log.Panic(err)
    }
 
    timestr := t.Format("Jan 2, 2006 at 3:04pm (MST)")
    client.Say(roomJid, fullName, "The time in Japan is:"+timestr)
 
    t = t.In(gmt)
    timestr = t.Format("Jan 2, 2006 at 3:04pm (MST)")
    client.Say(roomJid, fullName, "The time in London (and Dublin) is:"+timestr)
 
    t = t.In(sf)
    timestr = t.Format("Jan 2, 2006 at 3:04pm (MST)")
    client.Say(roomJid, fullName, "The time in San Francisco is:"+timestr)
  }
}
 
select {}
}

Leave a Reply