|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +# pylint: disable=too-many-lines |
| 5 | + |
| 6 | +from http import HTTPStatus |
| 7 | +from botbuilder.core import ActivityHandler, InvokeResponse |
| 8 | +from botbuilder.core.activity_handler import _InvokeResponseException |
| 9 | +from botbuilder.core.turn_context import TurnContext |
| 10 | +from botbuilder.schema.sharepoint import ( |
| 11 | + AceRequest, |
| 12 | + BaseHandleActionResponse, |
| 13 | + CardViewResponse, |
| 14 | + GetPropertyPaneConfigurationResponse, |
| 15 | + QuickViewHandleActionResponse, |
| 16 | + QuickViewResponse, |
| 17 | +) |
| 18 | +from ..serializer_helper import deserializer_helper |
| 19 | + |
| 20 | + |
| 21 | +class SharePointActivityHandler(ActivityHandler): |
| 22 | + """ |
| 23 | + The SharePointActivityHandler is derived from ActivityHandler. It adds support for |
| 24 | + SharePoint-specific events and interactions. |
| 25 | + """ |
| 26 | + |
| 27 | + async def on_invoke_activity(self, turn_context: TurnContext) -> InvokeResponse: |
| 28 | + """ |
| 29 | + Invoked when an invoke activity is received from the connector. |
| 30 | + Invoke activities can be used to communicate many different things. |
| 31 | +
|
| 32 | + :param turn_context: A context object for this turn. |
| 33 | +
|
| 34 | + :returns: An InvokeResponse that represents the work queued to execute. |
| 35 | +
|
| 36 | + .. remarks:: |
| 37 | + Invoke activities communicate programmatic commands from a client or channel to a bot. |
| 38 | + The meaning of an invoke activity is defined by the "invoke_activity.name" property, |
| 39 | + which is meaningful within the scope of a channel. |
| 40 | + """ |
| 41 | + try: |
| 42 | + if not turn_context.activity.name: |
| 43 | + raise NotImplementedError() |
| 44 | + |
| 45 | + if turn_context.activity.name == "cardExtension/getCardView": |
| 46 | + print("Printing AceReq", turn_context.activity.value) |
| 47 | + return self._create_invoke_response( |
| 48 | + await self.on_sharepoint_task_get_card_view( |
| 49 | + turn_context, |
| 50 | + deserializer_helper(AceRequest, turn_context.activity.value), |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + if turn_context.activity.name == "cardExtension/getQuickView": |
| 55 | + return self._create_invoke_response( |
| 56 | + await self.on_sharepoint_task_get_quick_view( |
| 57 | + turn_context, |
| 58 | + deserializer_helper(AceRequest, turn_context.activity.value), |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + if ( |
| 63 | + turn_context.activity.name |
| 64 | + == "cardExtension/getPropertyPaneConfiguration" |
| 65 | + ): |
| 66 | + return self._create_invoke_response( |
| 67 | + await self.on_sharepoint_task_get_property_pane_configuration( |
| 68 | + turn_context, |
| 69 | + deserializer_helper(AceRequest, turn_context.activity.value), |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + if ( |
| 74 | + turn_context.activity.name |
| 75 | + == "cardExtension/setPropertyPaneConfiguration" |
| 76 | + ): |
| 77 | + ace_request = deserializer_helper( |
| 78 | + AceRequest, turn_context.activity.value |
| 79 | + ) |
| 80 | + set_prop_pane_config_response = ( |
| 81 | + await self.on_sharepoint_task_set_property_pane_configuration( |
| 82 | + turn_context, ace_request |
| 83 | + ) |
| 84 | + ) |
| 85 | + self.validate_set_property_pane_configuration_response( |
| 86 | + set_prop_pane_config_response |
| 87 | + ) |
| 88 | + return self._create_invoke_response(set_prop_pane_config_response) |
| 89 | + |
| 90 | + if turn_context.activity.name == "cardExtension/handleAction": |
| 91 | + return self._create_invoke_response( |
| 92 | + await self.on_sharepoint_task_handle_action( |
| 93 | + turn_context, |
| 94 | + deserializer_helper(AceRequest, turn_context.activity.value), |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + if turn_context.activity.name == "cardExtension/token": |
| 99 | + await self.on_sign_in_invoke(turn_context) |
| 100 | + return self._create_invoke_response() |
| 101 | + |
| 102 | + except _InvokeResponseException as invoke_exception: |
| 103 | + return invoke_exception.create_invoke_response() |
| 104 | + return await super().on_invoke_activity(turn_context) |
| 105 | + |
| 106 | + async def on_sharepoint_task_get_card_view( |
| 107 | + self, turn_context: TurnContext, ace_request: AceRequest |
| 108 | + ) -> CardViewResponse: |
| 109 | + """ |
| 110 | + Override this in a derived class to provide logic for when a card view is fetched. |
| 111 | +
|
| 112 | + :param turn_context: A context object for this turn. |
| 113 | + :param ace_request: The ACE invoke request value payload. |
| 114 | + :returns: A Card View Response for the request. |
| 115 | + """ |
| 116 | + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) |
| 117 | + |
| 118 | + async def on_sharepoint_task_get_quick_view( |
| 119 | + self, turn_context: TurnContext, ace_request: AceRequest |
| 120 | + ) -> QuickViewResponse: |
| 121 | + """ |
| 122 | + Override this in a derived class to provide logic for when a quick view is fetched. |
| 123 | +
|
| 124 | + :param turn_context: A strongly-typed context object for this turn. |
| 125 | + :param ace_request: The ACE invoke request value payload. |
| 126 | + :returns: A Quick View Response for the request |
| 127 | + """ |
| 128 | + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) |
| 129 | + |
| 130 | + async def on_sharepoint_task_get_property_pane_configuration( |
| 131 | + self, turn_context: TurnContext, ace_request: AceRequest |
| 132 | + ) -> GetPropertyPaneConfigurationResponse: |
| 133 | + """ |
| 134 | + Override this in a derived class to provide logic for getting configuration pane properties. |
| 135 | +
|
| 136 | + :param turn_context: A strongly-typed context object for this turn. |
| 137 | + :param ace_request: The ACE invoke request value payload. |
| 138 | + :returns: A Property Pane Configuration Response for the request. |
| 139 | + """ |
| 140 | + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) |
| 141 | + |
| 142 | + async def on_sharepoint_task_set_property_pane_configuration( |
| 143 | + self, turn_context: TurnContext, ace_request: AceRequest |
| 144 | + ) -> BaseHandleActionResponse: |
| 145 | + """ |
| 146 | + Override this in a derived class to provide logic for setting configuration pane properties. |
| 147 | +
|
| 148 | + :param turn_context: A strongly-typed context object for this turn. |
| 149 | + :param ace_request: The ACE invoke request value payload. |
| 150 | + :returns: Card view or no-op action response. |
| 151 | + """ |
| 152 | + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) |
| 153 | + |
| 154 | + async def on_sharepoint_task_handle_action( |
| 155 | + self, turn_context: TurnContext, ace_request: AceRequest |
| 156 | + ) -> BaseHandleActionResponse: |
| 157 | + """ |
| 158 | + Override this in a derived class to provide logic for handling ACE actions. |
| 159 | +
|
| 160 | + :param turn_context: A strongly-typed context object for this turn. |
| 161 | + :param ace_request: The ACE invoke request value payload. |
| 162 | + :returns: A handle action response.. |
| 163 | + """ |
| 164 | + raise _InvokeResponseException(status_code=HTTPStatus.NOT_IMPLEMENTED) |
| 165 | + |
| 166 | + def validate_set_property_pane_configuration_response( |
| 167 | + self, response: BaseHandleActionResponse |
| 168 | + ): |
| 169 | + """ |
| 170 | + Validates the response for SetPropertyPaneConfiguration action. |
| 171 | +
|
| 172 | + :param response: The response object. |
| 173 | + :raises ValueError: If response is of type QuickViewHandleActionResponse. |
| 174 | + """ |
| 175 | + if isinstance(response, QuickViewHandleActionResponse): |
| 176 | + raise _InvokeResponseException( |
| 177 | + HTTPStatus.INTERNAL_SERVER_ERROR, |
| 178 | + "Response for SetPropertyPaneConfiguration action can't be of QuickView type.", |
| 179 | + ) |
0 commit comments