|
| 1 | +# Copyright (c) Microsoft Corp. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import json |
| 5 | +from typing import List |
| 6 | +import random |
| 7 | +from botbuilder.core import CardFactory, MessageFactory, TurnContext |
| 8 | +from botbuilder.schema import ( |
| 9 | + ChannelAccount, |
| 10 | + HeroCard, |
| 11 | + CardAction, |
| 12 | + CardImage, |
| 13 | + Attachment, |
| 14 | +) |
| 15 | +from botbuilder.schema.teams import ( |
| 16 | + MessagingExtensionAction, |
| 17 | + MessagingExtensionActionResponse, |
| 18 | + MessagingExtensionAttachment, |
| 19 | + MessagingExtensionResult, |
| 20 | + TaskModuleResponse, |
| 21 | + TaskModuleResponseBase, |
| 22 | + TaskModuleContinueResponse, |
| 23 | + TaskModuleMessageResponse, |
| 24 | + TaskModuleTaskInfo, |
| 25 | + TaskModuleRequest, |
| 26 | +) |
| 27 | +from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo |
| 28 | +from botbuilder.azure import CosmosDbPartitionedStorage |
| 29 | +from botbuilder.core.teams.teams_helper import deserializer_helper |
| 30 | + |
| 31 | +class TaskModuleBot(TeamsActivityHandler): |
| 32 | + async def on_message_activity(self, turn_context: TurnContext): |
| 33 | + reply = MessageFactory.attachment(self._get_task_module_hero_card()) |
| 34 | + await turn_context.send_activity(reply) |
| 35 | + |
| 36 | + def _get_task_module_hero_card(self) -> Attachment: |
| 37 | + task_module_action = CardAction( |
| 38 | + type="invoke", |
| 39 | + title="Adaptive Card", |
| 40 | + value={"type": "task/fetch", "data": "adaptivecard"}, |
| 41 | + ) |
| 42 | + card = HeroCard( |
| 43 | + title="Task Module Invocation from Hero Card", |
| 44 | + subtitle="This is a hero card with a Task Module Action button. Click the button to show an Adaptive Card within a Task Module.", |
| 45 | + buttons=[task_module_action], |
| 46 | + ) |
| 47 | + return CardFactory.hero_card(card) |
| 48 | + |
| 49 | + async def on_teams_task_module_fetch( |
| 50 | + self, turn_context: TurnContext, task_module_request: TaskModuleRequest |
| 51 | + ) -> TaskModuleResponse: |
| 52 | + reply = MessageFactory.text( |
| 53 | + f"OnTeamsTaskModuleFetchAsync TaskModuleRequest: {json.dumps(task_module_request.data)}" |
| 54 | + ) |
| 55 | + await turn_context.send_activity(reply) |
| 56 | + |
| 57 | + # base_response = TaskModuleResponseBase(type='continue') |
| 58 | + card = CardFactory.adaptive_card( |
| 59 | + { |
| 60 | + "version": "1.0.0", |
| 61 | + "type": "AdaptiveCard", |
| 62 | + "body": [ |
| 63 | + {"type": "TextBlock", "text": "Enter Text Here",}, |
| 64 | + { |
| 65 | + "type": "Input.Text", |
| 66 | + "id": "usertext", |
| 67 | + "placeholder": "add some text and submit", |
| 68 | + "IsMultiline": "true", |
| 69 | + }, |
| 70 | + ], |
| 71 | + "actions": [{"type": "Action.Submit", "title": "Submit",}], |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + task_info = TaskModuleTaskInfo( |
| 76 | + card=card, title="Adaptive Card: Inputs", height=200, width=400 |
| 77 | + ) |
| 78 | + continue_response = TaskModuleContinueResponse(type="continue", value=task_info) |
| 79 | + return TaskModuleResponse(task=continue_response) |
| 80 | + |
| 81 | + async def on_teams_task_module_submit( |
| 82 | + self, turn_context: TurnContext, task_module_request: TaskModuleRequest |
| 83 | + ) -> TaskModuleResponse: |
| 84 | + reply = MessageFactory.text( |
| 85 | + f"on_teams_messaging_extension_submit_action_activity MessagingExtensionAction: {json.dumps(task_module_request.data)}" |
| 86 | + ) |
| 87 | + await turn_context.send_activity(reply) |
| 88 | + |
| 89 | + message_response = TaskModuleMessageResponse(type="message", value="Thanks!") |
| 90 | + return TaskModuleResponse(task=message_response) |
0 commit comments