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\Tags\Custom 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. <googlesearch></googlesearch>

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 helloworld tag.

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

    
<?php
namespace App\Tags\Custom;

use App\Classes\LemurLog;
use App\Tags\AimlTag;
use App\Models\Conversation;

/**
 * Class HelloworldTag
 * @package App\Tags\Custom
 *
 * Usage: <helloworld />
 *
 * Example AIML:
 * <category>
 *  <pattern>TEST</pattern>
 *  <template><helloworld /></template>
 * </category>
 *
 * Expected Conversation:
 * Input: Test
 * Output: Hello World!
 *
 * Documentation:
 * https://docs.lemurengine.com/extend.html
 */
 class HelloworldTag extends AimlTag
 {
    protected $tagName = "Helloworld";


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


     /**
     * This method is called when the closing tag is encountered e.g. <helloworld/>
     * @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 googlesearch tag.

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

    
<?php

namespace App\Tags\Custom;

use App\Classes\LemurLog;
use App\Tags\AimlTag;
use App\Models\Conversation;

/**
 * Class Googlesearch
 * @package App\Tags\Custom
 *
 * Usage: <googlesearch>Cake Recipe</googlesearch
 *
 * Example AIML:
 * <category>
 *  <pattern>SEARCH GOOGLE FOR A *</pattern>
 *  <template><googlesearch><star /></googlesearch</template>
 * </category>
 *
 * Expected Conversation:
 * Input: Search google for a cake recipe
 * Output: Check these search results: <a href="https://www.google.com/search?q=cake+recipe" target="_new">cake recipe</a>
 *
 * Documentation:
 * https://docs.lemurengine.com/extend.html
 *
 */
 class GooglesearchTag extends AimlTag
 {
    protected $tagName = "Googlesearch";


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


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

         //gets the current content of the tag e.g. what currently exists in <googlesearch>something</googlesearch> (something)
         $tagContents = $this->getCurrentTagContents(true);

         //build the string for the response..
         $urlEncodedTagContents = urlencode($tagContents);
         $responseStr = "Check these search results: <a href=\"https://www.google.com/search?q=".$urlEncodedTagContents."\" target=\"_new\">".$tagContents."</a>";

         //build response in the stack
         $this->buildResponse($responseStr);
     }
 }
            
        

Guidelines

Follow the guidelines below when making your custom tag;

  1. Extend the class AimlTag()
  2. Override the closeTag() in the first instances
  3. You can also override the startTag(), ProcessContents() but in 99% of cases that isn't necessary
  4. If you need config for your tag create a config file in the config directory

If you would like to share your custom tag with others, in addition to the above you should;

  1. Document an example category and expect conversation in the comments
  2. Make your code as easy to read as possible

If you would submit your custom tag to the main project, in addition to the above you should;

  1. Write a test category in the database/seeds/UnitTestCategoriesTableSeeder.php file
  2. Write a test in the tests/AIML/AIMLConversationTest.php file
  3. Submit a pull request into the develop branch with your tag

Want a Chatbot in the simplest fasts way possible?

Host Bots

Hosted Solution

Check out our Hosted Solutions - We have a number of subscription plans available so you can get going in no time - with no coding experience. It's the simplest of way to get going quickly.

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