EchoBot II
respond() 関数を作成したので、次は send_message() という関数を定義しましょう。この関数は message という単一のパラメーターを受け取り、message とボットの応答をログに記録します。
この演習はコースの一部です
Python でチャットボットを作る
演習の手順
user_template文字列の.format()メソッドを使って、ユーザーのmessageをユーザーテンプレートに組み込み、結果を出力してください。respond()関数に渡されたmessageを引数として呼び出し、結果をresponseとして保存してください。bot_template文字列の.format()メソッドを使って、ボットのresponseをログに記録してください。- ボットに
"hello"というメッセージを送ってください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create templates
bot_template = "BOT : {0}"
user_template = "USER : {0}"
# Define a function that sends a message to the bot: send_message
def ____(____):
# Print user_template including the user_message
print(____.format(____))
# Get the bot's response to the message
response = ____(____)
# Print the bot template including the bot's response.
print(____.format(____))
# Send a message to the bot
send_message("____")