RocketChat / Rocket.Chat.Apps-engine

The Rocket.Chat Apps engine and definitions.

Home Page:https://rocketchat.github.io/Rocket.Chat.Apps-engine/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search for a user "self" chat return wrong room

albrosa opened this issue · comments

Developing a slash command, we want to return some messages only to the user sending it.
To get the room we use a code similar to this one:
read.getRoomReader().getDirectByUsernames('user', 'user')
we expect to get the user "self" room, but it return a direct with another user.

Taking a look in the code, the query used is this one:
const query = { t: 'd', usernames: { $size: usernames.length, $all: usernames }, usersCount: usernames.length, };
it will return any room with the user name, and length 2. Any room with the 'user' and another user will match this query.

So, is this a bug?
There is another way to achieve what we want?

This could be seen as a weird behavior, right? A user automatically sending messages to themselves. Not sure, just wondering here.

Anyways, an alternative to this problem would be to use your app's user to communicate with users. So instead of getDirectByUsernames('user', 'user') you'd do getDirectByUsernames('app_user', 'user'). You can query for your app's user using read.getUserReader().getAppUser(app.getID())

In this case, we want the user to receive a message when the user an incorrect message format to a group, but we want to send only to him.

The approach you suggest seems to make sense to our use case.

Since there is an alternative approach, I'll be closing this :)

Greetings!
Now I am faced with the same problem. I need to send a message to the user from the application user.
getDirectByUsernames('app_user', 'user') originally sounded like I wanted.

But the problem is that a personal chat is not created if the user has not yet opened a dialog with the application user
getDirectByUsernames returns undefined because the room doesn't exist yet, which is plausible)
But I would like to behave like with webhook integration, when a personal chat is created by itself, if it does not exist yet.

Or maybe you know how you can start a chat with a user on behalf of the application manually if getDirectByUsernames returns undefined

This would be a great addition indeed. But if it is the case that getDirectByUsernames doesn't return true, you can always create the new room.

Check out this example https://github.com/marceloschmidt/koko/blob/master/lib/helpers.ts#L33