My First Trigger

This section explains how to create and test a Trigger in the bot created in the "My First Bot" example

Step by Step


Step 1: Add Trigger


Adding an Easybots 'Trigger' to a bot is done in two steps:
  • Decorate a public instance method with the attributes
    [Easybots.Apps.TriggerAttribute] and
    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
  • Inside the method, call the this.TriggerInEasybotsPlatform() method.
class MyFirstBot : Easybots.Apps.Easybot
{
    public MyFirstBot() : base(ID: "My First Bot")
    {}

    [Easybots.Apps.Action]
    public string SayHelloWorld()
    {
        Console.WriteLine("Hello World!");
        return "Hello World!";
    }

    [Easybots.Apps.Trigger]
    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
    public string KeyPressed(string key)
    {
        this.TriggerInEasybotsPlatform(key);
        return key;
    }
}
Verify Step 1 - click to expand..
Start the app. In Easybots Studio, the bot "My First Bot", should have one trigger called 'KeyPressed'.

Step 2: Invoking the Trigger


The triggers are invoked inside the app, simply by calling the method decorated as [Trigger]. Each time that method is called, the Easybots platform will be notified.
For example, we can modify the 'Main' method, so that each time a key is pressed on the keyboard, the trigger 'KeyPressed' will be invoked.
static void Main(string[] args)
{
    var link = Easybots.Apps.EasybotsLink.CreateLink();
    var bot = new MyFirstBot();
    while(true)
    {
        var keyInfo = Console.ReadKey();
        string data = keyInfo.KeyChar.ToString();
          
        // This line will invoke the trigger
        bot.KeyPressed(data);
    }
}

Step 3: Create a Solution


Now we want to create a simple Easybots solution to test what we've build - when a key is pressed in the app, the 'SayHelloWorld' action will be invoked.
3.1. In Easybots Studio, add a new solution named 'Test Hello World'
3.2. With the mouse, drag & drop the KeyPressed trigger to the Test Hello World solution
3.3. Next, drag and drop the SayHelloWorld action to the KeyPressed node in the solution.


Step 4: Perform the actual test


In the previous step, we created a solution: when a key is pressed in the app, the app should say "Hello World". Let's do the actual test
4.1. Bring the app (console application) in focus.
4.2. Press any key on the keyboard.
After pressing the key, "Hello World" should also be written in the app.

Next: Passing Data