Accessing sender's custom fields inside ISlashCommand's executor

I have a simple text-type custom field I would like to access from inside a slash command.
The field is defined in Rocket.Chat server’s settings:

{
	"Extension": {
		"type": "text",
		"required": false,
		"public": true,
        "private": false,
		"sendToIntegrations": true
	
	}
}

Custom fields’ values should be accessible (I think) via the IUser.customFields? optional dictionary, but it seems it is not filled with anything.

public async executor(context: SlashCommandContext, read: IRead, modify: IModify, http: IHttp): Promise<void> {
        console.log(context.getSender());
        console.log(context.getSender().customFields?.['Extension']);
}

produces

rocketchat_1          | {
rocketchat_1          |   id: '3Gg7KDwQiGeY9Yp2w',
rocketchat_1          |   username: 'davide',
rocketchat_1          |   emails: [ { address: 'admin@example.com', verified: false } ],
rocketchat_1          |   type: 'user',
rocketchat_1          |   isEnabled: true,
rocketchat_1          |   name: 'Davide',
rocketchat_1          |   roles: [ 'admin' ],
rocketchat_1          |   status: 'online',
rocketchat_1          |   statusConnection: 'online',
rocketchat_1          |   utcOffset: 1,
rocketchat_1          |   createdAt: 2020-12-07T11:05:51.516Z,
rocketchat_1          |   updatedAt: 2020-12-15T10:10:26.918Z,
rocketchat_1          |   lastLoginAt: 2020-12-15T09:51:42.335Z,
rocketchat_1          |   appId: undefined
rocketchat_1          | }
rocketchat_1          | undefined

where no attribute customFields is present.
Is this the right way to access custom fields?
I am using the rocketchat/rocket.chat:latest Docker image to deploy the slash command.

Hi, did you manage to fix the problem?

Hi, thanks for reminding me of this.
I ended up not needing this any more.
However, you can access it through this REST call:
https://docs.rocket.chat/api/rest-api/methods/users/info
If the custom field Extension exists, the returned object will contain the field

{
    "user":
        //...
        "customFields": {
             "Extension": "blah"
        }
}

This call requires authentication, so you need to provide an authentication token in the headers of your request. Not sure if needed inside an app, but probably yes.
This call returns the token provided you have username and password of a registered user:
https://docs.rocket.chat/api/rest-api/methods/authentication/login

To make a REST call from a slashcommand, check
https://docs.rocket.chat/apps-development/recipes/making-http-requests

Not sure how to add HTTP headers to a GET request, couldn’t find the docs right on the spot.
I will update this answer as soon as I have some time to test some working example.

1 Like

Custom fields in applications are very useful. I was disappointed that there is no explicit way to get them.
I’m currently looking for workarounds in the process.
Have you made progress with this problem?

The room object has a field, but the user object does not. This looks like a bug.
image

I think this will be fixed by [FIX] Add missing custom fields to apps' users converter by cuonghuunguyen · Pull Request #21176 · RocketChat/Rocket.Chat · GitHub