Skip to content

ruilisi/Rocket.Chat.Go.SDK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rocket.Chat Go SDK

中文文档
forked from RocketChat/Rocket.Chat.Go.SDK
this version contains mostly apis that rocket-chat support.

struct

models: request struct
conf: get config from application.yml
rest: api function and response struct
test: test

how to use

example

init client
host is your rocket_chat server address

var Client *rest.Client

func InitRcServer(b bool) {
	var _url url.URL
	var host string
	if b {
		host = conf.GetEnv("ROCKETCHAT_URL")
	} else {
		host = conf.GetEnv("ROCKETCHAT_LOCALHOST")
	}

	if host == "localhost:3000" {
		_url = url.URL{
			Host: host,
			//Scheme: "https",
		}
	} else {
		_url = url.URL{
			Host:   host,
			Scheme: "https",
		}
	}
	Client = rest.NewClient(&_url, false)
}

This function is used to login rocket_chat and get userid and token.
Token in rocket_chat valid in 90 days.You can store it in redis.When token used in other apis which needs token,you can take out token directly from redis,it will be quickly.
Request apis from rocket_chat may timeout,use goroutine can set timeout return.Goroutine can also be used in your api server handlers when it need to use this project sdk.

import	"github.com/ruilisi/Rocket.Chat.Go.SDK/rest"

func LoginRc(user, password string) (userid, token string, err error) {
	redisToken := redis.HGetAll("rc:" + user)
	if len(redisToken) == 0 {
		finish := make(chan int)
		ch := make(chan string)
		res := make(chan string, 2)
		var wg sync.WaitGroup
		wg.Add(1)
		go func() {
			login := rest.AuthLoginRequest{
				User:     user,
				Password: password,
			}
			loginres, err := Client.AuthLogin(login)
			if err != nil {
				ch <- err.Error()
				return
			}
			if loginres.Status.Status != "success" {
				ch <- "login fail"
				return
			}
			redis.HSet("rc:"+user, loginres.Data.UserID, loginres.Data.AuthToken)
			redis.Expire("rc:"+user, time.Duration(2000)*time.Hour)
			res <- loginres.Data.UserID
			res <- loginres.Data.AuthToken
			defer wg.Done()
			return
		}()
		go func() {
			wg.Wait()
			finish <- 1
		}()

		select {
		case errmsg := <-ch:
			return "", "", fmt.Errorf("%s", errmsg)
		case <-finish:
			return <-res, <-res, nil
		case <-time.After(3 * time.Second):
			return "", "", fmt.Errorf("%s", "time out")
		}
	}

	for i, j := range redisToken {
		userid = i
		token = j
	}
	return userid, token, nil
}

Unlike origin sdk,this project expose client's token and userid.You can set token and userid directly.

import (
  immodels "github.com/ruilisi/Rocket.Chat.Go.SDK/models"
	"github.com/ruilisi/Rocket.Chat.Go.SDK/rest"
)

userid, token, err := LoginRc(user.Email, rocket_chat.ROCKETCHAT_PASS)
if err != nil {
	c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err})
	return
}
rocket_chat.Client.Auth = &rest.AuthInfo{
	Token: token,
	ID:    userid,
}

createGroup := immodels.CreateGroupRequest{
	Name:    fmt.Sprintf("%s_%s", department.Name, user.Username),
	Members: []string{user.Username, agentName},
}
createGroupRes, _ := Client.CreateGroup(&createGroup)	

set params

params := map[string][]string{
  "count": {"2"},
  "sort":  {"{\"value\":-1}"},
}
res, err := client.GroupList(params)

License

Code and documentation copyright 2020 the Rocket.Chat.Go.SDK Authors and ruilisi Network Code released under the MIT License.

hophacker

Contributors

ExcitingFrog
ExcitingFrog

💻 📖
Soryu23
Soryu23

💻 📖
chioazhao
chioazhao

💻 📖

Releases

No releases published

Languages