Conditional

A pizza ordering bot that only allows people over 18 to order

https://github.com/meya-ai/community/tree/master/examples/pizzabot

Notable concepts:

  • Conditional flow checks a users age and only allows those over 18 to proceed
  • Age checking component that routes the flow according to >= 18 or < 18
states:
    ask:
        component: meya.input_string
        properties:
            text: "How old are you?"
            output: age
    check:
        component: age_checker
        transitions:
            under: block
            over: allow
    block:
        component: meya.text
        properties:
            text: "Sorry, you have to be 18 to order our pizza."
        return: true
    allow:
        component: meya.text
        properties:
            text: "Awesome, you can order :)"
        return: true

This is a basic conditional flow that traverses to different states depending on the transitions specified.

The component that checks the age and sets the transitions according to the rules is called age_checker. If the age specified is under 18, the flow moves to the state block. If the age specified is over 18, the flow moves to the state allow.

from meya import Component


class AgeChecker(Component):

    def start(self):
        # read in the age, and default to `0` if invalid or missing
        age = self.db.flow.get('age')
        try:
            age = int(age)
        except:
            age = 0

        # check the age
        if age >= 18:
            action = "over"
        else:
            action = "under"

        # the action determines which transition is invoked
        # note that no message is returned, just an action
        return self.respond(message=None, action=action)

The data is retrieved from the datastore and checked against a condition (age >= 18). The actions over or under are referenced in the flow and moved to the states specified.