11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
33
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+ )
618
719from data_models import WelcomeUserState
820
@@ -18,76 +30,76 @@ def __init__(self, user_state: UserState):
1830
1931 self .user_state_accessor = self .user_state .create_property ("WelcomeUserState" )
2032
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
2537 a bot or a channel"""
26-
38+
2739 async def on_turn (self , turn_context : TurnContext ):
2840 await super ().on_turn (turn_context )
2941
3042 # save changes to WelcomeUserState after each turn
3143 await self .user_state .save_changes (turn_context )
3244
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-
4145 async def on_members_added_activity (
4246 self , members_added : [ChannelAccount ], turn_context : TurnContext
4347 ):
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+ """
4455 for member in members_added :
4556 if member .id != turn_context .activity .recipient .id :
4657 await turn_context .send_activity (
4758 f"Hi there { member .name } . " + self .WELCOME_MESSAGE
4859 )
4960
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
5567 read more information at: https://aka.ms/about-botframework-welcome-user"""
5668 )
5769
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
6073 handles 'hello', 'hi', 'help' and 'intro'. Try it now, type 'hi'"""
6174 )
6275
63- """
64- Respond to messages sent from the user.
65- """
66-
6776 async def on_message_activity (self , turn_context : TurnContext ):
77+ """
78+ Respond to messages sent from the user.
79+ """
6880 # 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+ )
7084
7185 if not welcome_user_state .did_welcome_user :
7286 welcome_user_state .did_welcome_user = True
7387
7488 await turn_context .send_activity (
7589 "You are seeing this message because this was your first message ever to this bot."
76- )
90+ )
7791
7892 name = turn_context .activity .from_property .name
7993 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 } "
8195 )
82-
96+
8397 else :
8498 # This example hardcodes specific utterances. You should use LUIS or QnA for more advance language
8599 # understanding.
86100 text = turn_context .activity .text .lower ()
87101 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 } " )
91103 elif text in ("intro" , "help" ):
92104 await self .__send_intro_card (turn_context )
93105 else :
@@ -97,37 +109,35 @@ async def __send_intro_card(self, turn_context: TurnContext):
97109 card = HeroCard (
98110 title = "Welcome to Bot Framework!" ,
99111 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" )],
108116 buttons = [
109117 CardAction (
110118 type = ActionTypes .open_url ,
111119 title = "Get an overview" ,
112120 text = "Get an overview" ,
113121 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" ,
115123 ),
116124 CardAction (
117125 type = ActionTypes .open_url ,
118126 title = "Ask a question" ,
119127 text = "Ask a question" ,
120128 display_text = "Ask a question" ,
121- value = "https://stackoverflow.com/questions/tagged/botframework"
129+ value = "https://stackoverflow.com/questions/tagged/botframework" ,
122130 ),
123131 CardAction (
124132 type = ActionTypes .open_url ,
125133 title = "Learn how to deploy" ,
126134 text = "Learn how to deploy" ,
127135 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+ ],
131139 )
132140
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