def payload_to_activity(payload: SlackPayload) -> Activity:
        """
        Creates an activity based on the Slack event payload.

        :param payload: The payload of the Slack event.
        :type payload: :class:`SlackPayload`
        :return: An activity containing the event data.
        :rtype: :class:`botbuilder.schema.Activity`
        """

        if not payload:
            raise Exception("payload is required")

        activity = Activity(
            channel_id="slack",
            conversation=ConversationAccount(id=payload.channel.id,
                                             properties={}),
            from_property=ChannelAccount(id=payload.message.bot_id if payload.
                                         message.bot_id else payload.user.id),
            recipient=ChannelAccount(),
            channel_data=payload,
            text=None,
            type=ActivityTypes.event,
        )

        if payload.thread_ts:
            activity.conversation.properties["thread_ts"] = payload.thread_ts

        if payload.actions and (payload.type == "block_actions"
                                or payload.type == "interactive_message"):
            activity.type = ActivityTypes.message
            activity.text = payload.actions.value

        return activity
    async def event_to_activity(event: SlackEvent,
                                client: SlackClient) -> Activity:
        """
        Creates an activity based on the Slack event data.

        :param event: The data of the Slack event.
        :type event: :class:`SlackEvent`
        :param client: The Slack client.
        :type client: :class:`SlackClient`
        :return: An activity containing the event data.
        :rtype: :class:`botbuilder.schema.Activity`
        """

        if not event:
            raise Exception("slack event is required")

        activity = Activity(
            id=event.event_ts,
            channel_id="slack",
            conversation=ConversationAccount(
                id=event.channel if event.channel else event.channel_id,
                properties={}),
            from_property=ChannelAccount(
                id=event.bot_id if event.bot_id else event.user_id if event.
                user_id else event.user),
            recipient=ChannelAccount(id=None),
            channel_data=event,
            text=event.text,
            type=ActivityTypes.event,
            timestamp=datetime.fromtimestamp(float(event.event_ts),
                                             tz=timezone.utc),
        )

        if event.thread_ts:
            activity.conversation.properties["thread_ts"] = event.thread_ts

        if not activity.conversation.id:
            if event.item and event.item_channel:
                activity.conversation.id = event.item_channel
            else:
                activity.conversation.id = event.team

        activity.recipient.id = await client.get_bot_user_by_team(
            activity=activity)

        # If this is a message originating from a user, we'll mark it as such
        # If this is a message from a bot (bot_id != None), we want to ignore it by
        # leaving the activity type as Event.  This will stop it from being included in dialogs,
        # but still allow the Bot to act on it if it chooses (via ActivityHandler.on_event_activity).
        # NOTE: This catches a message from ANY bot, including this bot.
        # Note also, bot_id here is not the same as bot_user_id so we can't (yet) identify messages
        # originating from this bot without doing an additional API call.
        if event.type == "message" and not event.subtype and not event.bot_id:
            activity.type = ActivityTypes.message

        return activity
 def activity_validator(activity: Activity) -> Activity:
     if not getattr(activity, "type", None):
         activity.type = ActivityTypes.message
     if activity.type != ActivityTypes.trace:
         nonlocal sent_non_trace_activity
         sent_non_trace_activity = True
     if not activity.input_hint:
         activity.input_hint = "acceptingInput"
     activity.id = None
     return activity
Exemple #4
0
    async def event_to_activity(event: SlackEvent,
                                client: SlackClient) -> Activity:
        """
        Creates an activity based on the Slack event data.

        :param event: The data of the Slack event.
        :type event: :class:`SlackEvent`
        :param client: The Slack client.
        :type client: :class:`SlackClient`
        :return: An activity containing the event data.
        :rtype: :class:`botbuilder.schema.Activity`
        """

        if not event:
            raise Exception("slack event is required")

        activity = Activity(
            id=event.event_ts,
            channel_id="slack",
            conversation=ConversationAccount(
                id=event.channel if event.channel else event.channel_id,
                properties={}),
            from_property=ChannelAccount(
                id=event.bot_id if event.bot_id else event.user_id),
            recipient=ChannelAccount(id=None),
            channel_data=event,
            text=event.text,
            type=ActivityTypes.event,
        )

        if not activity.conversation.id:
            if event.item and event.item_channel:
                activity.conversation.id = event.item_channel
            else:
                activity.conversation.id = event.team

        activity.recipient.id = await client.get_bot_user_identity(
            activity=activity)

        if event.thread_ts:
            activity.conversation.properties["thread_ts"] = event.thread_ts

        if event.type == "message" and not event.subtype and not event.bot_id:
            if not event.subtype:
                activity.type = ActivityTypes.message
                activity.text = event.text

            activity.conversation.properties[
                "channel_type"] = event.channel_type
            activity.value = event
        else:
            activity.name = event.type
            activity.value = event

        return activity
Exemple #5
0
    def payload_to_activity(payload: SlackPayload) -> Activity:
        """
        Creates an activity based on the Slack event payload.

        :param payload: The payload of the Slack event.
        :type payload: :class:`SlackPayload`
        :return: An activity containing the event data.
        :rtype: :class:`botbuilder.schema.Activity`
        """

        if not payload:
            raise Exception("payload is required")

        activity = Activity(
            channel_id="slack",
            conversation=ConversationAccount(id=payload.channel["id"],
                                             properties={}),
            from_property=ChannelAccount(
                id=payload.message.bot_id if payload.message.
                bot_id else payload.user["id"]),
            recipient=ChannelAccount(),
            channel_data=payload,
            text=None,
            type=ActivityTypes.event,
            value=payload,
        )

        if payload.thread_ts:
            activity.conversation.properties["thread_ts"] = payload.thread_ts

        if payload.actions:
            action = payload.actions[0]

            if action["type"] == "button":
                activity.text = action["value"]
            elif action["type"] == "select":
                selected_option = action["selected_options"]
                activity.text = selected_option[
                    "value"] if selected_option else None
            elif action["type"] == "static_select":
                activity.text = action["selected_options"]["value"]

            if activity.text:
                activity.type = ActivityTypes.message

        return activity
    async def process_activity(self, activity: Activity,
                               logic: Callable[[TurnContext], Awaitable]):
        self._conversation_lock.acquire()
        try:
            # ready for next reply
            if activity.type is None:
                activity.type = ActivityTypes.message

            activity.channel_id = self.template.channel_id
            activity.from_property = self.template.from_property
            activity.recipient = self.template.recipient
            activity.conversation = self.template.conversation
            activity.service_url = self.template.service_url

            activity.id = str((self._next_id))
            self._next_id += 1
        finally:
            self._conversation_lock.release()

        activity.timestamp = activity.timestamp or datetime.utcnow()
        await self.run_pipeline(TurnContext(self, activity), logic)
Exemple #7
0
    ConversationState,
    MemoryStorage,
    TurnContext,
    NullTelemetryClient,
)
from botbuilder.dialogs import (
    Dialog,
    DialogSet,
    WaterfallDialog,
    DialogTurnResult,
    DialogTurnStatus,
)

BEGIN_MESSAGE = Activity()
BEGIN_MESSAGE.text = "begin"
BEGIN_MESSAGE.type = "message"


class TelemetryWaterfallTests(aiounittest.AsyncTestCase):
    def test_none_telemetry_client(self):
        # arrange
        dialog = WaterfallDialog("myId")
        # act
        dialog.telemetry_client = None
        # assert
        self.assertEqual(type(dialog.telemetry_client), NullTelemetryClient)

    @patch("botbuilder.applicationinsights.ApplicationInsightsTelemetryClient")
    async def test_execute_sequence_waterfall_steps(self, MockTelemetry):  # pylint: disable=invalid-name
        # arrange
Exemple #8
0
            await step_context.context.send_activity("step2")
            return Dialog.end_of_turn

        async def Waterfall2_Step3(
                step_context: WaterfallStepContext) -> DialogTurnResult:
            await step_context.context.send_activity("step3")
            return Dialog.end_of_turn

        self.add_step(Waterfall2_Step1)
        self.add_step(Waterfall2_Step2)
        self.add_step(Waterfall2_Step3)


begin_message = Activity()
begin_message.text = 'begin'
begin_message.type = 'message'


class WaterfallTests(aiounittest.AsyncTestCase):
    def test_waterfall_none_name(self):
        self.assertRaises(TypeError, (lambda: WaterfallDialog(None)))

    def test_watterfall_add_none_step(self):
        waterfall = WaterfallDialog("test")
        self.assertRaises(TypeError, (lambda: waterfall.add_step(None)))

    async def test_waterfall_with_set_instead_of_array(self):
        self.assertRaises(TypeError, lambda: WaterfallDialog('a', {1, 2}))

    # TODO:WORK IN PROGRESS
    async def test_execute_sequence_waterfall_steps(self):