hatool
TypeScript icon, indicating that this package has built-in type declarations

0.10.0 • Public • Published

screenshot

A themeable chat-like UI element for Angular 10+.

See demo

screenshot


Quickstart

$ npm install --save hatool@latest

Then, in your app.module.ts, import the Hatool module:

import { HatoolLibModule } from 'hatool';

@NgModule({
  declarations: [
      // ...
  ],
  imports: [
    HatoolLibModule,

API

htl-hatool

This is the chatbot component. Put it where you'd like the chat box to live, and pass it a ContentManager instance.

<htl-hatool [content]='content'></htl-hatool>

ContentManager

The content manager is the object that holds all the chat compoments ('messages') that are currently visible to the user, as well as a queue of yet-to-be-displayed messages.

You need to instantiate a content manager for every chat component that you'd like to show, like so

import { ContentManager } from 'hatool';

// ...

const contentManager: ContentManager = new ContentManager();

Normally you wouldn't need to use the Content Manager's API directly, although in some advanced cases you might want to do that. In any case, through the Content Manager you can add messages to the chat manually, and change the behaviour of the chat in various ways.

ScriptRunnerImpl

The script runner is responsible for reading the conversation 'script' and applying it to the content manager.

You instantiate a Script Runner object using three parameters - a core Angular HttpClient instance, a ContentManager instance and the current locale (as scripts have i18n support - more on that later).

const runner = new ScriptRunnerImpl(this.http, this.content, 'en');

Running a script is done using the runner's run() method. This method returns an observable which fires when the script finished running.

runner.run(
    scriptUrl, scriptIndex,
    customFunctions,
    valueSetCallback,
    record
).subscribe(() => { console.log('done!'); });

record is one of the most important bits you need to pass the runner, as it serves as the context of the execution script. Commands refer to it, read from it and put values into it.

Other arguments are:

  • scriptUrl is the location of the script file that is to be executed
  • scriptIndex is the index of the specific script to be run in the script file (each script file can contain multiple scripts, see later).
  • customFunctions is a map containing any custom functionality that is required in the script (see later in the do command)
  • valueSetCallback is a callback that is called whenever a value is written in to the record (which is useful in case you'd like to save the record to a backend server, for example).

See a more detailed example here.

Script Syntax

We write scripts in a yaml file and compile it to a json file that the runner is able to read.

(To compile we can use the `hatool-compiler' tool).

Basic structure

Each script file contains one or more scripts:

- name: Main script
  description: script for random users browsing the website
  ...

- name: Support script
  description: script for users requesting support

Each script in a script file contains one or more snippets. The entry-point to the script is also named the default snippet. The default snippet must be named default.

Snippets

Each snippet has a name and a list of steps to execute - each step runs a single command.

A full 'Hello, World' example:

- name: Hello World
  description: A simple "Hello World" script
  snippets:
    - name: default
      steps:
        - say: Hello, World!

Script Commands

say

This command will add a 'bot' message to the chat.

The basic structure of this command is by using a simple string:

- say: Hi there!

You can also use simple templating to fill in values from the record:

- say: Hi there, {{name}}!

The templating engine supports dot notation for accessing inner objects:

- say: Hi there, {{name.first}}!

In case you need to support multiple languages, say also accepts the same string in multiple locales:

- say:
    es: Hola, amigo!
    _: Hi there, friend!

The script runner will choose the correct string based on the provided locale, or _ as the fallback.

wait

wait is a general purpose command for receiving input from the user.

It has a four modes: single text line, long text, single-select options and multiple-select options.

Single Text Line

This is the basic functionality of this command, used for receiving a user's response as a single line of text.

This command has the following mandatory parameter:

  • variable - field in record in which the response will be stored

And the following optional parameters:

  • response - Template for formatting the response, default is the raw user's response
  • required - Should we require a value in this field or allow empty responses? (true by default)
  • placeholder - text to show in the input box when it's empty (i.e. before the user started typing)
  • input-kind - used for native field validation and customization. Some possible values are text, number, date - see here for more examples. By default all inputs are text.
  • input-min - used to specify a minimum value for the field, when relevand by the input type
  • input-max - used to specify a maximum value for the field, when relevand by the input type
  • input-step - used to specify a numerical step value for the field, when relevand by the input type
  • suggestionsFrom - TBD
  • validation - a regular expression which the value needs to match before it's considered valid and accepted.
- say: What is your age?
- wait:
    placeholder: Your age...
    input-kind: number
    input-min: 1
    input-max: 120
    input-step: 1
    variable: age

Long Text

'Long Text' mode is very similar to 'Single Text Line' mode. The only difference being that a multiple-line input box is used instead of a single line one. The user interaction also differs so that pressing 'Enter' on the keyboard doesn't automatically submit the user's response, but rather adds a new line to the current response.

To enable 'Long Text' mode simply add long: yes to the wait command:

- wait:
    variable: thoughts
    long: yes

Single-select options

This variant of wait is for when there's a small finite set of possible responses for the user to choose from. These options are displayed as a few buttons, which when clicked correspond to the user selecting a specific option.

In this variant the command receives an options parameter which lists the different possible options.

Each item in option is an object with the following andatory field:

  • show - what to display as the button's label. Has the same behavious as in say (i.e. supports templating and i18n)

And these possible fields:

  • value - value to assign to this option (in case it's missing, the show value will be used)
  • steps - list of steps to be run when this option is selected.
  • unless - specifies a field name to check in record. It will attach the CSS unless class to this option if the value for that field in record is not truthy.
  • class - CSS class to be assigned to the button (for theming purposes). String value, empty by default.
  • echo - Prevents the button from being shown in case it was selected. Boolean value, true by default.
  • func - callable to be executed upon selection. The return value of the callable is then used as the actual selected value. If null is returned, then the selection is ignored altogether. Might be useful, for example, when wanting to integrate a 'confirm modal' UI for a specific option's choice.

The wait command contains an optionsFrom parameter instead of options. In that case, the content of the options is fetched from record[wait.optionsFrom].

- say: Please select a flavour
- wait:
    variable: selected_flavour
    options:
        - show: Dark Chocolate
          value: chocolate
        - show: Vanilla
          value: vanilla
        - show: I don't want Ice Cream
          value: doesnt-want
          class: other

Multiple-select options

This final variant of wait allows selecting multiple boolean possibilities (for a 'check all options that apply' kind of scenario).

To enable this mode, set the multi parameter to true while following the same rules for the Single-select options mode. Then, each option can have a field parameter which converts it to a checkbox option.

The return value is an object which contains all the fields from all the checked options set to true.

Always remember to leave one option without a field to serve as the "done selecting" option.

- say: Please choose toppings
- wait:
    variable: selected_toppings
    multi: true
    options:
        - show: Sprinkles
          field: sprinkles
        - show: Chocolate Syrup
          field: chocolate_syrup
        - show: Done selecting

switch

The switch command is used for branching.

It inspects the value of a specific firld of record, provided in the arg parameter, and based on its value matches a single case in a list of cases.

Each case is then evaluated and selected based on one of these four methods:

  • match - this case will be selected if the record[arg] is equal to the value of the match parameter
  • pattern - similar to match, except that the value of pattern is interpreted as a regular expression that is tested against record[arg]
  • default - if true, will serve as a fallback case in case no other case was selected
  • undefined - will only match if record[arg] is undefined

Each case can specify a list of steps to execute if selected in the steps parameter.

- switch:
    arg: selected_color
    cases:
        - match: red
          steps:
            - say: That's a very bold color!
        - pattern: .*blue
          steps:
            - say: Any kind of blue is great!
        - default: true
          steps:
            - say: I don't have any specific opinion on {{selected_color}}
        - undefined: true
            # Do nothing if not defined!

do

This command is used to incorporate custom functionality within the script.

It receives the mandatory cmd parameter - the name of the function to run - and a few optional parameters:

  • variable - is the name of the field in record in which the return value of the function is to be stored.

  • params - are a list of parameters that are passed to the function.

    A parameter can be any string and it will be processed using the templating and i18n engines prior to being passed to the function. A special value for parameters is record, allowing functions to receive the runner's record, read from it and manipulate it if necessary.

All functions must be defined in the customFunctions parameter for run(). Passed functions can be sync or async functions.

For example, you might define a function like so:

runner.run(
    ...,
    {
        updateCreationDate: (record) => {
            if (record.creation_date) {
                return record.creation_date;
            }
            return (new Date()).toLocaleDateString();
        }
    }
)

And in the script you would call it like so:

- do:
    cmd: updateCreationDate
    variable: creation_date
    params:
      - record

goto

goto is used to jump to another snippet in the current script.

Unlike the C-style goto's, when the called snippet is finished, the current (calling) snippet will continue execution from the next command after goto.

One exception for that is calling - goto: complete. The reserved snippet name complete can be used to stop the script from running altogether, similarly to using return in the middle of a function.

See pop for a more fine-grained method for exiting a snippet.

pop

pop can be used to return from a specific snippet. Execution will pop the call stack until it reaches a specific snippet.

For example, pop: default will return from all running snippets until control returns to the default snippet.

Customization

Theming

Custom commands

I18n

Advanced usage

Allow fixing previous responses

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.10.02latest

Version History

VersionDownloads (Last 7 Days)Published
0.10.02
0.9.20
0.9.10
0.9.00
0.8.41
0.8.31
0.8.21
0.8.10
0.8.00
0.7.80
0.7.70
0.7.60
0.7.51
0.7.40
0.7.30
0.7.20
0.7.10
0.7.00
0.6.12
0.6.01
0.5.440
0.5.430
0.5.421
0.5.410
0.5.400
0.5.390
0.5.380
0.5.370
0.5.360
0.5.350
0.5.340
0.5.331
0.5.320
0.5.310
0.5.302
0.5.291
0.5.281
0.5.270
0.5.260
0.5.251
0.5.240
0.5.230
0.5.220
0.5.210
0.5.200
0.5.190
0.5.180
0.5.170
0.5.160
0.5.150
0.5.140
0.5.132
0.5.124
0.5.112
0.5.100
0.5.91
0.5.81
0.5.70
0.5.60
0.5.54
0.5.40
0.5.30
0.5.20
0.5.10
0.5.03
0.4.00
0.3.690
0.3.682
0.3.671
0.3.661
0.3.651
0.3.640
0.3.630
0.3.621
0.3.610
0.3.601
0.3.591
0.3.581
0.3.570
0.3.564
0.3.551
0.3.540
0.3.532
0.3.520
0.3.510
0.3.500
0.3.491
0.3.481
0.3.470
0.3.460
0.3.450
0.3.440
0.3.430
0.3.420
0.3.413
0.3.401
0.3.390
0.3.380
0.3.370
0.3.361
0.3.350
0.3.340
0.3.331
0.3.320
0.3.310
0.3.301
0.3.291
0.3.280
0.3.270
0.3.260
0.3.250
0.3.240
0.3.230
0.3.220
0.3.211
0.3.200
0.3.190
0.3.180
0.3.171
0.3.160
0.3.150
0.3.140
0.3.130
0.3.120
0.3.111
0.3.100
0.3.90
0.3.80
0.3.70
0.3.60
0.3.50
0.3.40
0.3.30
0.3.20
0.3.10
0.3.00
0.2.40
0.2.30
0.2.20
0.2.10
0.2.00
0.1.70
0.1.60
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00
0.0.240
0.0.230
0.0.220
0.0.210
0.0.200
0.0.190
0.0.180
0.0.170
0.0.160
0.0.150
0.0.140
0.0.130
0.0.120
0.0.110
0.0.100
0.0.90
0.0.80
0.0.70
0.0.60
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i hatool

Weekly Downloads

62

Version

0.10.0

License

none

Unpacked Size

409 kB

Total Files

51

Last publish

Collaborators

  • akariv