Extend

Last updated: Mar 15th, 2021

How To

You can extend the capabilities of your bot using tags created by other people.

Or you can create new tags yourself and even offer them for other people to use.

Custom tag classes live in the App\LemurTag directory. You can have a look and see an example for the both types of tags;

  1. Self Closing Tags e.g. <helloworld/>
  2. Paired Tags e.g. <echo></echo>

In 99% of cases copying one of those files and adding your functionality to the closeTag() method will suit your needs.

Example Custom Self Closing Tags

Below is the custom HelloWorldTag.

You can find a copy of this in the app/LemurTag directory.

    
<?php

namespace App\LemurTag;

use LemurEngine\LemurBot\Classes\LemurLog;
use LemurEngine\LemurBot\LemurTag\AimlTag;
use LemurEngine\LemurBot\Models\Conversation;

/**
 * Class HelloWorldTag
 * @package App\LemurTag
 *
 * Usage: 
 *
 * Example AIML:
 * 
 *  TEST HELLO WORLD
 *  
 * 
 *
 * Expected Conversation:
 * Input: Test Hello World
 * Output: Hello World!
 *
 * Documentation:
 * https://docs.lemurengine.com/extend.html
 */
class HelloWorldTag extends AimlTag
{
    protected string $tagName = "HelloWorld";
    //this is a standard tag
    static $aimlTagType = self::TAG_STANDARD;


    /**
     * HelloWorldTag Constructor.
     * @param Conversation $conversation
     * @param array $attributes
     */
    public function __construct(Conversation $conversation, array $attributes = [])
    {
        parent::__construct($conversation, $attributes);
    }


    /**
     * This method is called when the closing tag is encountered e.g. 
     * @return string|void
     */
    public function closeTag()
    {
        //some debugging
        LemurLog::debug(
            __FUNCTION__, [
                'conversation_id'=>$this->conversation->id,
                'turn_id'=>$this->conversation->currentTurnId(),
                'tag_id'=>$this->getTagId(),
                'attributes'=>$this->getAttributes()
            ]
        );

        //build response in the stack
        $this->buildResponse("Hello World!");
    }
}


            
        

Example Custom Paired Tags

Below is the custom EchoTag.

You can find a copy of this in the app/LemurTag directory.

    
<?php
namespace App\LemurTag;

use LemurEngine\LemurBot\Classes\LemurLog;
use LemurEngine\LemurBot\LemurTag\AimlTag;
use LemurEngine\LemurBot\Models\Conversation;

/**
 * Class EchoTag
 * @package App\LemurTag
 *
 * Usage: 
 *
 * Example AIML:
 * 
 *  ECHO *
 *  
 * 
 *
 * Expected Conversation:
 * Input: Echo Yikes
 * Output: Yikes
 *
 * Documentation:
 * https://docs.lemurengine.com/extend.html
 */
class EchoTag extends AimlTag
{
    protected string $tagName = "Echo";
    //this is a standard tag
    static $aimlTagType = self::TAG_STANDARD;


    /**
     * FormalTag Constructor.
     * @param Conversation $conversation
     * @param array $attributes
     */
    public function __construct(Conversation $conversation, array $attributes = [])
    {
        parent::__construct($conversation, $attributes);
    }


    /**
     * when we close the  tag we need to decide if we want
     */
    public function closeTag()
    {

        LemurLog::debug(
            __FUNCTION__,
            [
                'conversation_id'=>$this->conversation->id,
                'turn_id'=>$this->conversation->currentTurnId(),
                'tag_id'=>$this->getTagId(),
                'attributes'=>$this->getAttributes()
            ]
        );

        //this will return the value of 
        $contents = $this->getCurrentTagContents(true);
        $this->buildResponse($contents);
    }
}
    
        

Guidelines

Follow the guidelines below when making your custom tag;

  1. It is critical that your class has the app/LemurTag namespace
  2. Extend the class AimlTag()
  3. Add your tag logic to the closeTag() method
  4. You can also update the startTag(), ProcessContents() but in 99% of cases that isn't necessary
  5. If you need config for your tag create a config file in the config directory

You can create custom tag packages to allow others to pull in your custom tags or just to better organise your own tags;
Checkout the custom GoogleSearchTag on Packagist for an example on how to do this: lemurtag-googlesearch

Want a Chatbot in the simplest fasts way possible?

Host Bots

Chatbot Consultants

If you need help with customisation or a bespoke feature - then get in touch with us over that theramenrobotdiscocode.com and let's see how we can help.

Get In Touch