Putting it all together II
With your chitchat_response(message)
function defined, the next step is to define a send_message()
function. This function should first call chitchat_response(message)
and only use the coffee bot policy if there is no matching message.
This exercise is part of the course
Building Chatbots in Python
Exercise instructions
- Define a
send_message()
function which takes in 3 arguments:state
,pending
, andmessage
. - Call
chitchat_response(message)
, storing the result inresponse
. If there is a response, print it and return thestate
along withNone
. - Unpack the
policy_rules
dictionary into the variablesnew_state
,response
, andpending_state
. To do this, pass in a tuple consisting ofstate
andinterpret(message)
. - If
pending
is not none, extract the new states and response by usingpending
as the key ofpolicy_rules
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define send_message()
def ____:
print("USER : {}".format(message))
response = ____
if response is not None:
print("BOT : {}".format(response))
return state, None
# Calculate the new_state, response, and pending_state
____, ____, ____ = ____[(____, ____)]
print("BOT : {}".format(response))
if pending is not None:
new_state, response, pending_state = ____
print("BOT : {}".format(response))
if pending_state is not None:
pending = (pending_state, interpret(message))
return new_state, pending
# Define send_messages()
def send_messages(messages):
state = INIT
pending = None
for msg in messages:
state, pending = send_message(state, pending, msg)
# Send the messages
send_messages([
"I'd like to order some coffee",
"555-12345",
"do you remember when I ordered 1000 kilos by accident?",
"kenyan"
])