{"id":1169,"date":"2014-07-02T09:24:11","date_gmt":"2014-07-02T09:24:11","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1169"},"modified":"2014-07-02T09:25:45","modified_gmt":"2014-07-02T09:25:45","slug":"simple-hipchat-bot-in-go","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/07\/simple-hipchat-bot-in-go\/","title":{"rendered":"Simple hipchat bot in go"},"content":{"rendered":"<p>This is some very simple hipchat bot code in golang. It will tell you the time in a few locations when you mention it.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npackage main\r\n\r\nimport &quot;github.com\/daneharrigan\/hipchat&quot;\r\nimport &quot;fmt&quot;\r\nimport &quot;time&quot;\r\nimport &quot;strings&quot;\r\nimport &quot;log&quot;\r\n\r\nfunc main() {\r\nuser := &quot;NNNNN_NNNNNN&quot;\r\npass := &quot;password&quot;\r\nresource := &quot;bot&quot; \/\/should be bot\r\nroomJid := &quot;NNNNN_text_text@conf.hipchat.com&quot;\r\nfullName := &quot;mr bot&quot; \/\/seems to need to be lowercase\r\nmentionName := &quot;mrbot&quot;\r\n\r\nclient, err := hipchat.NewClient(user, pass, resource)\r\nif err != nil {\r\n  fmt.Printf(&quot;client error: %s\\n&quot;, err)\r\n  return\r\n}\r\nfmt.Printf(&quot;Connected\\n&quot;)\r\n\r\nrooms := client.Rooms()\r\nfor _, element := range rooms {\r\n  fmt.Printf(&quot;Room: %s\\n&quot;, element.Id)\r\n  client.Join(element.Id, fullName)\r\n}\r\n\r\nclient.Status(&quot;chat&quot;)\r\n\/\/ for _,element := range rooms {\r\n\/\/ client.Say(element.Id, fullName, &quot;Hello&quot;)\r\n\/\/ }\r\n\r\nfor message := range client.Messages() {\r\n  if strings.HasPrefix(message.Body, &quot;@&quot;+mentionName) {\r\n    t := time.Now()\r\n    gmt, er1 := time.LoadLocation(&quot;Europe\/London&quot;)\r\n    sf, er2 := time.LoadLocation(&quot;America\/Los_Angeles&quot;)\r\n    if er1 != nil {\r\n      log.Panic(err)\r\n    }\r\n    if er2 != nil {\r\n      log.Panic(err)\r\n    }\r\n\r\n    timestr := t.Format(&quot;Jan 2, 2006 at 3:04pm (MST)&quot;)\r\n    client.Say(roomJid, fullName, &quot;The time in Japan is:&quot;+timestr)\r\n\r\n    t = t.In(gmt)\r\n    timestr = t.Format(&quot;Jan 2, 2006 at 3:04pm (MST)&quot;)\r\n    client.Say(roomJid, fullName, &quot;The time in London (and Dublin) is:&quot;+timestr)\r\n\r\n    t = t.In(sf)\r\n    timestr = t.Format(&quot;Jan 2, 2006 at 3:04pm (MST)&quot;)\r\n    client.Say(roomJid, fullName, &quot;The time in San Francisco is:&quot;+timestr)\r\n  }\r\n}\r\n\r\nselect {}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is some very simple hipchat bot code in golang. It will tell you the time in a few locations when you mention it. package main import &quot;github.com\/daneharrigan\/hipchat&quot; import &quot;fmt&quot; import &quot;time&quot; import &quot;strings&quot; import &quot;log&quot; func main() { user := &quot;NNNNN_NNNNNN&quot; pass := &quot;password&quot; resource := &quot;bot&quot; \/\/should be bot roomJid := &quot;NNNNN_text_text@conf.hipchat.com&quot; fullName [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1169","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-iR","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1172,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1172"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}