Giter VIP home page Giter VIP logo

rocket.chat.go.sdk's Introduction

Rocket.Chat Go SDK

This is very much a work in process. Things could break as we clean things up. Make sure to use something like dep to lock to a commit to prevent breakage.

Development

A local instance of Rocket.Chat is required for unit tests to confirm connection and subscription methods are functional.

Installing Rocket.Chat

Please see the Development Docs for information on locally deploying a Rocket.Chat instance. Deploying with Docker & Docker Compose is recommended.

Testing

Tests depend on an instance of Rocket.Chat running at http://localhost:3000. This is the default configuration for Rocket.Chat instances deployed with Docker Compose.

To test the rest and realtime packages, navigate to the respective directories and run go test.

rocket.chat.go.sdk's People

Contributors

d-gubert avatar dessolo avatar evanofslack avatar facundomedica avatar geekgonecrazy avatar graywolf336 avatar hackypenguin avatar lawri-van-buel avatar ldruschk avatar miraclesu avatar mriedmann avatar srrathi avatar yukithm avatar zakichammaa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rocket.chat.go.sdk's Issues

Inconsistent method return values throughout SDK

Overview

This issue documents inconsistent and perhaps unideomatic behavior regarding method return values throughout this SDK. Some methods return a resource type while other methods return a higher-level response type. I am proposing that returning the resource type along with an error should be preferable.

I've included some examples of these function signatures below:

Preferable

client.GetMessages has the following signature:

func (c *Client) GetMessages(channel *models.Channel, page *models.Pagination) ([]models.Message, error) 

This is logical, as we expect a message getter function to return a slice of messages and an error.

Unpreferable

Compare this to client.GetPublicChannels with the following signature:

func (c *Client) GetPublicChannels() (*ChannelsResponse, error)

Which returns ChannelsResponse

type ChannelsResponse struct {
	Status
	models.Pagination
	Channels []models.Channel `json:"channels"`
}

Rather than returning a slice of channels, this method returns a response struct with an embedded channels slice. The ChannelsResponse struct needs to exist in order to properly make the HTTP request, but it should not be a public type and it should not be returned from the method. Instead, the channels slice should be returned alongside an error.

There are a few methods currently in the SDK that could be updated to meet this standard (this list is not exshaustive):

Additionally, there are some methods that currently return a response type when it maybe be preferable to return a resource type or even just an error. These are typically cases where resources are being updated or deleted:

Changes like these that modify method signatures are of course breaking changes, so it must be considered whether this is something worth implementing.

Inadequate random ID generation

There is problem with random ID's that are generated for every new message:

func (c *Client) newRandomId() string {
return fmt.Sprintf("%f", rand.Float64())
}

The default precision for %f is 6 which means that it will take about 1178 new messages until there is less than 50% change that all generated random ID:s are unique.

Proposition: Either increase precision of %f in fmt.Sprintf, or implement more robust way to generate random ID:s.

New to Go, but would love to help

My team and I are looking to build some ChatOps integrations with our tools and RocketChat to compliment our SRE implementation.

Although I'm new to GO, I would love to be involved where I can. Do you have a board or a way for me to see where I might be able to help?

I thought I might start with some examples (as I figure them out :) )

realtime messages_test fail

I'm new to Go. The way I would fix the following tests would be:

messageChannel := make(chan models.Message)
err := c.SubscribeToMessageStream(&general, messageChannel)

But it might be better to return a channel (instead of editing the test)?

./messages_test.go:17:22: cannot assign 1 values to 2 variables
./messages_test.go:17:51: not enough arguments in call to c.SubscribeToMessageStream
	have (*models.Channel)
	want (*models.Channel, chan models.Message)
./messages_test.go:52:22: cannot assign 1 values to 2 variables
./messages_test.go:52:51: not enough arguments in call to c.SubscribeToMessageStream
	have (*models.Channel)
	want (*models.Channel, chan models.Message)

Subscription returned a nosub error map[id:3 msg:nosub]

Using the Client.Sub method to subscribe to stream-room-messages for a new room returns this error: Subscription returned a nosub error map[id:3 msg:nosub]. Here's an example:

info, err := client.GetInitialData(token)
if err != nil {
  return err
}

visitor, err := client.CreateGuest(token, fullName, email, department)
if err != nil {
  return err
}

log.Printf("Visitor created: %+v\n", visitor)

deskMessage := fmt.Sprintf("Visitor %s joined the chat:\n**%s**", fullName, message)
err = client.SendMessage(deskMessage, token, roomID)
if err != nil {
  return err
}

// subscribe to room updates
  channel, err := client.Sub("stream-room-messages", roomID)
  if err != nil {
  return fmt.Errorf("failed to subscribe to room updates: %w", err)
}

I am having a hard time determining if this is an issue with the SDK, the Go DDP implementation or a breaking change in RocketChat itself. Any help is appreciated.

Add support for attachments

Hey guys, this SDK does not support receiving messages with attachments. The Message structure contains attachments but only for PostMessage that is used while posting attachments but not receiving. Do you plan on implementing this feature or should I try to do it ?
Thanks

Feature: Modify SendMessage function to take a models.Message parameter instead of a string

Hi!

The function SendMessage(channel *models.Channel, text string) in realtime/messages.go currently takes a string and creates a models.Message variable in the function before sending it. This really limits what we can send to rocket.chat (we need to send attachments with the message for example).

I propose the following:

  • Implement a feature to modify this function so that it takes a models.Message parameter instead of a string.
  • Implement a new function called NewMessage(channel *models.Channel, text string) in realtime/messages.go that returns a basic message with a RoomID, a Msg, and an ID based on the function newRandomId().

Please let me know what you think about each of the above points. I can start working on a PR as soon as you approve these propositions.

Thanks!

SubscribeToMessageStream need help to understand

Hi,

I try to develop a real time light bot for Rocket.
My bot subscribe to each direct room (ie. conversation with other users) with a common channel.

	if channels, e = c.GetChannelsIn(); e != nil {
		log.Panic("can not get channels in " + e.Error())
	}

	msgChannel := make(chan models.Message, 10)
	for _, ch := range channels {
		fmt.Println(fmt.Printf("subscribed to: %v", ch.ID))
		if e = c.SubscribeToMessageStream(&models.Channel{ID: ch.ID}, msgChannel); e != nil {
			log.Println("can not subscribe to message stream for channel " + ch.ID + " " + e.Error())
		}
	}

	count := 0
	for {
		fmt.Println(count)
		select {
		case m := <-msgChannel:
			fmt.Println(fmt.Sprintf("ID: %s RoomID: %s Msg: %s User: %v", m.ID, m.RoomID, m.Msg, m.User))
			count++
		}
	}

When I send a direct message to the bot on the Rocket Chat application the for loop prints exactly [number of subscribed channels] times the log message.
It can not figure out why. I just wanted to wait for messages in the channel and reply to the sender.

Thanks for your help.

Update gopackage/ddp to fix a problem with realtime client close

Hi! First of all, thanks, that's a great project!

In realtime api there's a problem when client closes connection.

	RocketURL, err := url.Parse(c.RocketChatAddress)
	if err != nil {
		log.Println(err)
	}
	RocketClient, err := realtime.NewClient(RocketURL, false)
	if err != nil {
		log.Println(err)
	}
	RocketCreds := RocketModels.UserCredentials{ID: c.RocketXUserID, Token: c.RocketXAuthToken}
	_, err = RocketClient.Login(&RocketCreds)
	if err != nil {
		log.Println(err)
	}
	tmp := make(chan RocketModels.Message, 10)
	err = RocketClient.SubscribeToMyMessages(tmp)
	if err != nil {
		log.Println(err)
	}
	log.Println("Started connection with rocket chat")
	for {
		select {
		case msg := <-tmp:
			log.Println(msg)
			//TODO process msg using regexps, check in db all info, give it to user.
		case <-done:
			RocketClient.Close()
			return
		default:
		}
	}

In my case, i wanted to on receiving syscall sigterm safely close everything. And when i callled client close, i received in logger a problem like this

2023/02/06 16:54:48 Server is starting to accept connections
2023/02/06 16:54:48 About to connect to: wss://localhost:3000/websocket 3000 https
2023/02/06 16:54:48 Started connection with rocket chat
^C2023/02/06 16:55:05 Got SIGINT/SIGTERM, exiting.
2023/02/06 16:55:05 Inbox worker found nil event. May be due to broken websocket. Reconnecting.
exit status 1

When i started researching this undefined behavior, found this gopackage/ddp#13
So, all work needed, is to update a library...

License

Could you please clarify what is the license of your project?

Adding function to get group and channel members

Problem
Function to get group members already exist in https://github.com/RocketChat/Rocket.Chat.Go.SDK/blob/master/rest/group.go#L45 but the problem with this is it's only for fetching group members. We cannot get channel members through as it have different rest endpoint.

Solution
I'm thinking of to make this function a more generalised function to fetch both group and channel members and we can differentiate between them through type of the room and according to the type it will fetch members from the respected endpoint and just return the members.

Discuss
@geekgonecrazy Should I make it a common function as I said to get both group and channel members or made separate functions for both group and channel in their respective file and call them individually while using according to the type of room ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.