|
| 1 | +# Copyright (c) Microsoft Corp. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from typing import List |
| 5 | +import random |
| 6 | +from botbuilder.core import ( |
| 7 | + CardFactory, |
| 8 | + MessageFactory, |
| 9 | + TurnContext, |
| 10 | +) |
| 11 | +from botbuilder.schema import Attachment |
| 12 | +from botbuilder.schema.teams import ( |
| 13 | + MessagingExtensionAction, |
| 14 | + MessagingExtensionActionResponse, |
| 15 | + TaskModuleContinueResponse, |
| 16 | + MessagingExtensionResult, |
| 17 | + TaskModuleTaskInfo, |
| 18 | +) |
| 19 | +from botbuilder.core.teams import TeamsActivityHandler |
| 20 | +from example_data import ExampleData |
| 21 | + |
| 22 | + |
| 23 | +class ActionBasedMessagingExtensionFetchTaskBot(TeamsActivityHandler): |
| 24 | + async def on_message_activity(self, turn_context: TurnContext): |
| 25 | + value = turn_context.activity.value |
| 26 | + if value is not None: |
| 27 | + # This was a message from the card. |
| 28 | + answer = value["Answer"] |
| 29 | + choices = value["Choices"] |
| 30 | + reply = MessageFactory.text( |
| 31 | + f"{turn_context.activity.from_property.name} answered '{answer}' and chose '{choices}'." |
| 32 | + ) |
| 33 | + await turn_context.send_activity(reply) |
| 34 | + else: |
| 35 | + # This is a regular text message. |
| 36 | + reply = MessageFactory.text( |
| 37 | + "Hello from ActionBasedMessagingExtensionFetchTaskBot." |
| 38 | + ) |
| 39 | + await turn_context.send_activity(reply) |
| 40 | + |
| 41 | + async def on_teams_messaging_extension_fetch_task( |
| 42 | + self, turn_context: TurnContext, action: MessagingExtensionAction |
| 43 | + ) -> MessagingExtensionActionResponse: |
| 44 | + card = self._create_adaptive_card_editor() |
| 45 | + task_info = TaskModuleTaskInfo( |
| 46 | + card=card, height=450, title="Task Module Fetch Example", width=500 |
| 47 | + ) |
| 48 | + continue_response = TaskModuleContinueResponse(type="continue", value=task_info) |
| 49 | + return MessagingExtensionActionResponse(task=continue_response) |
| 50 | + |
| 51 | + async def on_teams_messaging_extension_submit_action( # pylint: disable=unused-argument |
| 52 | + self, turn_context: TurnContext, action: MessagingExtensionAction |
| 53 | + ) -> MessagingExtensionActionResponse: |
| 54 | + question = action.data["Question"] |
| 55 | + multi_select = action.data["MultiSelect"] |
| 56 | + option1 = action.data["Option1"] |
| 57 | + option2 = action.data["Option2"] |
| 58 | + option3 = action.data["Option3"] |
| 59 | + preview_card = self._create_adaptive_card_preview( |
| 60 | + user_text=question, |
| 61 | + is_multi_select=multi_select, |
| 62 | + option1=option1, |
| 63 | + option2=option2, |
| 64 | + option3=option3, |
| 65 | + ) |
| 66 | + |
| 67 | + extension_result = MessagingExtensionResult( |
| 68 | + type="botMessagePreview", |
| 69 | + activity_preview=MessageFactory.attachment(preview_card), |
| 70 | + ) |
| 71 | + return MessagingExtensionActionResponse(compose_extension=extension_result) |
| 72 | + |
| 73 | + async def on_teams_messaging_extension_bot_message_preview_edit( # pylint: disable=unused-argument |
| 74 | + self, turn_context: TurnContext, action: MessagingExtensionAction |
| 75 | + ) -> MessagingExtensionActionResponse: |
| 76 | + activity_preview = action.bot_activity_preview[0] |
| 77 | + content = activity_preview.attachments[0].content |
| 78 | + data = self._get_example_data(content) |
| 79 | + card = self._create_adaptive_card_editor( |
| 80 | + data.question, |
| 81 | + data.is_multi_select, |
| 82 | + data.option1, |
| 83 | + data.option2, |
| 84 | + data.option3, |
| 85 | + ) |
| 86 | + task_info = TaskModuleTaskInfo( |
| 87 | + card=card, height=450, title="Task Module Fetch Example", width=500 |
| 88 | + ) |
| 89 | + continue_response = TaskModuleContinueResponse(type="continue", value=task_info) |
| 90 | + return MessagingExtensionActionResponse(task=continue_response) |
| 91 | + |
| 92 | + async def on_teams_messaging_extension_bot_message_preview_send( # pylint: disable=unused-argument |
| 93 | + self, turn_context: TurnContext, action: MessagingExtensionAction |
| 94 | + ) -> MessagingExtensionActionResponse: |
| 95 | + activity_preview = action.bot_activity_preview[0] |
| 96 | + content = activity_preview.attachments[0].content |
| 97 | + data = self._get_example_data(content) |
| 98 | + card = self._create_adaptive_card_preview( |
| 99 | + data.question, |
| 100 | + data.is_multi_select, |
| 101 | + data.option1, |
| 102 | + data.option2, |
| 103 | + data.option3, |
| 104 | + ) |
| 105 | + message = MessageFactory.attachment(card) |
| 106 | + await turn_context.send_activity(message) |
| 107 | + |
| 108 | + def _get_example_data(self, content: dict) -> ExampleData: |
| 109 | + body = content["body"] |
| 110 | + question = body[1]["text"] |
| 111 | + choice_set = body[3] |
| 112 | + multi_select = "isMultiSelect" in choice_set |
| 113 | + option1 = choice_set["choices"][0]["value"] |
| 114 | + option2 = choice_set["choices"][1]["value"] |
| 115 | + option3 = choice_set["choices"][2]["value"] |
| 116 | + return ExampleData(question, multi_select, option1, option2, option3) |
| 117 | + |
| 118 | + def _create_adaptive_card_editor( |
| 119 | + self, |
| 120 | + user_text: str = None, |
| 121 | + is_multi_select: bool = False, |
| 122 | + option1: str = None, |
| 123 | + option2: str = None, |
| 124 | + option3: str = None, |
| 125 | + ) -> Attachment: |
| 126 | + return CardFactory.adaptive_card( |
| 127 | + { |
| 128 | + "actions": [ |
| 129 | + { |
| 130 | + "data": {"submitLocation": "messagingExtensionFetchTask"}, |
| 131 | + "title": "Submit", |
| 132 | + "type": "Action.Submit", |
| 133 | + } |
| 134 | + ], |
| 135 | + "body": [ |
| 136 | + { |
| 137 | + "text": "This is an Adaptive Card within a Task Module", |
| 138 | + "type": "TextBlock", |
| 139 | + "weight": "bolder", |
| 140 | + }, |
| 141 | + {"type": "TextBlock", "text": "Enter text for Question:"}, |
| 142 | + { |
| 143 | + "id": "Question", |
| 144 | + "placeholder": "Question text here", |
| 145 | + "type": "Input.Text", |
| 146 | + "value": user_text, |
| 147 | + }, |
| 148 | + {"type": "TextBlock", "text": "Options for Question:"}, |
| 149 | + {"type": "TextBlock", "text": "Is Multi-Select:"}, |
| 150 | + { |
| 151 | + "choices": [ |
| 152 | + {"title": "True", "value": "true"}, |
| 153 | + {"title": "False", "value": "false"}, |
| 154 | + ], |
| 155 | + "id": "MultiSelect", |
| 156 | + "isMultiSelect": "false", |
| 157 | + "style": "expanded", |
| 158 | + "type": "Input.ChoiceSet", |
| 159 | + "value": "true" if is_multi_select else "false", |
| 160 | + }, |
| 161 | + { |
| 162 | + "id": "Option1", |
| 163 | + "placeholder": "Option 1 here", |
| 164 | + "type": "Input.Text", |
| 165 | + "value": option1, |
| 166 | + }, |
| 167 | + { |
| 168 | + "id": "Option2", |
| 169 | + "placeholder": "Option 2 here", |
| 170 | + "type": "Input.Text", |
| 171 | + "value": option2, |
| 172 | + }, |
| 173 | + { |
| 174 | + "id": "Option3", |
| 175 | + "placeholder": "Option 3 here", |
| 176 | + "type": "Input.Text", |
| 177 | + "value": option3, |
| 178 | + }, |
| 179 | + ], |
| 180 | + "type": "AdaptiveCard", |
| 181 | + "version": "1.0", |
| 182 | + } |
| 183 | + ) |
| 184 | + |
| 185 | + def _create_adaptive_card_preview( |
| 186 | + self, |
| 187 | + user_text: str = None, |
| 188 | + is_multi_select: bool = False, |
| 189 | + option1: str = None, |
| 190 | + option2: str = None, |
| 191 | + option3: str = None, |
| 192 | + ) -> Attachment: |
| 193 | + return CardFactory.adaptive_card( |
| 194 | + { |
| 195 | + "actions": [ |
| 196 | + { |
| 197 | + "type": "Action.Submit", |
| 198 | + "title": "Submit", |
| 199 | + "data": {"submitLocation": "messagingExtensionSubmit"}, |
| 200 | + } |
| 201 | + ], |
| 202 | + "body": [ |
| 203 | + { |
| 204 | + "text": "Adaptive Card from Task Module", |
| 205 | + "type": "TextBlock", |
| 206 | + "weight": "bolder", |
| 207 | + }, |
| 208 | + {"text": user_text, "type": "TextBlock", "id": "Question"}, |
| 209 | + { |
| 210 | + "id": "Answer", |
| 211 | + "placeholder": "Answer here...", |
| 212 | + "type": "Input.Text", |
| 213 | + }, |
| 214 | + { |
| 215 | + "choices": [ |
| 216 | + {"title": option1, "value": option1}, |
| 217 | + {"title": option2, "value": option2}, |
| 218 | + {"title": option3, "value": option3}, |
| 219 | + ], |
| 220 | + "id": "Choices", |
| 221 | + "isMultiSelect": is_multi_select, |
| 222 | + "style": "expanded", |
| 223 | + "type": "Input.ChoiceSet", |
| 224 | + }, |
| 225 | + ], |
| 226 | + "type": "AdaptiveCard", |
| 227 | + "version": "1.0", |
| 228 | + } |
| 229 | + ) |
0 commit comments