Skip to content

Commit

Permalink
New examples + scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud committed Aug 15, 2016
1 parent 95c4e52 commit a9e6f93
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 82 deletions.
171 changes: 114 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
# BotML format
# BotML

<abbr title="Bot Markup Language">BotML</abbr> is a powerful markup language for modern chatbots.
[![NPM version](https://badge.fury.io/js/botml.svg)](http://badge.fury.io/js/botml.io)
[![NPM dependencies](https://david-dm.org/BotML/botml-js/status.svg)](https://david-dm.org/BotML/botml-js)

<abbr title="Bot Markup Language">BotML</abbr> is a declarative and powerful markup language for modern chatbots.

## How to use

```bash
$ npm install botml -g
```

Then either run the cli:

```bash
$ bot # my-botml-implementation.bot
```

or use it in your code:

```js
var BotML = require('botml');
var bot = new BotML();
bot.load(['my-botml-implementation.bot']);
bot.start();
```

## Features

Expand All @@ -21,119 +45,152 @@ To do:

## Format

### Blocks

**Specification**

! BOTML <version>
```bash
! BOTML <version>
```

The current version being `1`.

**Comment**

# This line is not interpreted
```bash
# This line is not interpreted
```

**Dialogue**

> <input>
< <output>
```bash
> <input>
< <output>
```

Example:

> hi
< hi there
```bash
> hi
< hi there
```

**List**

= <list>
- <item>
- <item>
```bash
= <list>
- <item>
- <item>
```

Example:

= fruits
- apples
- apricots
- bananas
```bash
= fruits
- apples
- apricots
- bananas

> I like [fruits]
< Oh. I prefer [fruits].
> I like [fruits]
< Oh. I prefer [fruits].
```

**Service**

API endpoints can be leveraged as easily as:

@ <name> <endpoint>
```bash
# Definition
@ <name> <endpoint>
# Consumption
@ <name>($).<output>
```

Example:

@ geoapi http://localhost:3000/search?q=$

> go to *
| location = @geoapi($)
< You want to go to $location?

**NLP**
```bash
@ geodomain http://freegeoip.net/json/$

TODO
> Where is *
@ geodomain($).city
< It is running from $.
```

**Scripting**

Scripting can be done with Javascript code evaluation.

> It will cost you #{price} USD
< `1000 * $price`k USD is a lot!
```bash
> It will cost you #{price} USD
< `1000 * $price`k USD is a lot!
```

**Variable**

Variables can be either textual (*) or numeric (#)

> My name is *{name}
< Nice to meet you, $name
```bash
> My name is *{name}
< Nice to meet you, $name

> I am #{age} years old
< Seems that you have `age`
> I am #{age} years old
< Seems that you have `age`
```

**Regular Expression**

> I like to /move|break|stretch/ it
< Cool bro.
```bash
> I like to /move|break|stretch/ it
< Cool bro.
```

**Dialogue workflow**

# A grocery shopper must know what and how many to buy
~ grocery shopping
< What?
> #{count} ${item}
> ${item}
< How many ${item}?
> #{count}
```bash
# A grocery shopper must know what and how many to buy
~ grocery shopping
< What?
> #{count} ${item}
> ${item}
< How many ${item}?
> #{count}
```

Simple question for learning a notion:

< Where were you born?
@ city = geoapi($)
> So you are from $city.
```bash
< Where were you born?
@ geoapi($).city
> So you are from $.
```

The same question with more checks and conditional branching:

~ origin
< Where were you born?
> in *{city}
> near *{city}
> *{city}
@ city = geoapi($city)
if $city == 'Heaven'
< Then go to hell!
else
< Gotcha. You're from $city.
```bash
~ origin
< Where were you born?
> in *{city}
> near *{city}
> *{city}
@ city = geoapi($city).city
if $city == 'Heaven'
< Then go to hell!
else
< Gotcha. You're from $city.
```
**Trigger**
@ trigger('name')
```bash
@ trigger('name')
```
Example:
> hi
@ trigger('said_hi')
```bash
> hi
@ trigger('said_hi')
```
Then handle the 'said_hi' event in your code according to your needs.
Expand Down
12 changes: 12 additions & 0 deletions examples/calculator.bot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
! BOTML 1

# A simple calculator.
#
# Example:
# > 1 + 1
# < 1 + 1 = 2
# > 4 * 100 + 55 % 6
# < 4 * 100 + 55 % 6 = 401

> *
< $ = `$`
10 changes: 5 additions & 5 deletions examples/hello.bot
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# A simple yet polite bot.
#
# Examples:
# - Hi! or - Hello Mr. Robot
# - Hello. What's your name? - Hey. What's your name?
# - Alice - My name is Bob
# - Nice to meet you Alice! - Nice to meet you Bob!
# > Hi! or > Hello Mr. Robot
# < Hello. What's your name? < Hey. What's your name?
# > Alice > My name is Bob
# < Nice to meet you Alice! < Nice to meet you Bob!

= hello
- hello
Expand All @@ -17,7 +17,7 @@
- aloha

> [hello]
< [hello]. What's your name?
< [hello]. `1000 * 19` What's your name?
> My name is *
> *
< Nice to meet you $!
22 changes: 16 additions & 6 deletions examples/service.bot
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
# The domain is actually saved in memory and is geolocalized to its city of
# operation.
#
# Example:
# - Where is github.com?
# - It is running from San Francisco.
# Examples:
# > Where is github.com?
# < It is running from San Francisco.
#
# > What is my ip?
# < It's 93.184.216.34

@ geoapi http://freegeoip.net/json/$
@ geodomain http://freegeoip.net/json/$

> Where is *(?)
@ geoapi($)
< It is running from $.city.
@ geodomain($).city
< It is running from $.


@ ip http://httpbin.org/ip

> ip
@ ip($).origin
< It's $.
20 changes: 10 additions & 10 deletions examples/workflow.bot
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# A bot that has a lot to ask you.
#
# Example:
# - Hello sir. Please answer this quick survey to help us better know you. Alright?
# - Ok
# - Great!
# < Hello sir. Please answer this quick survey to help us better know you. Alright?
# > Ok
# < Great!
# How old are you?
# - 25
# - And where were you born?
# - I was born in Bermuda Island
# - Gotcha. You're from Bermuda Island.
# - What is your email address?
# - email@example.org
# - Perfect. We'll keep in touch with you over 'email@example.org'.
# > 25
# < And where were you born?
# > I was born in Bermuda Island
# < Gotcha. You're from Bermuda Island.
# What is your email address?
# > email@example.org
# < Perfect. We'll keep in touch with you over 'email@example.org'.

~ survey
< [hello] sir. Please answer this quick survey to help us better know you. [ok]?
Expand Down
20 changes: 18 additions & 2 deletions lib/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ module.exports = class Block {
}
break;
case 'workflow':
// if the current line references another workflow, let's interpolate it
this.value = this.raw.replace(/^\s*~\s*\[([!\]]+)\]\s*$/, (m, workflow) => {
let w = context.workflows.get(workflow);
if (w) {
this.raw = w.raw;
} else {
warn(`The workflow "${workflow}" does not exist.`);
}
});
// remove the first line
this.value = this.raw.replace(/^\s*~.+$\n\s*/m, '');
this.value = this.raw.replace(/^\s*~\s*[^\[]+\s*$\n\s*/m, '');
break;
}
//debug('updateText', this.label, this.activators, this.split('\n').join('\\'));
Expand All @@ -75,7 +84,14 @@ module.exports = class Block {
.split(/\s*<\s*/).filter(s => s).map(s => s.trim())
;
let answer = interpolateLists(random(responseCandidates));
if (answer) say(answer);
try {
answer = answer.replace(/`([^`]*)`/g, (m,script) =>
eval(interpolateVariables(script))
);
if (answer) say(answer);
} catch (e) {
warn("Error while running script", e);
}
// remove all following lines of the bot answering
this.next();
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (raw_pattern) => {
.replace(/\(([^\)]+)\)/g, '(?:$1)?')
.replace(/#/g, '(\d+)')
.replace(/_/g, '([\\w\\._\\-]+)')
.replace(/\*/g, '([\\w\\._\\-]*)')
.replace(/\*/g, '(.*)')
// lists
.replace(/\[(\w+)\]/, (m,l) => `(?:${context.lists.get(l.toLowerCase()).value.join('|')})`)
;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "botml",
"version": "0.0.1",
"description": "<abbr title=\"Bot Markup Language\">BotML</abbr> is a powerful markup language for modern chatbots.",
"description": "BotML is a powerful markup language for modern chatbots.",
"keywords": [ "bot", "chatbot", "aiml", "chatscript", "buddyscript", "rivescript", "siml" ],
"author": "Arnaud Leymet <arnaud.leymet@gmail.com>",
"license": "MIT",
Expand Down

0 comments on commit a9e6f93

Please sign in to comment.