Improv parses message templates at runtime with data from user attributes, pre-populated data and/or custom extensions.
e.g. "hello ${ this.user.name }" will render with the value at the user.name path in current data.
Message strings containing expressions are automatically rendered by Improv middleware and can be merged with data from any source, including a Transcript search for instance.
Note:
this.user.name
is the value at user.name path..use
..reset()
will clear everything (for testing).Improv
Setup middleware and improv data collection in the brain.
This is the main interface to get either a new or existing instance. If the robot is new but an instance exists (e.g. in tests) then Improv will attach the new robot but keep existing config and extensions.
Object
Configure the Improv instance
Improv
Add function to extend current data when rendering a template. Should return key/values to merge and/or override keys of existing data.
If given a path argument, the extension function will only be called when the template string being rendered contains that path. This can prevent slow or expensive requests from running when their data isn't required.
Extensions can set properties within current data model or return a new object, the keys and values will be deep merged so either will work.
array
Search an array of strings for template expressions with this.
data path.
Object
Provdies current data to messages merged with response context any extra data returned by added extensions.
array
Replace expressions in sent string if they match the format of a data at a path bound to 'this', with the data at that path after collecting from all sources.
string
Convert string containing expression to an interpolation template and call with supplied data bound to 'this'.
Pre-renders expressions to catch and replace any unknowns. Failed expressions will be replaced with fallback unless a full replacement is configured, to replace the entire string.
Middleware parses template expressions, replacing with data if required.
Object
Add data to context on the fly
Object
Add data to context specific for a given user's ID
Object
Remove data from data on the fly
Object
Remove data from data for a given user's ID
Wipte slate for tests to reinitialise without existing instance or context
Improv parses message templates at runtime with data from user attributes, pre-populated data and/or custom extensions.
e.g. “hello ${ this.user.name }” will render with the value at the user.name path in current data.
Message strings containing expressions are automatically rendered by Improv middleware and can be merged with data from any source, including a Transcript search for instance.
Note:
this.user.name
is the value at user.name path..use
..reset()
will clear everything (for testing).Kind: global class
Returns: Improv
- New or prior existing (singleton) instance
Param | Type | Description |
---|---|---|
robot | Robot |
Hubot Robot instance |
Improv
Setup middleware and improv data collection in the brain.
This is the main interface to get either a new or existing instance. If the robot is new but an instance exists (e.g. in tests) then Improv will attach the new robot but keep existing config and extensions.
Kind: global function
Returns: Improv
- The instance - really only accessed by tests
Param | Type | Description |
---|---|---|
robot | Robot |
The robot to use, usually existing from constructor |
Object
Configure the Improv instance
Kind: global function
Returns: Object
- The module exports for chaining
Param | Type | Description |
---|---|---|
[options] | Object |
Key/val options for config |
[options.save] | boolean |
Keep data collection in hubot brain |
[options.fallback] | string |
Fallback content replace any unknowns within messages |
[options.replacement] | string |
Replace all messages containing unknowns, overrides fallback |
[options.admins] | array |
Array of usernames authorised to populate data |
Improv
Add function to extend current data when rendering a template. Should return key/values to merge and/or override keys of existing data.
If given a path argument, the extension function will only be called when the template string being rendered contains that path. This can prevent slow or expensive requests from running when their data isn’t required.
Extensions can set properties within current data model or return a new object, the keys and values will be deep merged so either will work.
Kind: global function
Returns: Improv
- The instance for chaining
Param | Type | Description |
---|---|---|
dataFunc | function |
Receives current data, to return more |
[dataPath] | string |
Scope for running the extension (optional) |
Example (extend data with user transcript history)
improv.use(robot)
improv.extend((data) => {
data.user.favColor = 'always blue'
return data
}, 'user.favColor')
robot.send({ user: user }, 'I know your favorite color is ${ this.user.favColor }')
// ^ middleware will render template with the values and user in data
// ^ by providing the path, it will only be run when specifically required
array
Search an array of strings for template expressions with this.
data path.
Kind: global function
Returns: array
- Path matches objects\n
- [0]: expression including braces, e.g. `${ ... }`
- [1]: the path, e.g. this.path.to.data
- index: index of expression in string
- input: string containing the matched path
Param | Type | Description |
---|---|---|
strings | array |
Strings to search (usually from middleware) |
Object
Provdies current data to messages merged with response context any extra data returned by added extensions.
Kind: global function
Returns: Object
- Data, middleware context and any extensions merged
Param | Type | Description |
---|---|---|
context | Object |
Data provided at runtime to merge with improv data (usually from middleware) |
[paths] | array |
Paths required, to filter out unnecessary extensions |
array
Replace expressions in sent string if they match the format of a data at a path bound to ‘this’, with the data at that path after collecting from all sources.
Kind: global function
Returns: array
- Strings populated with context values
Param | Type | Description |
---|---|---|
context | object |
Context object (usually from middleware) |
context.strings | array |
One or more strings being posted |
context.response | object |
Response object being replied to |
string
Convert string containing expression to an interpolation template and call with supplied data bound to ‘this’.
Pre-renders expressions to catch and replace any unknowns. Failed expressions will be replaced with fallback unless a full replacement is configured, to replace the entire string.
Kind: global function
Returns: string
- The result of rendering match input with given data
Param | Type | Description |
---|---|---|
string | string |
The string to replace expressions within |
match | object |
RegExp result where string contained an expression |
callData | object |
Data to become ‘this’ when rendering expressions |
Middleware parses template expressions, replacing with data if required.
Kind: global function
Param | Type | Description |
---|---|---|
context | Object |
Passed through middleware stack, with res |
next | function |
Called when all middleware is complete |
done | function |
Initial (final) completion callback |
Object
Add data to context on the fly
Kind: global function
Returns: Object
- The module exports for chaining
Param | Type | Description |
---|---|---|
path | array/string |
The path of the property to set |
value | * |
The value to set |
Object
Add data to context specific for a given user’s ID
Kind: global function
Returns: Object
- The module exports for chaining
Param | Type | Description |
---|---|---|
id | string |
The id of the user to remember for |
path | array/string |
The path of the property to set |
value | * |
The value to set |
Object
Remove data from data on the fly
Kind: global function
Returns: Object
- The module exports for chaining
Param | Type | Description |
---|---|---|
path | array/string |
The path of the property to unset |
Object
Remove data from data for a given user’s ID
Kind: global function
Returns: Object
- The module exports for chaining
Param | Type | Description |
---|---|---|
id | string |
The id of the user to remember for |
path | array/string |
The path of the property to unset |
Wipte slate for tests to reinitialise without existing instance or context
Kind: global function