External functions
This section describes how external calls work. External functions are needed to process data with SDK. For example you may need process data, get access to databases or APIs and use it for any other purposes.
In DashaScript, external functions are declared using an external function
keyword:
external function foo(arg1: number, arg2: string): string;
To call an external function, you should prefix the call expression with an external
keyword:
node Node { do { var result = external foo(123, "aaa"); } }
For each external function, an implementation must be provided to an application object using the setExternal
method:
const dasha = require("@dasha.ai/sdk"); const app = await dasha.deploy("path/to/app"); app.setExternal("foo", (args, conv) => { // arguments are passed as a dictionary in the first argument (`args`) // the keys are the same as argument names in DashaScript console.log(args.arg1, args.arg2); // the second argument (`conv`) is a conversation object // it can be used, for example, to access conversation input variables console.log(conv.input.foo); // the function should return a value of an appropriate type // or a promise that resolves to a value of that type // that also means that these functions may be `async` return "qwerty"; });
Found a mistake? Let us know.