1 | Chat-bots in Lua
|
2 | ================
|
3 |
|
4 | TinyBrain (TinyBrain.de) now offers a way of creating a chat-bot (a program you can talk to) using very simple Lua code.
|
5 |
|
6 | Software installation required on your machine: none. All you need is a browser. Your chat-bots will be available on the Internet instantly.
|
7 |
|
8 | To create a chat-bot, upload a snippet of Lua code to the TinyBrain snippets database (http://tinybrain.de:8080/tb/). Choose type "Lua code - Chat-bot" and make the snippet public by ticking the appropriate checkbox.
|
9 |
|
10 | Remember the snippet's ID, e.g. #123 and enter the ID on the front page (http://tinybrain.de).
|
11 |
|
12 | That's all!
|
13 |
|
14 |
|
15 | How to write the code
|
16 | =====================
|
17 |
|
18 | You'll get the user's last input in the variable "input". So a simple, stateless bot can look like this:
|
19 |
|
20 | return(input == "" and "Say something!" or "You just said: "..input)
|
21 |
|
22 | Yes, that's only one line of code. :)
|
23 |
|
24 |
|
25 | Stateful bots
|
26 | =============
|
27 |
|
28 | Of course it is nice to have the bot remember something between user lines. To do this, use the "cookies" table, which is a Lua map (with string keys and string values).
|
29 |
|
30 | Here's an example from snippet #27 (http://tinybrain.de/27):
|
31 |
|
32 | if input ~= "" and cookies.name == nil then
|
33 | cookies.name = input
|
34 | end
|
35 |
|
36 | if cookies.name then
|
37 | return("Hello "..cookies.name.."!")
|
38 | else
|
39 | return "What's your name?"
|
40 | end
|
41 |
|
42 | Yes, that's all. btw, the cookies are totally persistent - even between server restarts.
|
43 |
|
44 |
|
45 | Sandbox
|
46 | =======
|
47 |
|
48 | The Lua scripts run in a very tight sandbox. There is literally nothing "bad" you can do, not even writing endless loops.
|
49 |
|
50 | If you write more complex examples, you'll probably need more Lua libraries (only very few functions are available in the sandbox right now). If you have such a need, just write a mail (stefan.reich.maker.of.eye@gmail.com).
|
51 |
|
52 | Cheers!
|
53 | -Stefan |