8000 Merge pull request #160 from Microsoft/axsuarez/echo-bot-sample · baruchiro/botbuilder-python@a24d931 · GitHub
[go: up one dir, main page]

Skip to content

Commit a24d931

Browse files
authored
Merge pull request microsoft#160 from Microsoft/axsuarez/echo-bot-sample
Updating echobot sample
2 parents 44d910b + b3542b6 commit a24d931

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

samples/Console-EchoBot/bot.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from sys import exit
2+
3+
class EchoBot():
4+
async def on_turn(self, context):
5+
# Check to see if this activity is an incoming message.
6+
# (It could theoretically be another type of activity.)
7+
if (context.activity.type == 'message' and context.activity.text):
8+
# Check to see if the user sent a simple "quit" message.
9+
if (context.activity.text.lower() == 'quit'):
10+
# Send a reply.
11+
await context.send_activity('Bye!')
12+
exit(0)
13+
else:
14+
# Echo the message text back to the user.
15+
await context.send_activity(f'I heard you say {context.activity.text}')
16+
17+
18+

samples/Console-EchoBot/main.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,11 @@
66
from botbuilder.schema import ActivityTypes
77

88
from adapter import ConsoleAdapter
9+
from bot import EchoBot
910

1011
# Create adapter
1112
adapter = ConsoleAdapter()
12-
13-
# Create MemoryStorage, UserState and ConversationState
14-
memory = MemoryStorage()
15-
# Commented out user_state because it's not being used.
16-
# user_state = UserState(memory)
17-
conversation_state = ConversationState(memory)
18-
19-
# Register both State middleware on the adapter.
20-
# Commented out user_state because it's not being used.
21-
# adapter.use(user_state)
22-
adapter.use(conversation_state)
23-
24-
25-
async def logic(context: TurnContext):
26-
if context.activity.type == ActivityTypes.message:
27-
state = await conversation_state.get(context)
28-
29-
# If our conversation_state already has the 'count' attribute, increment state.count by 1
30-
# Otherwise, initialize state.count with a value of 1
31-
if hasattr(state, 'count'):
32-
state.count += 1
33-
else:
34-
state.count = 1
35-
await context.send_activity(f'{state.count}: You said "{context.activity.text}"')
36-
else:
37-
await context.send_activity(f'[{context.activity.type} event detected]')
13+
bot = EchoBot()
3814

3915
loop = asyncio.get_event_loop()
4016

@@ -43,7 +19,7 @@ async def logic(context: TurnContext):
4319
# Greet user
4420
print("Hi... I'm an echobot. Whatever you say I'll echo back.")
4521

46-
loop.run_until_complete(adapter.process_activity(logic))
22+
loop.run_until_complete(adapter.process_activity(bot.on_turn))
4723
except KeyboardInterrupt:
4824
pass
4925
finally:

samples/Core-Bot/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
This sample shows how to create a simple EchoBot with state.
66
"""
77

8-
8+
import yaml
9+
import os
910
from aiohttp import web
1011
from botbuilder.schema import (Activity, ActivityTypes)
1112
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, TurnContext,
@@ -27,7 +28,11 @@
2728
user_state = UserState(memory)
2829
conversation_state = ConversationState(memory)
2930

30-
dialog = MainDialog({})
31+
relative_path = os.path.abspath(os.path.dirname(__file__))
32+
path = os.path.join(relative_path, "config.yaml")
33+
with open(path, 'r') as ymlfile:
34+
cfg = yaml.load(ymlfile)
35+
dialog = MainDialog(cfg['settings'])
3136
bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)
3237

3338
async def messages(req: web.Request) -> web.Response:

0 commit comments

Comments
 (0)
0