Preprocessing
CSML Studio also offers a way to preprocess every incoming event of certain types, in order to add some metadata or transform it. Here are some use cases:
  • transcribe audio inputs
  • translate incoming text
  • save uploaded files to a dedicated file storage
  • perform OCR on each uploaded image
  • run a custom natural language processing library on text inputs (for that use case, you may also want to look at Natural Language Processing)
Preprocessors are simply external apps that are run on every incoming event (of the given type) and return a valid CSML event. The only specificity is that the parameters it must accept and return are standard CSML event payloads. Preprocessors that fail to accept or return such payloads are simply ignored and your incoming event will not be transformed.

How to add a CSML preprocessor

Let's create a simple Text preprocessor that will return a picture of a hot dog if the user had the word "hot dog" in their input, or change the text to "Not hot dog" otherwise.
To add a preprocessor, you first need to add a preprocessor-compatible app under the apps section (see here). For this tutorial, let's just use the Quick Mode, but of course you can use Complete Mode as well.
This is the code we are going to use:
1
exports.handler = async (event) => {
2
3
const text = event.content.text;
4
5
if (text.toLowerCase().includes('hot dog')) {
6
return {
7
content_type: 'image',
8
content: {
9
P url: 'https://media.istockphoto.com/photos/hot-dog-on-white-picture-id1130731707',
10
_original_event: event,
11
},
12
};
13
}
14
15
return {
16
content_type: 'text',
17
content: {
18
text: 'Not hot dog!',
19
_original_event: event,
20
},
21
};
22
23
};
24
Copied!
It is really simple: if the user text input contains the words "hot dog" anywhere, then return an image of a hotdog. Otherwise, change the text to "Not hot dog". In both cases, the original, untransformed event is still accessible with event._original_event in you CSML code, if needed.
Next, go to Apps > Add Custom App and under Quick Mode, simply copy and paste it, select a name for your app (for instance, hotdog seems like a fitting name), and click save.
Then, head to the Preprocessing page. We only want to preprocess text events, but of course, we could do a similar app for other types of events.
Now, back to the CSML Editor, you can do something like the following:
1
start:
2
if (event.get_type() == "image") say Image(event)
3
else say event
4
goto end
Copied!
Last modified 5mo ago
Export as PDF
Copy link