|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from botbuilder.core import ActivityHandler, TurnContext, UserState, CardFactory, MessageFactory |
| 5 | +from botbuilder.schema import ChannelAccount, HeroCard, CardImage, CardAction, ActionTypes |
| 6 | + |
| 7 | +from data_models import WelcomeUserState |
| 8 | + |
| 9 | + |
| 10 | +class WelcomeUserBot(ActivityHandler): |
| 11 | + def __init__(self, user_state: UserState): |
| 12 | + if user_state is None: |
| 13 | + raise TypeError( |
| 14 | + "[WelcomeUserBot]: Missing parameter. user_state is required but None was given" |
| 15 | + ) |
| 16 | + |
| 17 | + self.user_state = user_state |
| 18 | + |
| 19 | + self.user_state_accessor = self.user_state.create_property("WelcomeUserState") |
| 20 | + |
| 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 |
| 25 | + a bot or a channel""" |
| 26 | + |
| 27 | + async def on_turn(self, turn_context: TurnContext): |
| 28 | + await super().on_turn(turn_context) |
| 29 | + |
| 30 | + # save changes to WelcomeUserState after each turn |
| 31 | + await self.user_state.save_changes(turn_context) |
| 32 | + |
| 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 | + async def on_members_added_activity( |
| 42 | + self, members_added: [ChannelAccount], turn_context: TurnContext |
| 43 | + ): |
| 44 | + for member in members_added: |
| 45 | + if member.id != turn_context.activity.recipient.id: |
| 46 | + await turn_context.send_activity( |
| 47 | + f"Hi there { member.name }. " + self.WELCOME_MESSAGE |
| 48 | + ) |
| 49 | + |
| 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 |
| 55 | + read more information at: https://aka.ms/about-botframework-welcome-user""" |
| 56 | + ) |
| 57 | + |
| 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 |
| 60 | + handles 'hello', 'hi', 'help' and 'intro'. Try it now, type 'hi'""" |
| 61 | + ) |
| 62 | + |
| 63 | + """ |
| 64 | + Respond to messages sent from the user. |
| 65 | + """ |
| 66 | + |
| 67 | + async def on_message_activity(self, turn_context: TurnContext): |
| 68 | + # Get the state properties from the turn context. |
| 69 | + welcome_user_state = await self.user_state_accessor.get(turn_context, WelcomeUserState) |
| 70 | + |
| 71 | + if not welcome_user_state.did_welcome_user: |
| 72 | + welcome_user_state.did_welcome_user = True |
| 73 | + |
| 74 | + await turn_context.send_activity( |
| 75 | + "You are seeing this message because this was your first message ever to this bot." |
| 76 | + ) |
| 77 | + |
| 78 | + name = turn_context.activity.from_property.name |
| 79 | + await turn_context.send_activity( |
| 80 | + f"It is a good practice to welcome the user and provide personal greeting. For exampl
1241
e: Welcome { name }" |
| 81 | + ) |
| 82 | + |
| 83 | + else: |
| 84 | + # This example hardcodes specific utterances. You should use LUIS or QnA for more advance language |
| 85 | + # understanding. |
| 86 | + text = turn_context.activity.text.lower() |
| 87 | + if text in ("hello", "hi"): |
| 88 | + await turn_context.send_activity( |
| 89 | + f"You said { text }" |
| 90 | + ) |
| 91 | + elif text in ("intro", "help"): |
| 92 | + await self.__send_intro_card(turn_context) |
| 93 | + else: |
| 94 | + await turn_context.send_activity(self.WELCOME_MESSAGE) |
| 95 | + |
| 96 | + async def __send_intro_card(self, turn_context: TurnContext): |
| 97 | + card = HeroCard( |
| 98 | + title="Welcome to Bot Framework!", |
| 99 | + 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 | + ], |
| 108 | + buttons=[ |
| 109 | + CardAction( |
| 110 | + type=ActionTypes.open_url, |
| 111 | + title="Get an overview", |
| 112 | + text="Get an overview", |
| 113 | + display_text="Get an overview", |
| 114 | + value="https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0" |
| 115 | + ), |
| 116 | + CardAction( |
| 117 | + type=ActionTypes.open_url, |
| 118 | + title="Ask a question", |
| 119 | + text="Ask a question", |
| 120 | + display_text="Ask a question", |
| 121 | + value="https://stackoverflow.com/questions/tagged/botframework" |
| 122 | + ), |
| 123 | + CardAction( |
| 124 | + type=ActionTypes.open_url, |
| 125 | + title="Learn how to deploy", |
| 126 | + text="Learn how to deploy", |
| 127 | + 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 | + ] |
| 131 | + ) |
| 132 | + |
| 133 | + return await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card))) |
0 commit comments