Beispiel #1
0
def upload_images(token: str, channel: str, filepath: str, description: str):
    client = WebClient(token=token)
    with _open(filepath, mode="rb") as f:
        if isinstance(f, io.BytesIO):
            image_type = imghdr.what(f)
        else:
            raise ValueError(f"'{filepath}' is not supported format")
        if image_type not in SUPPORTED_IMAGES:
            raise ValueError(f"'{filepath}' is not supported format")
        client.files_upload(file=f,
                            channels=channel,
                            initial_comment=description)
Beispiel #2
0
def file_shared(body, client: WebClient, context, logger):
    context.ack()
    file_id = body["event"]["file_id"]
    file_info = client.files_info(file=file_id)

    file_type = file_info["file"]["filetype"]
    if file_type not in ["jpg", "jpeg", "png"]:
        return

    url = file_info["file"]["url_private"]

    image = im.open_url(url, cfg.token)

    sides = ["left", "right"]
    uploaded_files = {}
    for side in sides:
        mirrored = im.mirror(image, side=side)
        mirrored.save(f"/tmp/{file_id}-{side}.{file_type}")

        with open(f"/tmp/{file_id}-{side}.{file_type}", "rb") as file_content:
            result = client.files_upload(file=file_content)
            uploaded_files[side] = result["file"]["permalink"]

    msg = f"<{uploaded_files['right']}| ><{uploaded_files['left']}| >"

    channel_name = channel_name_from_id(body["event"]["channel_id"])
    channel = "sapsik"
    if channel_name == "bot_testing":
        channel = channel_name

    sent = client.chat_postMessage(text=msg, channel=channel)
    for side in sides:
        try:
            client.reactions_add(channel=sent["channel"], timestamp=sent["ts"], name=f"point_{side}")
        except SlackApiError:
            pass