Commit 0e1eb1ec authored by Tim Kinnane's avatar Tim Kinnane
Browse files

feat(api.users): Add helper queries to API

- Add `bot-helpers` user queries as `api.users` methods
- Related #5
- Add demo interactions in `start.ts`
parent b2ac5be0
[asteroid]: https://www.npmjs.com/package/asteroid
[lru]: https://www.npmjs.com/package/lru
[rest]: https://rocket.chat/docs/developer-guides/rest-api/
[start]: https://github.com/RocketChat/Rocket.Chat.js.SDK/blob/master/src/utils/start.ts
# Rocket.Chat Node.js SDK
......@@ -95,6 +96,18 @@ node easybot.js
_TBD: insert screenshot of bot working on a server_
### Demo
There's a simple listener script provided to demonstrate functionality locally.
[See the source here][start] and/or run it with `yarn start`.
The start script will log to console any message events that appear in its
stream. It will respond to a couple specific commands demonstrating usage of
the API helpers. Try messaging the bot directly one of the following:
- `tell everyone <something>` - It will send that "something" to everyone
- `who's online` - It will tell you who's online
## Overview
Using this package third party apps can control and query a Rocket.Chat server
......@@ -120,7 +133,7 @@ Below is just a summary:
---
Currently, there are two modules exported by the SDK:
The following modules are exported by the SDK:
- `driver` - Handles connection, method calls, room subscriptions (via Asteroid)
- `methodCache` - Manages results cache for calls to server (via LRU cache)
- `api` - Provides a client for making requests with Rocket.Chat's REST API
......@@ -144,7 +157,11 @@ more advanced methods that can be called from the `driver.asteroid` interface.
Rocket.Chat REST API calls can be made via `api.get` or `api.post`, with
parameters defining the endpoint, payload and if authorization is required
(respectively). See the
(respectively). See the [REST API docs][rest] for details.
Some common requests for user queries are made available as simple helpers under
`api.users`, such as `api.users.onlineIDs()` which returns the user IDs of all
online users. Run `ts-node src/utils/users.ts` for a demo of user query outputs.
## MESSAGE OBJECTS
......@@ -409,6 +426,8 @@ called, it will attempt to login first and keep the response token for later.
Bots and apps should manually call the API `.logout` method on shutdown if they
have used the API.
---
### `api.loggedIn()`
Returns boolean status of existing login
......@@ -447,6 +466,44 @@ Exported property with details of the current API session
- `.userId` - The logged in user's ID
- `.authToken` - The current auth token
### `api.userFields`
Exported property for user query helper default fields
- Defaults to `{ name: 1, username: 1, status: 1, type: 1 }`
- See https://rocket.chat/docs/developer-guides/rest-api/query-and-fields-info/
### `api.users.all([fields])`
Helper for querying all users
- Optional fields object (see fields docs link above)
- Returns promise, resolves with array of user objects
### `api.users.allNames()`
Helper for querying all usernames
- Returns promise, resolves with array of usernames
### `api.users.allIDs()`
Helper for querying all user IDs
- Returns promise, resolves with array of IDs
### `api.users.online([fields])`
Helper for querying online users
- Optional fields object (see fields docs link above)
- Returns promise, resolves with array of user objects
### `api.users.onlineNames()`
Helper for querying online usernames
- Returns promise, resolves with array of usernames
### `api.users.onlineIDs()`
Helper for querying online user IDs
- Returns promise, resolves with array of IDs
---
## Development
......
TN:
SF:/Volumes/x/code/rocketchat/Rocket.Chat.js.SDK/src/lib/api.ts
FN:24,loggedIn
FN:47,getQueryString
FN:49,(anonymous_8)
FN:62,setAuth
FN:68,getHeaders
FN:81,clearHeaders
FN:87,success
FN:109,post
FN:119,(anonymous_15)
FN:120,(anonymous_16)
FN:124,(anonymous_17)
FN:141,get
FN:152,(anonymous_20)
FN:153,(anonymous_21)
FN:157,(anonymous_22)
FN:170,login
FN:197,logout
FN:203,(anonymous_26)
FNF:18
FN:25,loggedIn
FN:48,getQueryString
FN:50,(anonymous_8)
FN:63,setAuth
FN:69,getHeaders
FN:82,clearHeaders
FN:88,success
FN:110,post
FN:120,(anonymous_15)
FN:121,(anonymous_16)
FN:125,(anonymous_17)
FN:142,get
FN:153,(anonymous_20)
FN:154,(anonymous_21)
FN:158,(anonymous_22)
FN:171,login
FN:198,logout
FN:204,(anonymous_26)
FN:215,(anonymous_27)
FN:215,(anonymous_28)
FN:216,(anonymous_29)
FN:216,(anonymous_30)
FN:216,(anonymous_31)
FN:217,(anonymous_32)
FN:217,(anonymous_33)
FN:217,(anonymous_34)
FN:218,(anonymous_35)
FN:218,(anonymous_36)
FN:219,(anonymous_37)
FN:219,(anonymous_38)
FN:219,(anonymous_39)
FN:220,(anonymous_40)
FN:220,(anonymous_41)
FN:220,(anonymous_42)
FNF:34
FNH:16
FNDA:31,loggedIn
FNDA:22,getQueryString
FNDA:32,loggedIn
FNDA:23,getQueryString
FNDA:14,(anonymous_8)
FNDA:8,setAuth
FNDA:43,getHeaders
FNDA:10,clearHeaders
FNDA:45,success
FNDA:44,getHeaders
FNDA:11,clearHeaders
FNDA:46,success
FNDA:21,post
FNDA:21,(anonymous_15)
FNDA:21,(anonymous_16)
FNDA:0,(anonymous_17)
FNDA:19,get
FNDA:19,(anonymous_20)
FNDA:19,(anonymous_21)
FNDA:20,get
FNDA:20,(anonymous_20)
FNDA:20,(anonymous_21)
FNDA:0,(anonymous_22)
FNDA:20,login
FNDA:20,logout
FNDA:7,(anonymous_26)
FNDA:21,logout
FNDA:8,(anonymous_26)
FNDA:0,(anonymous_27)
FNDA:0,(anonymous_28)
FNDA:0,(anonymous_29)
FNDA:0,(anonymous_30)
FNDA:0,(anonymous_31)
FNDA:0,(anonymous_32)
FNDA:0,(anonymous_33)
FNDA:0,(anonymous_34)
FNDA:0,(anonymous_35)
FNDA:0,(anonymous_36)
FNDA:0,(anonymous_37)
FNDA:0,(anonymous_38)
FNDA:0,(anonymous_39)
FNDA:0,(anonymous_40)
FNDA:0,(anonymous_41)
FNDA:0,(anonymous_42)
DA:1,1
DA:2,1
DA:3,1
DA:16,1
DA:24,1
DA:25,31
DA:29,1
DA:17,1
DA:25,1
DA:26,32
DA:30,1
DA:33,1
DA:42,1
DA:47,1
DA:48,22
DA:49,13
DA:50,14
DA:53,14
DA:58,1
DA:31,1
DA:34,1
DA:43,1
DA:48,1
DA:49,23
DA:50,13
DA:51,14
DA:54,14
DA:59,1
DA:62,1
DA:63,8
DA:60,1
DA:63,1
DA:64,8
DA:68,1
DA:69,43
DA:70,33
DA:75,1
DA:77,32
DA:81,1
DA:82,10
DA:83,10
DA:87,1
DA:88,45
DA:109,1
DA:115,21
DA:65,8
DA:69,1
DA:70,44
DA:71,34
DA:76,1
DA:78,33
DA:82,1
DA:83,11
DA:84,11
DA:88,1
DA:89,46
DA:110,1
DA:116,21
DA:117,21
DA:118,21
......@@ -78,116 +109,127 @@ DA:120,21
DA:121,21
DA:122,21
DA:123,21
DA:124,0
DA:126,21
DA:124,21
DA:125,0
DA:127,21
DA:129,0
DA:128,21
DA:130,0
DA:141,1
DA:147,19
DA:148,19
DA:149,19
DA:150,19
DA:151,19
DA:152,19
DA:153,19
DA:154,19
DA:155,19
DA:156,19
DA:157,0
DA:159,19
DA:160,19
DA:162,0
DA:170,1
DA:171,20
DA:131,0
DA:142,1
DA:148,20
DA:149,20
DA:150,20
DA:151,20
DA:152,20
DA:153,20
DA:154,20
DA:155,20
DA:156,20
DA:157,20
DA:158,0
DA:160,20
DA:161,20
DA:163,0
DA:171,1
DA:172,20
DA:173,13
DA:173,20
DA:174,13
DA:175,12
DA:177,1
DA:180,8
DA:175,13
DA:176,12
DA:178,1
DA:181,8
DA:182,8
DA:188,8
DA:183,8
DA:189,8
DA:190,8
DA:192,0
DA:197,1
DA:198,20
DA:199,13
DA:191,8
DA:193,0
DA:198,1
DA:199,21
DA:200,13
DA:202,7
DA:203,7
DA:204,7
DA:205,7
LF:82
LH:76
BRDA:30,0,0,1
BRDA:30,0,1,0
BRDA:34,1,0,1
BRDA:34,1,1,0
BRDA:35,2,0,1
BRDA:35,2,1,0
BRDA:43,3,0,1
BRDA:43,3,1,0
BRDA:48,4,0,9
BRDA:48,4,1,13
BRDA:48,5,0,22
BRDA:48,5,1,15
BRDA:48,5,2,15
BRDA:51,6,0,2
BRDA:51,6,1,12
BRDA:68,7,0,1
BRDA:69,8,0,10
BRDA:69,8,1,33
BRDA:70,9,0,1
BRDA:70,9,1,32
BRDA:71,10,0,33
BRDA:71,10,1,32
BRDA:71,10,2,32
BRDA:71,10,3,32
BRDA:97,11,0,43
BRDA:97,11,1,2
BRDA:90,12,0,45
BRDA:90,12,1,45
BRDA:90,12,2,28
BRDA:90,12,3,44
BRDA:90,12,4,17
BRDA:90,12,5,28
BRDA:90,12,6,26
BRDA:90,12,7,2
BRDA:90,12,8,0
BRDA:90,12,9,0
BRDA:112,13,0,0
BRDA:117,14,0,0
BRDA:117,14,1,21
BRDA:117,15,0,21
BRDA:117,15,1,13
BRDA:121,16,0,0
BRDA:121,16,1,21
BRDA:122,17,0,0
BRDA:122,17,1,21
BRDA:144,18,0,0
BRDA:149,19,0,1
BRDA:149,19,1,18
BRDA:149,20,0,19
BRDA:149,20,1,18
BRDA:154,21,0,0
BRDA:154,21,1,19
BRDA:155,22,0,0
BRDA:155,22,1,19
BRDA:170,23,0,6
BRDA:172,24,0,13
BRDA:172,24,1,7
BRDA:174,25,0,12
BRDA:174,25,1,1
BRDA:181,26,0,8
BRDA:181,26,1,0
BRDA:181,27,0,8
BRDA:181,27,1,8
BRDA:198,28,0,13
BRDA:198,28,1,7
BRF:65
DA:201,13
DA:203,8
DA:204,8
DA:205,8
DA:206,8
DA:211,1
DA:214,1
DA:215,0
DA:216,0
DA:217,0
DA:218,0
DA:219,0
DA:220,0
LF:90
LH:78
BRDA:31,0,0,1
BRDA:31,0,1,0
BRDA:35,1,0,1
BRDA:35,1,1,0
BRDA:36,2,0,1
BRDA:36,2,1,0
BRDA:44,3,0,1
BRDA:44,3,1,0
BRDA:49,4,0,10
BRDA:49,4,1,13
BRDA:49,5,0,23
BRDA:49,5,1,15
BRDA:49,5,2,15
BRDA:52,6,0,2
BRDA:52,6,1,12
BRDA:69,7,0,1
BRDA:70,8,0,10
BRDA:70,8,1,34
BRDA:71,9,0,1
BRDA:71,9,1,33
BRDA:72,10,0,34
BRDA:72,10,1,33
BRDA:72,10,2,33
BRDA:72,10,3,33
BRDA:98,11,0,44
BRDA:98,11,1,2
BRDA:91,12,0,46
BRDA:91,12,1,46
BRDA:91,12,2,28
BRDA:91,12,3,45
BRDA:91,12,4,18
BRDA:91,12,5,28
BRDA:91,12,6,26
BRDA:91,12,7,2
BRDA:91,12,8,0
BRDA:91,12,9,0
BRDA:113,13,0,0
BRDA:118,14,0,0
BRDA:118,14,1,21
BRDA:118,15,0,21
BRDA:118,15,1,13
BRDA:122,16,0,0
BRDA:122,16,1,21
BRDA:123,17,0,0
BRDA:123,17,1,21
BRDA:145,18,0,0
BRDA:150,19,0,1
BRDA:150,19,1,19
BRDA:150,20,0,20
BRDA:150,20,1,19
BRDA:155,21,0,0
BRDA:155,21,1,20
BRDA:156,22,0,0
BRDA:156,22,1,20
BRDA:171,23,0,6
BRDA:173,24,0,13
BRDA:173,24,1,7
BRDA:175,25,0,12
BRDA:175,25,1,1
BRDA:182,26,0,8
BRDA:182,26,1,0
BRDA:182,27,0,8
BRDA:182,27,1,8
BRDA:199,28,0,13
BRDA:199,28,1,8
BRDA:215,29,0,0
BRDA:218,30,0,0
BRF:67
BRH:51
end_of_record
TN:
......@@ -622,8 +664,8 @@ FNDA:0,(anonymous_3)
FNDA:0,(anonymous_4)
FNDA:2,replaceLog
FNDA:2,silence
FNDA:210,(anonymous_7)
FNDA:207,(anonymous_8)
FNDA:212,(anonymous_7)
FNDA:208,(anonymous_8)
FNDA:0,(anonymous_9)
FNDA:0,(anonymous_10)
FNDA:0,(anonymous_11)
......@@ -635,8 +677,8 @@ DA:18,0
DA:22,1
DA:25,2
DA:29,2
DA:30,210
DA:31,207
DA:30,212
DA:31,208
DA:32,0
DA:33,0
DA:34,0
......
......@@ -24,6 +24,11 @@ export interface IMessage {
_id: string;
username: string;
};
u?: {
_id: string;
username: string;
name: string;
};
}
export interface IAttachment {
fields: IAttachmentField[];
......
{"version":3,"file":"messageInterfaces.js","sourceRoot":"","sources":["../../src/config/messageInterfaces.ts"],"names":[],"mappings":";AAAA,8DAA8D","sourcesContent":["/** @todo contribute these to @types/rocketchat and require */\n\nexport interface IMessage {\n rid: string | null, // room ID\n _id?: string, // generated by Random.id()\n t?: string, // type, e.g. rm\n msg?: string, // text content\n alias?: string, // ??\n emoji?: string, // emoji code\n avatar?: string, // url\n groupable?: boolean, // ?\n bot?: any, // integration details\n urls?: string[], // ?\n mentions?: string[], // ?\n reactions?: { [emoji: string]: string[] } // emoji: [usernames]\n location?: {\n type: string, // e.g. Point\n coordinates: string[] // longitude, latitude\n },\n attachments?: IAttachment[],\n editedAt?: Date,\n editedBy?: {\n _id: string,\n username: string\n }\n}\n\nexport interface IAttachment {\n fields: IAttachmentField[],\n color?: string,\n text?: string,\n ts?: string,\n thumb_url?: string,\n message_link?: string,\n collapsed?: boolean,\n author_name?: string,\n author_link?: string,\n author_icon?: string,\n title?: string,\n title_link?: string,\n title_link_download?: string,\n image_url?: string,\n audio_url?: string,\n video_url?: string\n}\n\nexport interface IAttachmentField {\n short?: boolean,\n title: 'string',\n value: 'string'\n}\n"]}
\ No newline at end of file
{"version":3,"file":"messageInterfaces.js","sourceRoot":"","sources":["../../src/config/messageInterfaces.ts"],"names":[],"mappings":";AAAA,8DAA8D","sourcesContent":["/** @todo contribute these to @types/rocketchat and require */\n\nexport interface IMessage {\n rid: string | null // room ID\n _id?: string // generated by Random.id()\n t?: string // type e.g. rm\n msg?: string // text content\n alias?: string // ??\n emoji?: string // emoji code\n avatar?: string // url\n groupable?: boolean // ?\n bot?: any // integration details\n urls?: string[] // ?\n mentions?: string[] // ?\n reactions?: { [emoji: string]: string[] } // emoji: [usernames]\n location?: {\n type: string // e.g. Point\n coordinates: string[] // longitude latitude\n }\n attachments?: IAttachment[]\n editedAt?: Date\n editedBy?: {\n _id: string\n username: string\n }\n u?: { // the user who sent the message\n _id: string\n username: string\n name: string\n }\n}\n\nexport interface IAttachment {\n fields: IAttachmentField[]\n color?: string\n text?: string\n ts?: string\n thumb_url?: string\n message_link?: string\n collapsed?: boolean\n author_name?: string\n author_link?: string\n author_icon?: string\n title?: string\n title_link?: string\n title_link_download?: string\n image_url?: string\n audio_url?: string\n video_url?: string\n}\n\nexport interface IAttachmentField {\n short?: boolean\n title: 'string'\n value: 'string'\n}\n"]}
\ No newline at end of file
......@@ -77,3 +77,12 @@ export declare function get(endpoint: string, data?: any, auth?: boolean, ignore
export declare function login(user?: ILoginCredentials): Promise<ILoginResultAPI>;
/** Logout a user at end of API calls */
export declare function logout(): Promise<void>;
/** Defaults for user queries */
export declare const userFields: {
name: number;
username: number;
status: number;
type: number;
};
/** Query helpers for user collection requests */
export declare const users: any;
......@@ -198,4 +198,15 @@ function logout() {
});
}
exports.logout = logout;
/** Defaults for user queries */
exports.userFields = { name: 1, username: 1, status: 1, type: 1 };
/** Query helpers for user collection requests */
exports.users = {
all: (fields = exports.userFields) => get('users.list', { fields }).then((r) => r.users),
allNames: () => get('users.list', { fields: { 'username': 1 } }).then((r) => r.users.map((u) => u.username)),
allIDs: () => get('users.list', { fields: { '_id': 1 } }).then((r) => r.users.map((u) => u._id)),
online: (fields = exports.userFields) => get('users.list', { fields, query: { 'status': { $ne: 'offline' } } }).then((r) => r.users),
onlineNames: () => get('users.list', { fields: { 'username': 1 }, query: { 'status': { $ne: 'offline' } } }).then((r) => r.users.map((u) => u.username)),
onlineIds: () => get('users.list', { fields: { '_id': 1 }, query: { 'status': { $ne: 'offline' } } }).then((r) => r.users.map((u) => u._id))
};
//# sourceMappingURL=api.js.map
\ No newline at end of file
This diff is collapsed.
......@@ -2,4 +2,3 @@ import { INewUserAPI } from './interfaces';
export declare const apiUser: INewUserAPI;
export declare const botUser: INewUserAPI;
export declare const mockUser: INewUserAPI;
export declare const botRooms: string[];
......@@ -31,6 +31,4 @@ exports.mockUser = {
sendWelcomeEmail: false,
verified: true
};
// Names for roomId lookup and join for test interactions
exports.botRooms = ['general'];
//# sourceMappingURL=config.js.map
\ No newline at end of file
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":";;AAEA,sEAAsE;AACzD,QAAA,OAAO,GAAgB;IAClC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,OAAO;IAC/C,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,MAAM;CAC3C,CAAA;AAED,+DAA+D;AAClD,QAAA,OAAO,GAAgB;IAClC,KAAK,EAAE,eAAe;IACtB,IAAI,EAAE,KAAK;IACX,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,MAAM;IACnD,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,KAAK;IAC9C,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,CAAC,KAAK,CAAC;IACd,mBAAmB,EAAE,IAAI;IACzB,qBAAqB,EAAE,KAAK;IAC5B,gBAAgB,EAAE,KAAK;IACvB,QAAQ,EAAE,IAAI;CACf,CAAA;AAED,sEAAsE;AACzD,QAAA,QAAQ,GAAgB;IACnC,KAAK,EAAE,gBAAgB;IACvB,IAAI,EAAE,WAAW;IACjB,QAAQ,EAAE,MAAM;IAChB,QAAQ,EAAE,MAAM;IAChB,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,CAAC,MAAM,CAAC;IACf,mBAAmB,EAAE,IAAI;IACzB,qBAAqB,EAAE,KAAK;IAC5B,gBAAgB,EAAE,KAAK;IACvB,QAAQ,EAAE,IAAI;CACf,CAAA;AAED,yDAAyD;AAC5C,QAAA,QAAQ,GAAG,CAAC,SAAS,CAAC,CAAA","sourcesContent":["import { INewUserAPI } from './interfaces'\n\n// The API user, should be provisioned on build with local Rocket.Chat\nexport const apiUser: INewUserAPI = {\n username: process.env.ADMIN_USERNAME || 'admin',\n password: process.env.ADMIN_PASS || 'pass'\n}\n\n// The Bot user, will attempt to login and run methods in tests\nexport const botUser: INewUserAPI = {\n email: 'bot@localhost',\n name: 'Bot',\n password: process.env.ROCKETCHAT_PASSWORD || 'pass',\n username: process.env.ROCKETCHAT_USER || 'bot',\n active: true,\n roles: ['bot'],\n joinDefaultChannels: true,\n requirePasswordChange: false,\n sendWelcomeEmail: false,\n verified: true\n}\n\n// The Mock user, will send messages via API for the bot to respond to\nexport const mockUser: INewUserAPI = {\n email: 'mock@localhost',\n name: 'Mock User',\n password: 'mock',\n username: 'mock',\n active: true,\n roles: ['user'],\n joinDefaultChannels: true,\n requirePasswordChange: false,\n sendWelcomeEmail: false,\n verified: true\n}\n\n// Names for roomId lookup and join for test interactions\nexport const botRooms = ['general']\n"]}
\ No newline at end of file
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":";;AAEA,sEAAsE;AACzD,QAAA,OAAO,GAAgB;IAClC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,OAAO;IAC/C,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,MAAM;CAC3C,CAAA;AAED,+DAA+D;AAClD,QAAA,OAAO,GAAgB;IAClC,KAAK,EAAE,eAAe;IACtB,IAAI,EAAE,KAAK;IACX,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,MAAM;IACnD,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,KAAK;IAC9C,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,CAAC,KAAK,CAAC;IACd,mBAAmB,EAAE,IAAI;IACzB,qBAAqB,EAAE,KAAK;IAC5B,gBAAgB,EAAE,KAAK;IACvB,QAAQ,EAAE,IAAI;CACf,CAAA;AAED,sEAAsE;AACzD,QAAA,QAAQ,GAAgB;IACnC,KAAK,EAAE,gBAAgB;IACvB,IAAI,EAAE,WAAW;IACjB,QAAQ,EAAE,MAAM;IAChB,QAAQ,EAAE,MAAM;IAChB,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,CAAC,MAAM,CAAC;IACf,mBAAmB,EAAE,IAAI;IACzB,qBAAqB,EAAE,KAAK;IAC5B,gBAAgB,EAAE,KAAK;IACvB,QAAQ,EAAE,IAAI;CACf,CAAA","sourcesContent":["import { INewUserAPI } from './interfaces'\n\n// The API user, should be provisioned on build with local Rocket.Chat\nexport const apiUser: INewUserAPI = {\n username: process.env.ADMIN_USERNAME || 'admin',\n password: process.env.ADMIN_PASS || 'pass'\n}\n\n// The Bot user, will attempt to login and run methods in tests\nexport const botUser: INewUserAPI = {\n email: 'bot@localhost',\n name: 'Bot',\n password: process.env.ROCKETCHAT_PASSWORD || 'pass',\n username: process.env.ROCKETCHAT_USER || 'bot',\n active: true,\n roles: ['bot'],\n joinDefaultChannels: true,\n requirePasswordChange: false,\n sendWelcomeEmail: false,\n verified: true\n}\n\n// The Mock user, will send messages via API for the bot to respond to\nexport const mockUser: INewUserAPI = {\n email: 'mock@localhost',\n name: 'Mock User',\n password: 'mock',\n username: 'mock',\n active: true,\n roles: ['user'],\n joinDefaultChannels: true,\n requirePasswordChange: false,\n sendWelcomeEmail: false,\n verified: true\n}\n"]}
\ No newline at end of file
......@@ -7,30 +7,59 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
}
Object.defineProperty(exports, "__esModule", { value: true });
// Test script uses standard methods and env config to connect and log streams
const driver = __importStar(require("../lib/driver"));
const config_1 = require("./config");
// Start subscription to log message stream (used for e2e test)
const __1 = require("..");
const delay = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms));
// Start subscription to log message stream (used for e2e test and demo)
function start() {
return __awaiter(this, void 0, void 0, function* () {
yield driver.connect();
yield driver.login({ username: config_1.botUser.username, password: config_1.botUser.password });
yield driver.joinRooms(config_1.botRooms);
yield driver.subscribeToMessages();
yield driver.respondToMessages((err, msg, msgOpts) => {
yield __1.driver.connect();
yield __1.driver.login({ username: config_1.botUser.username, password: config_1.botUser.password });
yield __1.driver.subscribeToMessages();
yield __1.driver.respondToMessages((err, msg, msgOpts) => {
if (err)
throw err;
console.log('[respond]', JSON.stringify(msg), JSON.stringify(msgOpts));
demo(msg).catch((e) => console.error(e));
}, {
rooms: ['general'],
allPublic: false,
dm: true,
edited: true,
livechat: false
});
});
}
// Demo bot-style interactions
// A: Listen for "tell everyone <something>" and send that something to everyone
// B: Listen for "who's online" and tell that person who's online
function demo(message) {
return __awaiter(this, void 0, void 0, function* () {
console.log(message);
if (!message.msg)
return;
if (/tell everyone/i.test(message.msg)) {
const match = message.msg.match(/tell everyone (.*)/i);
if (!match || !match[1])
return;
const sayWhat = `@${message.u.username} says "${match[1]}"`;
const usernames = yield __1.api.users.allNames();
for (let username of usernames) {
if (username !== config_1.botUser.username) {
const toWhere = yield __1.driver.getDirectMessageRoomId(username);
yield __1.driver.sendToRoomId(sayWhat, toWhere); // DM ID hax