1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
- from botbuilder .core import ActivityHandler , TurnContext , UserState , CardFactory , MessageFactory
5
- from botbuilder .schema import ChannelAccount , HeroCard , CardImage , CardAction , ActionTypes
4
+ from botbuilder .core import (
5
+ ActivityHandler ,
6
+ TurnContext ,
7
+ UserState ,
8
+ CardFactory ,
9
+ MessageFactory ,
10
+ )
11
+ from botbuilder .schema import (
12
+ ChannelAccount ,
13
+ HeroCard ,
14
+ CardImage ,
15
+ CardAction ,
16
+ ActionTypes ,
17
+ )
6
18
7
19
from data_models import WelcomeUserState
8
20
@@ -18,76 +30,76 @@ def __init__(self, user_state: UserState):
18
30
19
31
self .user_state_accessor = self .user_state .create_property ("WelcomeUserState" )
20
32
21
- self .WELCOME_MESSAGE = """This is a simple Welcome Bot sample. This bot will introduce you
22
- to welcoming and greeting users. You can say 'intro' to see the
23
- introduction card. If you are running this bot in the Bot Framework
24
- Emulator, press the 'Restart Conversation' button to simulate user joining
33
+ self .WELCOME_MESSAGE = """This is a simple Welcome Bot sample. This bot will introduce you
34
+ to welcoming and greeting users. You can say 'intro' to see the
35
+ introduction card. If you are running this bot in the Bot Framework
36
+ Emulator, press the 'Restart Conversation' button to simulate user joining
25
37
a bot or a channel"""
26
-
38
+
27
39
async def on_turn (self , turn_context : TurnContext ):
28
40
await super ().on_turn (turn_context )
29
41
30
42
# save changes to WelcomeUserState after each turn
31
43
await self .user_state .save_changes (turn_context )
32
44
33
- """
34
- Greet when users are added to the conversation.
35
- Note that all channels do not send the conversation update activity.
36
- If you find that this bot works in the emulator, but does not in
37
- another channel the reason is most likely that the channel does not
38
- send this activity.
39
- """
40
-
41
45
async def on_members_added_activity (
42
46
self , members_added : [ChannelAccount ], turn_context : TurnContext
43
47
):
48
+ """
49
+ Greet when users are added to the conversation.
50
+ Note that all channels do not send the conversation update activity.
51
+ If you find that this bot works in the emulator, but does not in
52
+ another channel the reason is most likely that the channel does not
53
+ send this activity.
54
+ """
44
55
for member in members_added :
45
56
if member .id != turn_context .activity .recipient .id :
46
57
await turn_context .send_activity (
47
58
f"Hi there { member .name } . " + self .WELCOME_MESSAGE
48
59
)
49
60
50
- await turn_context .send_activity ("""You are seeing this message because the bot received at least one
51
- 'ConversationUpdate' event, indicating you (and possibly others)
52
- joined the conversation. If you are using the emulator, pressing
53
- the 'Start Over' button to trigger this event again. The specifics
54
- of the 'ConversationUpdate' event depends on the channel. You can
61
+ await turn_context .send_activity (
62
+ """You are seeing this message because the bot received at least one
63
+ 'ConversationUpdate' event, indicating you (and possibly others)
64
+ joined the conversation. If you are using the emulator, pressing
65
+ the 'Start Over' button to trigger this event again. The specifics
66
+ of the 'ConversationUpdate' event depends on the channel. You can
55
67
read more information at: https://aka.ms/about-botframework-welcome-user"""
56
68
)
57
69
58
- await turn_context .send_activity ("""It is a good pattern to use this event to send general greeting
59
- to user, explaining what your bot can do. In this example, the bot
70
+ await turn_context .send_activity (
71
+ """It is a good pattern to use this event to send general greeting
72
+ to user, explaining what your bot can do. In this example, the bot
60
73
handles 'hello', 'hi', 'help' and 'intro'. Try it now, type 'hi'"""
61
74
)
62
75
63
- """
64
- Respond to messages sent from the user.
65
- """
66
-
67
76
async def on_message_activity (self , turn_context : TurnContext ):
77
+ """
78
+ Respond to messages sent from the user.
79
+ """
68
80
# Get the state properties from the turn context.
69
- welcome_user_state = await self .user_state_accessor .get (turn_context , WelcomeUserState )
81
+ welcome_user_state = await self .user_state_accessor .get (
82
+ turn_context , WelcomeUserState
83
+ )
70
84
71
85
if not welcome_user_state .did_welcome_user :
72
86
welcome_user_state .did_welcome_user = True
73
87
74
88
await turn_context .send_activity (
75
89
"You are seeing this message because this was your first message ever to this bot."
76
- )
90
+ )
77
91
78
92
name = turn_context .activity .from_property .name
79
93
await turn_context .send_activity (
80
- f"It is a good practice to welcome the user and provide personal greeting. For example: Welcome { name } "
94
+ f"It is a good practice to welcome the user and provide personal greeting. For example: Welcome { name } "
81
95
)
82
-
96
+
83
97
else :
84
98
# This example hardcodes specific utterances. You should use LUIS or QnA for more advance language
85
99
# understanding.
86
100
text = turn_context .activity .text .lower ()
87
101
if text in ("hello" , "hi" ):
88
- await turn_context .send_activity (
89
- f"You said { text } "
90
- )
102
+ await turn_context .send_activity (f"You said { text } " )
91
103
elif text in ("intro" , "help" ):
92
104
await self .__send_intro_card (turn_context )
93
105
else :
@@ -97,37 +109,35 @@ async def __send_intro_card(self, turn_context: TurnContext):
97
109
card = HeroCard (
98
110
title = "Welcome to Bot Framework!" ,
99
111
text = "Welcome to Welcome Users bot sample! This Introduction card "
100
- "is a great way to introduce your Bot to the user and suggest "
101
- "some things to get them started. We use this opportunity to "
102
- "recommend a few next steps for learning more creating and deploying bots." ,
103
- images = [
104
- CardImage (
105
- url = "https://aka.ms/bf-welcome-card-image"
106
- )
107
- ],
112
+ "is a great way to introduce your Bot to the user and suggest "
113
+ "some things to get them started. We use this opportunity to "
114
+ "recommend a few next steps for learning more creating and deploying bots." ,
115
+ images = [CardImage (url = "https://aka.ms/bf-welcome-card-image" )],
108
116
buttons = [
109
117
CardAction (
110
118
type = ActionTypes .open_url ,
111
119
title = "Get an overview" ,
112
120
text = "Get an overview" ,
113
121
display_text = "Get an overview" ,
114
- value = "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0"
122
+ value = "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0" ,
115
123
),
116
124
CardAction (
117
125
type = ActionTypes .open_url ,
118
126
title = "Ask a question" ,
119
127
text = "Ask a question" ,
120
128
display_text = "Ask a question" ,
121
- value = "https://stackoverflow.com/questions/tagged/botframework"
129
+ value = "https://stackoverflow.com/questions/tagged/botframework" ,
122
130
),
123
131
CardAction (
124
132
type = ActionTypes .open_url ,
125
133
title = "Learn how to deploy" ,
126
134
text = "Learn how to deploy" ,
127
135
display_text = "Learn how to deploy" ,
128
- value = "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0"
129
- )
130
- ]
136
+ value = "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0" ,
137
+ ),
138
+ ],
131
139
)
132
140
133
- return await turn_context .send_activity (MessageFactory .attachment (CardFactory .hero_card (card )))
141
+ return await turn_context .send_activity (
142
+ MessageFactory .attachment (CardFactory .hero_card (card ))
143
+ )
0 commit comments