func init() { var err error client, err = hipchat.NewClient(username, password, resource) if err != nil { log.Fatal(err.Error()) } }
func doHipChat(username, roomjid string) { if strings.HasSuffix(username, "@chat.hipchat.com") { username = strings.Replace(username, "@chat.hipchat.com", "", 1) } if !strings.HasSuffix(roomjid, "@conf.hipchat.com") { roomjid += "@conf.hipchat.com" } pass, err := getpass("Password for user \"" + username + "\": ") if err != nil { log.Print(err) } client, err := hipchat.NewClient(username, pass, "bot") if err != nil { log.Print(err) os.Exit(1) } nick, mname := getUserInfo(client, username) client.Status("chat") client.Join(roomjid, nick) log.Printf("[%s] now serving room [%s]", nick, roomjid) log.Print("hit ^C to exit") go client.KeepAlive() for message := range client.Messages() { if strings.HasPrefix(message.Body, "@"+mname) { go client.Say(roomjid, nick, process(message.Body)) } } }
func main() { user := "******" pass := "******" resource := "bot" client, err := hipchat.NewClient(user, pass, resource) if err != nil { fmt.Printf("client error: %s\n", err) return } var fullName string var mentionName string for _, user := range client.Users() { if user.Id == client.Id { fullName = user.Name mentionName = user.MentionName break } } fmt.Printf("name: %s\n", fullName) fmt.Printf("mention: %s\n", mentionName) }
func main() { user := "******" pass := "******" resource := "bot" roomJid := "*****@*****.**" fullName := "Some Bot" mentionName := "SomeBot" client, err := hipchat.NewClient(user, pass, resource) if err != nil { fmt.Printf("client error: %s\n", err) return } client.Status("chat") client.Join(roomJid, fullName) go func() { for { select { case message := <-client.Messages(): if strings.HasPrefix(message.Body, "@"+mentionName) { client.Say(roomJid, fullName, "Hello") } } } }() select {} }
// Connect is the entry point for the robot. This will dial in the hipchat client with the provided credentials and // start a keepAlive loop to prevent logouts. // Upon connecting the bot will join all available rooms and gather all the user info from the server. func (robot *Robot) Connect(jabberID string, password string) error { client, err := hipchat.NewClient(jabberID, password, "gobot") robot.client = client client.Status("chat") robot.JoinAllAvailableRooms() log.Printf("Connected to all available rooms") robot.initialLoaded = false robot.UserList = make(map[string]*hipchat.User) go robot.CollectUserObjects() go client.KeepAlive() return err }
func NewBot(user, pass string) (*Bot, error) { c, err := hipchat.NewClient(user, pass, resource) if err != nil { return nil, err } c.Status("chat") log.Println("Connected and available") b := &Bot{ client: c, } return b, b.init() }
func main() { user := "******" pass := "******" resource := "bot" roomJid := "*****@*****.**" fullName := "Some Bot" client, err := hipchat.NewClient(user, pass, resource) if err != nil { fmt.Printf("client error: %s\n", err) return } client.Status("chat") client.Join(roomJid, fullName) client.Say(roomJid, fullName, "Hello") select {} }
func newHipchatChatClient(L *lua.LState, co *CommonClientOption, opt *lua.LTable) { user, uok := getStringField(L, opt, "user") password, pok := getStringField(L, opt, "password") if !uok || !pok { L.RaiseError("'user' and 'password' are required") } host, _ := getStringField(L, opt, "host") if len(host) == 0 { host = "chat.hipchat.com" } conf, _ := getStringField(L, opt, "conf") if len(conf) == 0 { conf = "conf.hipchat.com" } resource, _ := getStringField(L, opt, "resource") if len(resource) == 0 { resource = "bot" } roomJids := L.GetField(opt, "room_jids") if co.Logger == nil { co.Logger = log.New(os.Stdout, "", log.Lshortfile|log.LstdFlags) } // hipchatobj, err := hipchat.NewClientWithServerInfo(user, password, resource, host, conf) hipchatobj, err := hipchat.NewClient(user, password, resource) if err != nil { co.Logger.Printf("[ERROR] %s", err.Error()) os.Exit(1) } co.Logger.Printf("[INFO] connected to %s", host) chatClient := &hipchatChatClient{hipchatobj, co, co.Logger, make(map[string][]*lua.LFunction), []string{}, "", ""} if tbl, ok := roomJids.(*lua.LTable); ok { tbl.ForEach(func(key, value lua.LValue) { chatClient.roomsJids = append(chatClient.roomsJids, value.String()) }) } L.Push(newChatClient(L, hipchatChatClientTypeName, chatClient, luar.New(L, chatClient.hipchatobj).(*lua.LUserData))) }
func main() { user := "******" pass := "******" resource := "bot" client, err := hipchat.NewClient(user, pass, resource) if err != nil { fmt.Printf("client error: %s\n", err) return } client.RequestUsers() select { case users := <-client.Users(): for _, user := range users { if user.Id == client.Id { fmt.Printf("name: %s\n", user.Name) fmt.Printf("mention: %s\n", user.MentionName) } } } }
func (a *adapter) startConnection() error { client, err := hipchat.NewClient(a.user, a.password, a.resource) if err != nil { hal.Logger.Error(err.Error()) return err } client.Status("chat") for _, user := range client.Users() { // retrieve the name and mention name of our bot from the server if user.Id == client.Id { a.name = user.Name a.nick = user.MentionName // skip adding the bot to the users map continue } // Initialize a newUser object in case we need it. newUser := hal.User{ ID: user.Id, Name: user.Name, Options: map[string]interface{}{ "mentionName": user.MentionName, }, } // Prepopulate our users map because we can easily do so. // If a user doesn't exist, set it. u, err := a.Robot.Users.Get(user.Id) if err != nil { a.Robot.Users.Set(user.Id, newUser) } // If the user doesn't match completely (say, if someone changes their name), // then adjust what we have stored. if u.Name != user.Name || mentionName(&u) != user.MentionName { a.Robot.Users.Set(user.Id, newUser) } } // Make a map of room JIDs to human names roomJids := make(map[string]string, len(client.Rooms())) for _, room := range client.Rooms() { roomJids[room.Name] = room.Id } client.Status("chat") // Only join the rooms we want for _, room := range a.rooms { hal.Logger.Debugf("%s - joined %s", a, room) client.Join(roomJids[room], a.name) } a.client = client a.Robot.Alias = a.nick // send an empty string every 60 seconds so hipchat doesn't disconnect us go client.KeepAlive() for message := range client.Messages() { from := strings.Split(message.From, "/") // ignore messages directly from the channel // TODO: don't do this :) if len(from) < 2 { continue } // ingore messages from our bot if from[1] == a.name { continue } msg := a.newMessage(message) a.Receive(msg) } return nil }
func main() { if len(os.Args) != 3 { fmt.Printf( `rtop-bot %s - (c) 2015 RapidLoop - http://www.rtop-monitor.org/rtop-bot rtop-bot is a HipChat bot that can do remote system monitoring over SSH Usage: rtop-bot userJid roomJid where userJid is the HipChat user jabber ID, like 139999_999914 roomJid is the HipChat room jabber ID, like 139999_opschat `, VERSION) os.Exit(1) } log.SetPrefix("rtop-bot: ") log.SetFlags(0) username := os.Args[1] roomjid := os.Args[2] if strings.HasSuffix(username, "@chat.hipchat.com") { username = strings.Replace(username, "@chat.hipchat.com", "", 1) } if !strings.HasSuffix(roomjid, "@conf.hipchat.com") { roomjid += "@conf.hipchat.com" } pass, err := getpass("Password for user \"" + username + "\": ") if err != nil { log.Print(err) } // get default username for SSH connections usr, err := user.Current() if err != nil { log.Print(err) os.Exit(1) } sshUsername = usr.Username // expand ~/.ssh/id_rsa and check if it exists idRsaPath = filepath.Join(usr.HomeDir, ".ssh", "id_rsa") if _, err := os.Stat(idRsaPath); os.IsNotExist(err) { idRsaPath = "" } // expand ~/.ssh/config and parse if it exists sshConfig := filepath.Join(usr.HomeDir, ".ssh", "config") if _, err := os.Stat(sshConfig); err == nil { parseSshConfig(sshConfig) } client, err := hipchat.NewClient(username, pass, "bot") if err != nil { log.Print(err) os.Exit(1) } nick, mname := getUserInfo(client, username) client.Status("chat") client.Join(roomjid, nick) log.Printf("[%s] now serving room [%s]", nick, roomjid) log.Print("hit ^C to exit") go client.KeepAlive() for message := range client.Messages() { if strings.HasPrefix(message.Body, "@"+mname) { go client.Say(roomjid, nick, process(message.Body)) } } }