Learn Rivescript
Study from rivescript and rivescript-js Write part of the doc as I prefered.
Compare with Superscript, I prefer rivescript more for the following reason:
- Doc is far more complete than superscript and has all kinds of examples.
- Rivescript is more controllable than superscript. Superscript will do some random thing for more Intelligent.
- Rivescript using Unicode to support Chinese wildcards and almost support all basic Chinese rules, which superscript cannot do.
- Really simple, without MongoDB, while superscript has MongoDB built-in.
Command
+
Triggers
-
Replies
!
Definitions
//
Comments
%
Previous
*
Conditionals
^
Line Breaking
Tags
Tags has <angled>
brackets
Insert text in their place, or set a variable silently.
<@>
<star>
,<star1>
- <starN>
These tags can not be used with + Trigger
<botstar>
, <botstar1>
- <botstarN>
This tag is similar to <star>
, but it captures wildcards present in a % Previous
line. Here is an example:
+ i bought a new * |
These tags can not be used with + Trigger
.
<input>
, <reply>
The input and reply tags are used for showing previous messages sent by the user and the bot, respectively. The previous 9 messages and responses are stored, so you can use the tags <input1>
through <input9>
, or <reply1>
through <reply9>
to get a particular message or reply. <input>
is an alias for <input1>
, and <reply>
is an alias for <reply1>
.
// If the user repeats the bot's previous message |
<id>
This tag inserts the user’s ID, which was passed in to the RiveScript interpreter when fetching a reply. With the interpreter shipped with the Perl RiveScript library, the <id>
is, by default, localuser
.
<bot>
The <bot>
tag is used for retrieving a bot variable. It can also be used to set a bot variable.
+ what is your name |
<env>
The <env>
tag is used for retrieving global variables. It can also be used to set a global variable.
+ set debug mode (true|false) |
<get>
,<set>
<add>
, <sub>
, <mult>
, <div>
+ give me 5 points |
These tags can not be used with + Trigger
<@>
equal to {@ <star>}
<formal>
, <sentence>
, <uppercase>
, <lowercase>
<call>
Tags have {curly}
brackets
Modify the text around them.
{random}
+ say something random |
{weight}
+ greetings |
{@ } / <@>
{topic}
{person},
{ok}
\s
\n
Line Breaking
+ tell me a poem |
\s
space
\n
line break
Definitions
- ! local concat = newline | space | none (”file scoped”)
- ! version = 2.0
- ! var name = Tutorial
- ! global debug = true
- ! global depth = 50
- ! person i am = you are
rivescript brain
start from begin.rive
— contains some configuration settings for your bot’s brain.
- const
- variables— bot variables
- Substitutions: always lowercased
- array
- global
- person
Trigger
catch-all wildcards
+ my name is * |
special wildcards
#
only match a number_
only match a word with no numbers or spaces
Alternatives and Optionals
alternatives
+ what is your (home|office|cell) number |
optionals
+ how [are] you |
+ [*] the machine [*] |
use [*]
optionals to ignore parts of a message by putting it before or after your trigger instead of on both sides.
Arrays in triggers
// Single word array items |
+ what color is my (@colors) * |
// Without parenthesis, the array doesn't go into a <star> tag. |
// Arrays in an optional |
priority triggers
Default: triggers with more words are tested first
This is useful to “hand tune” how well a trigger matches the user’s message.
+ google * |
What if somebody asked the bot, “google write perl script”? They might expect the bot to provide them with a Google search link, but instead the bot replies talking about needing Perl. This is because “ perl script” has more words than “google “, and therefore would usually be a better match.
+ google *{weight=10} |
Redirections
use outside the reply
+ hello |
use inside the reply
{@ <star>}
will redirect to *
reply
+ * or something{weight=100} |
shortcut of {@ <star>}
is <@>
+ hello * |
% Previous
use %
to make a short discussion
% Previous
lines need to be lowercased just like triggers do.
+ knock knock |
Learning Things
<set>
& <get>
+ my name is * |
retrieve variables from begin.rive
// The user can ask the bot its name too! |
The <formal>
tag is a shortcut for {formal}<star>{/formal}
// Store the name with the correct casing |
Writing Conditionals
+ what is my name |
+ my name is * |
Constions:
== equal to |
< less than |
Labeled sections
begin with >
and end with <
Topic
+ i hate you |
Also, you can use <set topic=random>
instead, but there is a small difference in how the two tags will behave:
The <set>
tag can appear multiple times in a reply and each one is processed in order. The {topic}
tag can only appear once (if there are multiple ones, the first one wins). So, they’ll both do the same job, but {topic}
is a little shorter to type.