예제 #1
0
    def set_photo(
        self,
        crop_w: int = None,
        crop_x: int = None,
        crop_y: int = None,
        image: Union[str, PathLike, IO] = None,
        **kwargs
    ) -> Response:
        """
        Set the user profile photo
        https://api.slack.com/methods/users.setPhoto

        :param token: Authentication token bearing required scopes.
        :type str: e.g. xxxx-xxxxxxxxx-xxxx

        :param crop_w: Width/height of crop box (always square)
        :type int: e.g. 100

        :param crop_x: X coordinate of top-left corner of crop box
        :type int: e.g. 10

        :param crop_y: Y coordinate of top-left corner of crop box
        :type int: e.g. 15

        :param image: File contents via multipart/form-data.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file"

        :returns response:
        :type requests.Response: e.g. <Response [200]>

        example:
        >>> client = SlackTime(token='insert-your-token-here')
        >>> response = client.users.set_photo(**your_params)
        <Response [200]>
        """

        payload = {"token": self._token}

        if crop_w is not None:
            payload["crop_w"] = crop_w

        if crop_x is not None:
            payload["crop_x"] = crop_x

        if crop_y is not None:
            payload["crop_y"] = crop_y

        if image is not None:
            file_to_upload = make_file(image)
            kwargs["files"] = {"file": file_to_upload}

        return self._post("users.setPhoto", payload=payload, **kwargs)
예제 #2
0
def test_make_file_with_io(temp_file):
    f = make_file(open(temp_file, "rb"))
    assert isinstance(f, io.BufferedReader)
    assert f.read().decode() == TEXT
예제 #3
0
def test_make_file_with_os_pathlike(temp_file):
    f = make_file(temp_file)
    assert isinstance(f, io.BufferedReader)
    assert f.read().decode() == TEXT
예제 #4
0
def test_make_file_with_str(temp_file):
    f = make_file(FILENAME)
    assert isinstance(f, io.BufferedReader)
    assert f.read().decode() == TEXT
예제 #5
0
    def upload(
        self,
        channels: Union[str, Iterable] = None,
        content: Union[str, PathLike, IO] = None,
        file: Union[str, PathLike, IO] = None,
        filename: str = None,
        filetype: str = None,
        initial_comment: str = None,
        thread_ts: float = None,
        title: str = None,
        **kwargs
    ) -> Response:
        """
        Uploads or creates a file.
        https://api.slack.com/methods/files.upload

        :param token: Authentication token bearing required scopes.
        :type str: e.g. xxxx-xxxxxxxxx-xxxx

        :param channels: Comma-separated list of channel names or IDs where the file will be shared.
        :type Union[str, Iterable]: e.g. C1234567890,C2345678901,C3456789012

        :param content: File contents via a POST variable. If omitting this parameter, you must provide a file.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file

        :param file: File contents via multipart/form-data. If omitting this parameter, you must submit content.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file

        :param filename: Filename of file.
        :type str: e.g. foo.txt

        :param filetype: A file type identifier.
        :type str: e.g. php

        :param initial_comment: The message text introducing the file in specified channels.
        :type str: e.g. Best!

        :param thread_ts: Provide another message's ts value to upload this file as a reply. Never use a reply's ts value; use its parent instead.
        :type float: e.g. 1234567890.123456

        :param title: Title of file.
        :type str: e.g. My File

        :returns response:
        :type requests.Response: e.g. <Response [200]>

        example:
        >>> client = SlackTime(token='insert-your-token-here')
        >>> response = client.files.upload(**your_params)
        <Response [200]>
        >>> response.json()
        {
            "ok": true,
            "file": {
                "id": "F0TD00400",
                "created": 1532293501,
                "timestamp": 1532293501,
                "name": "dramacat.gif",
                "title": "dramacat",
                "mimetype": "image/jpeg",
                "filetype": "gif",
                "pretty_type": "JPEG",
                "user": "******",
                "editable": false,
                "size": 43518,
                "mode": "hosted",
                "is_external": false,
                "external_type": "",
                "is_public": false,
                "public_url_shared": false,
                "display_as_bot": false,
                "username": "",
                "url_private": "https://.../dramacat.gif",
                "url_private_download": "https://.../dramacat.gif",
                "thumb_64": "https://.../dramacat_64.gif",
                "thumb_80": "https://.../dramacat_80.gif",
                "thumb_360": "https://.../dramacat_360.gif",
                "thumb_360_w": 360,
                "thumb_360_h": 250,
                "thumb_480": "https://.../dramacat_480.gif",
                "thumb_480_w": 480,
                "thumb_480_h": 334,
                "thumb_160": "https://.../dramacat_160.gif",
                "image_exif_rotation": 1,
                "original_w": 526,
                "original_h": 366,
                "permalink": "https://.../dramacat.gif",
                "permalink_public": "https://.../More-Path-Components",
                "comments_count": 0,
                "is_starred": false,
                "shares": {
                    "private": {
                        "D0L4B9P0Q": [
                            {
                                "reply_users": [],
                                "reply_users_count": 0,
                                "reply_count": 0,
                                "ts": "1532293503.000001"
                            }
                        ]
                    }
                },
                "channels": [],
                "groups": [],
                "ims": [
                    "D0L4B9P0Q"
                ],
                "has_rich_preview": false
            }
        }
        """

        payload = {"token": self._token}

        if channels is not None:
            payload["channels"] = comma_separated_string(channels)

        if content is not None:
            file_to_upload = make_file(content)
            payload["content"] = file_to_upload

        if file is not None:
            file_to_upload = make_file(file)
            kwargs["files"] = {"file": file_to_upload}

        if filename is not None:
            payload["filename"] = filename

        if filetype is not None:
            payload["filetype"] = filetype

        if initial_comment is not None:
            payload["initial_comment"] = initial_comment

        if thread_ts is not None:
            payload["thread_ts"] = thread_ts

        if title is not None:
            payload["title"] = title

        return self._post("files.upload", payload=payload, **kwargs)
예제 #6
0
    def add(
        self,
        external_id: int,
        external_url: str,
        title: str,
        filetype: str = None,
        indexable_file_contents: Union[str, PathLike, IO] = None,
        preview_image: Union[str, PathLike, IO] = None,
        **kwargs
    ) -> Response:
        """
        Adds a file from a remote service
        https://api.slack.com/methods/files.remote.add

        :param token: Authentication token bearing required scopes.
        :type str: e.g. xxxx-xxxxxxxxx-xxxx

        :param external_id: Creator defined GUID for the file.
        :type int: e.g. 123456

        :param external_url: URL of the remote file.
        :type str: e.g. http://example.com/my_cloud_service_file/abc123

        :param title: Title of the file being shared.
        :type str: e.g. Danger, High Voltage!

        :param filetype: type of file
        :type str: e.g. doc

        :param indexable_file_contents: A text file (txt, pdf, doc, etc.) containing textual search terms that are used to improve discovery of the remote file.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file"

        :param preview_image: Preview of the document via multipart/form-data.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file"

        :returns response:
        :type requests.Response: e.g. <Response [200]>

        example:
        >>> client = SlackTime(token='insert-your-token-here')
        >>> response = client.files.remote.add(**your_params)
        <Response [200]>
        >>> response.json()
            {
                "ok": true,
                "file": {
                    "id": "F0GDJ3XMH",
                    "created": 1563919925,
                    "timestamp": 1563919925,
                    "name": "LeadvilleAndBackAgain",
                    "title": "LeadvilleAndBackAgain",
                    "mimetype": "application/vnd.slack-remote",
                    "filetype": "remote",
                    "pretty_type": "Remote",
                    "user": "******",
                    "editable": false,
                    "size": 0,
                    "mode": "external",
                    "is_external": true,
                    "external_type": "app",
                    "is_public": false,
                    "public_url_shared": false,
                    "display_as_bot": false,
                    "username": "",
                    "url_private": "https://docs.google.com/document/d/1TA9fIaph4eSz2fC_1JGMuYaYUc4IvieIop0WqfCXw5Y/edit?usp=sharing",
                    "permalink": "https://kraneflannel.slack.com/files/U0F8RBVNF/F0GDJ3XMH/leadvilleandbackagain",
                    "comments_count": 0,
                    "is_starred": false,
                    "shares": {},
                    "channels": [],
                    "groups": [],
                    "ims": [],
                    "external_id": "1234",
                    "external_url": "https://docs.google.com/document/d/1TA9fIaph4eSz2fC_1JGMuYaYUc4IvieIop0WqfCXw5Y/edit?usp=sharing",
                    "has_rich_preview": false
                }
            }

        """

        payload = {
            "token": self._token,
            "external_id": external_id,
            "external_url": external_url,
            "title": title,
        }

        if filetype is not None:
            payload["filetype"] = filetype

        if indexable_file_contents is not None:
            file_to_upload = make_file(indexable_file_contents)
            payload["indexable_file_contents"] = file_to_upload

        if preview_image is not None:
            file_to_upload = make_file(indexable_file_contents)
            kwargs["files"] = {"file": file_to_upload}

        return self._get("files.remote.add", payload=payload, **kwargs)
예제 #7
0
    def update(
        self,
        external_id: int = None,
        external_url: str = None,
        file: str = None,
        filetype: str = None,
        indexable_file_contents: Union[str, PathLike, IO] = None,
        preview_image: Union[str, PathLike, IO] = None,
        title: str = None,
        **kwargs
    ) -> Response:
        """
        Updates an existing remote file.
        https://api.slack.com/methods/files.remote.update

        :param token: Authentication token bearing required scopes.
        :type str: e.g. xxxx-xxxxxxxxx-xxxx

        :param external_id: Creator defined GUID for the file.
        :type int: e.g. 123456

        :param external_url: URL of the remote file.
        :type str: e.g. http://example.com/my_cloud_service_file/abc123

        :param file: Specify a file by providing its ID.
        :type str: e.g. F2147483862

        :param filetype: type of file
        :type str: e.g. doc

        :param indexable_file_contents: File containing contents that can be used to improve searchability for the remote file.
        :type Union[str, PathLike, IO]: e.g. '/absolute/path/to/file' or actual IO file"

        :param preview_image: Preview of the document via multipart/form-data.
        :type Union[str, PathLike]: e.g. '/absolute/path/to/file' or actual IO file"

        :param title: Title of the file being shared.
        :type str: e.g. Danger, High Voltage!

        :returns response:
        :type requests.Response: e.g. <Response [200]>

        example:
        >>> client = SlackTime(token='insert-your-token-here')
        >>> response = client.files.remote.update(**your_params)
        <Response [200]>
        """

        payload = {"token": self._token}

        if external_id is not None:
            payload["external_id"] = external_id

        if external_url is not None:
            payload["external_url"] = external_url

        if file is not None:
            payload["file"] = file

        if filetype is not None:
            payload["filetype"] = filetype

        if indexable_file_contents is not None:
            file_to_upload = make_file(file)
            payload["indexable_file_contents"] = file_to_upload

        if preview_image is not None:
            file_to_upload = make_file(file)
            kwargs["files"] = {"file": file_to_upload}

        if title is not None:
            payload["title"] = title

        return self._get("files.remote.update", payload=payload, **kwargs)