Scripts client
The SDK provides a Scripts client to execute Action scripts.
The client
variable in the following code examples refers to the IMClient
instance. When using the Web SDK, the variable name can be chosen freely, but it is also called client
at instantiation in the documentation.
Action script example
Assume the following Action script is published and enabled:
// For simplicity, assume 'name' is always present
var name = Context.Data["name"].ToString();
Context.Result["message"] = $"Hello, {name}!";
This script will get the "name" property from the Data
, and sets a hello message under message
in the Result
.
For more information, see creating scripts.
Executing a script with the SDK
Assume that the script above has identifier fYaE4Q518ECLRtO2b3W-3Q
. With the SDK, the script can be executed as follows:
// using Newtonsoft.Json.Linq;
// ...
var arguments = new JObject
{
{ "name", "world" } // Key: "name", value: "world"
};
var scriptIdentifier = "fYaE4Q518ECLRtO2b3W-3Q";
var result = await clMClientient.Scripts.ExecuteActionScriptAsync(scriptIdentifier, arguments);
MClient.Logger.Info(result["message"].ToString());
This will log the message that the script returned, which should be: "Hello, world!".
The result
returned by the ExecuteActionScriptAsync
is identical to the Context.Result
object from the Action script.