Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active December 28, 2025 22:52
Show Gist options
  • Select an option

  • Save ewalk153/a345e8738450d4086f8d to your computer and use it in GitHub Desktop.

Select an option

Save ewalk153/a345e8738450d4086f8d to your computer and use it in GitHub Desktop.
Mini-bot is a mini hipchat bot that listens in a given room, and runs actions based on user interactions
package main
import (
"errors"
"fmt"
api "github.com/andybons/hipchat"
"github.com/daneharrigan/hipchat"
"github.com/joho/godotenv"
"os"
"strings"
)
type Bot struct {
User string
Pass string
AuthToken string
FullName string
MentionName string
}
type BotClient struct {
Bot *Bot
ChatClient *hipchat.Client
APIClient api.Client
}
var RoomNotFound = errors.New("Room not found")
func (b BotClient) RoomJabberIdByName(name string) (string, error) {
rooms, err := b.APIClient.RoomList()
if err != nil {
return "", err
}
for _, room := range rooms {
if room.Name == name {
return room.XMPPJabberId, nil
}
}
return "", RoomNotFound
}
func NewBot(user, pass, authToken string) *Bot {
b := &Bot{User: user, Pass: pass, AuthToken: authToken}
return b
}
func (b *BotClient) LoadName() {
for _, user := range b.ChatClient.Users() {
if user.Id == b.ChatClient.Id {
b.Bot.FullName = user.Name
b.Bot.MentionName = user.MentionName
break
}
}
}
func (b *BotClient) Connect() error {
resource := "bot"
b.APIClient = api.Client{AuthToken: b.Bot.AuthToken}
var err error
b.ChatClient, err = hipchat.NewClient(b.Bot.User, b.Bot.Pass, resource)
if err != nil {
return err
}
return nil
}
func BotFromEnv() *Bot {
return NewBot(os.Getenv("HC_USER"), os.Getenv("HC_PASS"), os.Getenv("HC_AUTHTOKEN"))
}
func EnvRoomName() string {
return os.Getenv("HC_ROOM_NAME")
}
func cerr(err error) {
if err != nil {
fmt.Printf("client error: %s\n", err)
os.Exit(1)
}
}
func main() {
godotenv.Load()
b := BotClient{Bot: BotFromEnv()}
cerr(b.Connect())
b.LoadName()
b.ChatClient.Status("chat")
roomJid, err := b.RoomJabberIdByName(EnvRoomName())
cerr(err)
b.ChatClient.Join(roomJid, b.Bot.FullName)
//TODO: build this into an actions to listen for
for message := range b.ChatClient.Messages() {
if strings.HasPrefix(message.Body, "@"+b.Bot.MentionName) {
b.ChatClient.Say(roomJid, b.Bot.FullName, "Hello")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment