-
Notifications
You must be signed in to change notification settings - Fork 0
Components: Processor
Processors components are responsible for processing the incoming events and/or data, and can be both - synchronous and asynchronous. It is also possible to create processor components that do not modify the data but only produce side-effects (e.g. send emails, send slack message, etc).
In the simplest instance, synchronous "Hello world!" processor component would look like this:
export default (data) => {
return Rx.Observable.return(`${data} - hello world!`);
};
This processor takes in data, converts it to a string and dispatches a new string with appended "- hello world!" sentence.
Note that it returns Rx.Observable
- this is a requirement for all processors since they are applied with .flatMap
.
To do asynchronous processing, you need to simply return an observable and then .flatMap
it with your asynchronous action.
Here's an example of processor that executed Duckduckgo search using incoming data.text
field:
import {DDG} from 'node-ddg-api';
const ddg = new DDG('exynize-ddg');
export default (data) => Rx.Observable
.return(data)
.flatMap(input => Rx.Observable.create(obs => {
ddg.instantAnswer(input.text, {skip_disambig: '0'}, (err, res) => {
if (err) {
obs.onError(err);
return;
}
obs.onNext(Object.assign({}, data, res));
obs.onCompleted();
});
}));
Sometimes you might need to allow to pass configuration options for the processor.
This can be done by adding more parameters to the function.
Here's an example of processor component that will send reply to Slack with result
text:
import Botkit from 'botkit';
export default (token, data) => Rx.Observable.create(obs => {
const controller = Botkit.slackbot({debug: false});
const bot = controller.spawn({token}).startRTM(err => {
if (err) {
obs.onError(err);
return;
}
bot.reply({...data}, `Here's what I found: \n> ${data.result}`);
obs.onCompleted();
});
return () => {
bot.closeRTM();
};
});
Note that observable
variable will always be the last parameter of the function.
You can find real-world examples of processor components in usecases folder, currently the following ones are available: