keiffster / program-y

Python 3.x based AIML 2.0 Chatbot interpreter, framework, related programs and knowledge files
https://keiffster.github.io/program-y/
Other
348 stars 138 forks source link

What is the best approach to set variables from a service? #244

Closed acriptis closed 4 years ago

acriptis commented 4 years ago

I need an embedded component within AIML scenario that sets a variable of the user to specific value in runtime. Then my template supposed to reuse the variable.

I've implemented a service which sets a property:

class DayTimeClfService(Service):
    """
    AIML Service that classifies time into 4 classes:
    - evening,
    - night,
    - morning,
    - day
    and puts it into user_variable `time_of_day`
    """

    def __init__(self, config=None, api=None):
        Service.__init__(self, config)

    def ask_question(self, client_context, question: str):
        time_of_day = classify_current_time()
        # service adds a property with part of date for usage by greeting templates:
        client_context.brain.properties.add_property('time_of_day', time_of_day)

And I have an AIML category:

<category>
        <pattern>HELLODAY</pattern>
        <template>
            <sraix service="daytimeclf"></sraix>
            <get name="time_of_day" />
            <get name="name" />
            <condition name="time_of_day">
                <li value="night">Good Night, friend! Why don't you sleep so late?</li>
                <li value="morning">Good Morning!</li>
                <li value="day">Hello!</li>
                <li value="evening">Good Evening!</li>
                <li>Hi. What timezone do you use?</li>
            </condition>
        </template>
    </category>

Unfortunately when we get out of sraix the property is missed. Although within service context I can command: client_context.brain.properties.property("time_of_day") and it returns correct result.

By the way I tried to set dynamic_variable, but it even does not set it:

ipdb> client_context.brain.dynamics.set_dynamic_var(client_context, 'time_of_day', 'evening')                                                                              
ipdb> client_context.brain.dynamics.dynamic_vars                                                                                                                           
{'GETTIME': <programy.dynamic.variables.datetime.GetTime object at 0x7f0df1db93c8>}

Is it possible to set properties and variables from custom services?