Example #1
0
    def test_basic_json(self):
        self.assertDictEqual(
            Attachment(text="some text").to_dict(), {"text": "some text", "fields": []}
        )

        self.assertDictEqual(
            Attachment(
                text="attachment text",
                title="Attachment",
                fallback="fallback_text",
                pretext="some_pretext",
                title_link="link in title",
                fields=[
                    AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value")
                    for i in range(5)
                ],
                color="#FFFF00",
                author_name="John Doe",
                author_link="http://johndoeisthebest.com",
                author_icon="http://johndoeisthebest.com/avatar.jpg",
                thumb_url="thumbnail URL",
                footer="and a footer",
                footer_icon="link to footer icon",
                ts=123456789,
                markdown_in=["fields"],
            ).to_dict(),
            {
                "text": "attachment text",
                "author_name": "John Doe",
                "author_link": "http://johndoeisthebest.com",
                "author_icon": "http://johndoeisthebest.com/avatar.jpg",
                "footer": "and a footer",
                "title": "Attachment",
                "footer_icon": "link to footer icon",
                "pretext": "some_pretext",
                "ts": 123456789,
                "fallback": "fallback_text",
                "title_link": "link in title",
                "color": "#FFFF00",
                "thumb_url": "thumbnail URL",
                "fields": [
                    {"title": "field_0_title", "value": "field_0_value", "short": True},
                    {"title": "field_1_title", "value": "field_1_value", "short": True},
                    {"title": "field_2_title", "value": "field_2_value", "short": True},
                    {"title": "field_3_title", "value": "field_3_value", "short": True},
                    {"title": "field_4_title", "value": "field_4_value", "short": True},
                ],
                "mrkdwn_in": ["fields"],
            },
        )
 async def test_with_attachments(self):
     url = os.environ[SLACK_SDK_TEST_INCOMING_WEBHOOK_URL]
     webhook = AsyncWebhookClient(url)
     response = await webhook.send(
         text="fallback",
         attachments=[
             Attachment(
                 text="attachment text",
                 title="Attachment",
                 fallback="fallback_text",
                 pretext="some_pretext",
                 title_link="link in title",
                 fields=[
                     AttachmentField(title=f"field_{i}_title",
                                     value=f"field_{i}_value")
                     for i in range(5)
                 ],
                 color="#FFFF00",
                 author_name="John Doe",
                 author_link="http://johndoeisthebest.com",
                 author_icon="http://johndoeisthebest.com/avatar.jpg",
                 thumb_url="thumbnail URL",
                 footer="and a footer",
                 footer_icon="link to footer icon",
                 ts=123456789,
                 markdown_in=["fields"],
             )
         ])
     self.assertEqual(200, response.status_code)
     self.assertEqual("ok", response.body)
    def __init__(self, **kwargs):
        self.ephemeral = kwargs.get("ephemeral")
        self.as_user = kwargs.get("as_user")
        self.icon_url = kwargs.get("icon_url")
        self.icon_emoji = kwargs.get("icon_emoji")
        self.thread_ts = kwargs.get("thread_ts")
        self.user = kwargs.get("user")
        self.channel = kwargs.get("channel")
        self.text = kwargs.get("text")
        self.team = kwargs.get("team")
        self.ts = kwargs.get("ts")  # pylint: disable=invalid-name
        self.username = kwargs.get("username")
        self.bot_id = kwargs.get("bot_id")
        self.icons = kwargs.get("icons")
        self.blocks: [Block] = kwargs.get("blocks")

        # Create proper Attachment objects
        # It would appear that we can get dict fields from the wire that aren't defined
        # in the Attachment class.  So only pass in known fields.
        attachments = kwargs.get("attachments")
        if attachments is not None:
            self.attachments = [
                Attachment(
                    **{x: att[x]
                       for x in att if x in Attachment.attributes})
                for att in kwargs.get("attachments")
            ]
Example #4
0
    def activity_to_slack(activity: Activity) -> SlackMessage:
        """
        Formats a BotBuilder Activity into an outgoing Slack message.

        :param activity: A BotBuilder Activity object.
        :type activity: :class:`botbuilder.schema.Activity`
        :return: A Slack message object with {text, attachments, channel, thread ts} and any fields found in
         activity.channelData.
        :rtype: :class:`SlackMessage`
        """

        if not activity:
            raise Exception("Activity required")

        # use ChannelData if available
        if activity.channel_data:
            message = activity.channel_data
        else:
            message = SlackMessage(
                ts=activity.timestamp,
                text=activity.text,
                channel=activity.conversation.id,
            )

            if activity.attachments:
                attachments = []
                for att in activity.attachments:
                    if att.name == "blocks":
                        message.blocks = att.content
                    else:
                        new_attachment = Attachment(
                            author_name=att.name,
                            thumb_url=att.thumbnail_url,
                            text="",
                        )
                        attachments.append(new_attachment)

                if attachments:
                    message.attachments = attachments

            if (activity.conversation.properties
                    and "thread_ts" in activity.conversation.properties):
                message.thread_ts = activity.conversation.properties[
                    "thread_ts"]

        if message.ephemeral:
            message.user = activity.recipient.id

        if (message.icon_url
                or not (message.icons and message.icons.status_emoji)
                or not message.username):
            message.as_user = False

        return message
    def test_send_attachments(self):
        client = WebhookClient("http://localhost:8888")

        resp = client.send(
            text="hello!",
            response_type="ephemeral",
            attachments=[
                {
                    "color": "#f2c744",
                    "blocks": [
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>",
                            },
                        },
                        {"type": "divider"},
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "Pick a date for the deadline.",
                            },
                            "accessory": {
                                "type": "datepicker",
                                "initial_date": "1990-04-28",
                                "placeholder": {
                                    "type": "plain_text",
                                    "text": "Select a date",
                                },
                            },
                        },
                    ],
                }
            ],
        )
        self.assertEqual("ok", resp.body)

        resp = client.send(
            text="hello!",
            response_type="ephemeral",
            attachments=[
                Attachment(
                    text="attachment text",
                    title="Attachment",
                    fallback="fallback_text",
                    pretext="some_pretext",
                    title_link="link in title",
                    fields=[
                        AttachmentField(
                            title=f"field_{i}_title", value=f"field_{i}_value"
                        )
                        for i in range(5)
                    ],
                    color="#FFFF00",
                    author_name="John Doe",
                    author_link="http://johndoeisthebest.com",
                    author_icon="http://johndoeisthebest.com/avatar.jpg",
                    thumb_url="thumbnail URL",
                    footer="and a footer",
                    footer_icon="link to footer icon",
                    ts=123456789,
                    markdown_in=["fields"],
                )
            ],
        )
        self.assertEqual("ok", resp.body)
Example #6
0
 def setUp(self) -> None:
     self.simple = Attachment(text="some_text")
Example #7
0
class AttachmentTests(unittest.TestCase):
    def setUp(self) -> None:
        self.simple = Attachment(text="some_text")

    def test_basic_json(self):
        self.assertDictEqual(
            Attachment(text="some text").to_dict(), {"text": "some text", "fields": []}
        )

        self.assertDictEqual(
            Attachment(
                text="attachment text",
                title="Attachment",
                fallback="fallback_text",
                pretext="some_pretext",
                title_link="link in title",
                fields=[
                    AttachmentField(title=f"field_{i}_title", value=f"field_{i}_value")
                    for i in range(5)
                ],
                color="#FFFF00",
                author_name="John Doe",
                author_link="http://johndoeisthebest.com",
                author_icon="http://johndoeisthebest.com/avatar.jpg",
                thumb_url="thumbnail URL",
                footer="and a footer",
                footer_icon="link to footer icon",
                ts=123456789,
                markdown_in=["fields"],
            ).to_dict(),
            {
                "text": "attachment text",
                "author_name": "John Doe",
                "author_link": "http://johndoeisthebest.com",
                "author_icon": "http://johndoeisthebest.com/avatar.jpg",
                "footer": "and a footer",
                "title": "Attachment",
                "footer_icon": "link to footer icon",
                "pretext": "some_pretext",
                "ts": 123456789,
                "fallback": "fallback_text",
                "title_link": "link in title",
                "color": "#FFFF00",
                "thumb_url": "thumbnail URL",
                "fields": [
                    {"title": "field_0_title", "value": "field_0_value", "short": True},
                    {"title": "field_1_title", "value": "field_1_value", "short": True},
                    {"title": "field_2_title", "value": "field_2_value", "short": True},
                    {"title": "field_3_title", "value": "field_3_value", "short": True},
                    {"title": "field_4_title", "value": "field_4_value", "short": True},
                ],
                "mrkdwn_in": ["fields"],
            },
        )

    def test_footer_length(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.footer = STRING_301_CHARS
            self.simple.to_dict()

    def test_ts_without_footer(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.ts = 123456789
            self.simple.to_dict()

    def test_markdown_in_invalid(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.markdown_in = ["nothing"]
            self.simple.to_dict()

    def test_color_valid(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.color = "red"
            self.simple.to_dict()

        with self.assertRaises(SlackObjectFormationError):
            self.simple.color = "#ZZZZZZ"
            self.simple.to_dict()

        self.simple.color = "#bada55"
        self.assertEqual(self.simple.to_dict()["color"], "#bada55")

        self.simple.color = "good"
        self.assertEqual(self.simple.to_dict()["color"], "good")

    def test_image_url_and_thumb_url(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.thumb_url = "some URL"
            self.simple.image_url = "some URL"
            self.simple.to_dict()

        self.simple.image_url = None
        self.simple.to_dict()

    def author_name_without_author_link(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.author_name = "http://google.com"
            self.simple.to_dict()

        self.simple.author_name = None
        self.simple.to_dict()

    def author_icon_without_author_name(self):
        with self.assertRaises(SlackObjectFormationError):
            self.simple.author_icon = "http://google.com/images.jpg"
            self.simple.to_dict()

        self.simple.author_icon = None
        self.simple.to_dict()