1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00
golang library for rocket.chat
Go to file
2022-10-25 11:58:16 +03:00
.github/workflows Create close-issues-and-pr.yml 2022-10-04 23:29:42 +03:00
.gitignore init commit 2020-04-30 17:36:31 +03:00
authentication.go update struct 2022-09-01 16:22:58 +03:00
autotranslate.go add auth, translate. edit new client. 2020-06-20 18:14:33 +03:00
CHANGELOG.md changelog update 2022-10-05 19:32:43 +03:00
channels.go update struct 2022-09-01 16:22:58 +03:00
chat.go review 2022-09-27 10:57:03 +03:00
go.mod add paginations 2022-10-02 15:48:22 +03:00
gorocket.go Fixed method addQueryParams. Implemented correct addition of paganization parameters 2022-10-25 11:58:16 +03:00
groups.go fix struct, add leader and owner rest 2022-09-08 12:21:06 +03:00
hooks.go Do not exit the program on failed API Calls, return an error instead 2022-03-07 13:19:29 +01:00
info.go update struct 2022-09-01 16:22:58 +03:00
LICENSE add license 2020-09-08 23:21:44 +03:00
README.md add pagination example to readme 2022-10-02 15:53:01 +03:00
users.go fix struct, add leader and owner rest 2022-09-08 12:21:06 +03:00

Golang Rocket Chat REST API client

Use this simple client if you need to connect to Rocket Chat in Golang.

How to use

Just import

import (
	"github.com/badkaktus/gorocket"
)

Create client

client := gorocket.NewClient("https://your-rocket-chat.com")

// login as the main admin user
login := gorocket.LoginPayload{
    User:     "admin-login",
    Password: "admin-password",
}

lg, err := client.Login(&login)

if err != nil {
    fmt.Printf("Error: %+v", err)
}
fmt.Printf("I'm %s", lg.Data.Me.Username)

or

client := gorocket.NewWithOptions("https://your-rocket-chat.com", 
    gorocket.WithUserID("my-user-id"),
    gorocket.WithToken("my-bot-token"),
    gorocket.WithTimeout(1 * time.Second),
)

Manage user

str := gorocket.NewUser{
    Email:                 "test@email.com",
    Name:                  "John Doe",
    Password:              "simplepassword",
    Username:              "johndoe",
    Active:                true,
}

me, err := client.UsersCreate(&str)
if err != nil {
    fmt.Printf("Error: %+v", err)
}
fmt.Printf("User was created %t", me.Success)

Post a message

// create a new channel
str := gorocket.CreateChannelRequest{
    Name:     "newchannel",
}

channel, err := client.CreateChannel(&str)
if err != nil {
    fmt.Printf("Error: %+v", err)
}
fmt.Printf("Channel was created %t", channel.Success)
// post a message
str := gorocket.Message{
    Channel:     "somechannel",
    Text:        "Hey! This is new message from Golang REST Client",
}

msg, err := client.PostMessage(&str)
if err != nil {
    fmt.Printf("Error: %+v", err)
}
fmt.Printf("Message was posted %t", msg.Success)

Pagination

If endpoint support pagination, you can use that like this:

// sort field in map. 1 - asc, -1 - desc
srt := map[string]int{"_updatedAt": 1, "name": -1}

client.Count(10).Offset(10).Sort(srt).ChannelList()

PS

Feel free to create issue for add new endpoint to this client