我想写一个快速简单的聊天机器人,可以携带与用户的对话。我想知道如何创建对话,允许无限量的输入和响应。现在我使用的代码不允许用户定义输入。这就是我现在正在使用的代码。在# Import the random module
import random
bot_template = "AGENT: {0}"
user_template = "USER: {0}"
# Define variables
name = "Greg"
weather = "cloudy"
# Define a dictionary containing a list of responses for each message
responses = {
"what's your name?": [
"my name is {0}".format(name),
"they call me {0}".format(name),
"I go by {0}".format(name)
],
"what's today's weather?": [
"the weather is {0}".format(weather),
"it's {0} today".format(weather)
],
"default": ["default message"]
}
# Use random.choice() to choose a matching response
def respond(message):
# Check if the message is in the responses
if message in responses:
# Return a random matching response
bot_message = random.choice(responses[message])
else:
# Return a random "default" response
bot_message = random.choice(responses["default"])
return bot_message
# Define a function that sends a message to the bot: send_message
def send_message(message):
# Print user_template including the user_message
print(user_template.format(message))
# Get the bot's response to the message
response = respond(message)
# Print the bot template including the bot's response.
print(bot_template.format(response))
# Send a message to the bot
send_message("what's today's weather?")