Skip to main content

Project files

At this step, we will create a configuration file and a script for the bot using functions from the previous step.

Configuration file

Create a chatbot.yaml bot configuration file and set the following parameters:

# Project name
name: lastLetterGame
# Main file
entryPoint:
- main.sc

# NLU parameters:
botEngine: v2
language: en

nlp:
intentNoMatchThresholds:
phrases: 0.2
patterns: 0.2

Script file

Create a main.sc file in the src folder and paste the following script:

require: responseCity.js

require: city/cityEn.sc
module = sys.zb-common

theme: /

state: Start || modal = true
q!: $regex</start>
intent!: /LetsPlay
script:
$session = {}
$client = {}
$temp = {}
$response = {}
a: Let's play "Cities" Game. Who will start first: bot or user?

state: User
intent: /user
a: Name the city
script:
$session.keys = Object.keys($Cities);
$session.prevBotCity = 0;
go!: /LetsPlayCitiesGame

state: Computer
intent: /computer
script:
$session.keys = Object.keys($Cities);
var city = $Cities[chooseRandCityKey($session.keys)].value.name
$reactions.answer(city)
$session.prevBotCity = city

go!: /LetsPlayCitiesGame

state: LocalCatchAll
event: noMatch
a: I don't understand you. Please try again.

state: LetsPlayCitiesGame
state: CityPattern
q: * $City *
script:
if (isAFullNameOfCity()) {
if (checkLetter($parseTree._City.name, $session.prevBotCity) == true
|| $session.prevBotCity == 0) {
var removeCity = findByName($parseTree._City.name, $session.keys, $Cities)

if (checkCity($parseTree, $session.keys, $Cities) == true) {
$session.keys.splice(removeCity, 1)
var key = responseCity($parseTree, $session.keys, $Cities)
if (key == 0) {
$reactions.answer("I give up")
} else {
$reactions.answer($Cities[key].value.name)
$session.prevBotCity = $Cities[key].value.name
removeCity = findByName($Cities[key].value.name, $session.keys, $Cities)
$session.keys.splice(removeCity, 1)
}
} else $reactions.answer("You can't name the same city twice. Try again.")
}
} else $reactions.answer("Please use only full cities name")

state: noMatch
event: noMatch
a: I don't know this city. Try again.

state: EndGame
intent!: /endThisGame
a: That's a pity! If you change your mind, just text me "Let's play"

Additional modules

At the beginning of the script, we use the require tag to import responseCity.js, which contains the functions created in the previous step.

We also import the city system module, which allows accessing the cities dictionary from script extensions via the $Cities variable, as well as use the $City named entity in patterns.

The main states

The script consists of a few states:

  • Start — start of the script. The bot welcomes the user, offers them to play the game, and asks to choose who will start the game first.
  • LetsPlayCitiesGame — start of the game. The state contains a logic for checking the entered city and returns the bot’s answer.
  • EndGame — end of the game, if the user wants to finish the game.

Next, move on to working with the script structure.