Merge pull request #160 from Microsoft/axsuarez/echo-bot-sample
Updating echobot sample
This commit is contained in:
Коммит
a24d9313ab
|
@ -0,0 +1,18 @@
|
|||
from sys import exit
|
||||
|
||||
class EchoBot():
|
||||
async def on_turn(self, context):
|
||||
# Check to see if this activity is an incoming message.
|
||||
# (It could theoretically be another type of activity.)
|
||||
if (context.activity.type == 'message' and context.activity.text):
|
||||
# Check to see if the user sent a simple "quit" message.
|
||||
if (context.activity.text.lower() == 'quit'):
|
||||
# Send a reply.
|
||||
await context.send_activity('Bye!')
|
||||
exit(0)
|
||||
else:
|
||||
# Echo the message text back to the user.
|
||||
await context.send_activity(f'I heard you say {context.activity.text}')
|
||||
|
||||
|
||||
|
|
@ -6,35 +6,11 @@ from botbuilder.core import TurnContext, ConversationState, UserState, MemorySto
|
|||
from botbuilder.schema import ActivityTypes
|
||||
|
||||
from adapter import ConsoleAdapter
|
||||
from bot import EchoBot
|
||||
|
||||
# Create adapter
|
||||
adapter = ConsoleAdapter()
|
||||
|
||||
# Create MemoryStorage, UserState and ConversationState
|
||||
memory = MemoryStorage()
|
||||
# Commented out user_state because it's not being used.
|
||||
# user_state = UserState(memory)
|
||||
conversation_state = ConversationState(memory)
|
||||
|
||||
# Register both State middleware on the adapter.
|
||||
# Commented out user_state because it's not being used.
|
||||
# adapter.use(user_state)
|
||||
adapter.use(conversation_state)
|
||||
|
||||
|
||||
async def logic(context: TurnContext):
|
||||
if context.activity.type == ActivityTypes.message:
|
||||
state = await conversation_state.get(context)
|
||||
|
||||
# If our conversation_state already has the 'count' attribute, increment state.count by 1
|
||||
# Otherwise, initialize state.count with a value of 1
|
||||
if hasattr(state, 'count'):
|
||||
state.count += 1
|
||||
else:
|
||||
state.count = 1
|
||||
await context.send_activity(f'{state.count}: You said "{context.activity.text}"')
|
||||
else:
|
||||
await context.send_activity(f'[{context.activity.type} event detected]')
|
||||
bot = EchoBot()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
|
@ -43,7 +19,7 @@ if __name__ == "__main__":
|
|||
# Greet user
|
||||
print("Hi... I'm an echobot. Whatever you say I'll echo back.")
|
||||
|
||||
loop.run_until_complete(adapter.process_activity(logic))
|
||||
loop.run_until_complete(adapter.process_activity(bot.on_turn))
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
This sample shows how to create a simple EchoBot with state.
|
||||
"""
|
||||
|
||||
|
||||
import yaml
|
||||
import os
|
||||
from aiohttp import web
|
||||
from botbuilder.schema import (Activity, ActivityTypes)
|
||||
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, TurnContext,
|
||||
|
@ -27,7 +28,11 @@ memory = MemoryStorage()
|
|||
user_state = UserState(memory)
|
||||
conversation_state = ConversationState(memory)
|
||||
|
||||
dialog = MainDialog({})
|
||||
relative_path = os.path.abspath(os.path.dirname(__file__))
|
||||
path = os.path.join(relative_path, "config.yaml")
|
||||
with open(path, 'r') as ymlfile:
|
||||
cfg = yaml.load(ymlfile)
|
||||
dialog = MainDialog(cfg['settings'])
|
||||
bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)
|
||||
|
||||
async def messages(req: web.Request) -> web.Response:
|
||||
|
|
Загрузка…
Ссылка в новой задаче