How to create a Slack message containing an uploaded image?

后端 未结 2 1800
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 02:20

I\'d like to create a message in the #general channel of my Slackspace from within a PHP script. The message should contain text and an image which was created locally on-th

2条回答
  •  Happy的楠姐
    2021-01-18 02:46

    After much tinkering I found that while I could not use the API to create a message and upload an image simultaneously, I can first upload an image and then, with the timestamp returned, use update message to add text to the original message with the image upload.

    This is the flow:

    1- Use files_upload method to upload an image to my channel (using the channel name)

                response = client.files_upload(
                channels=my_channel_name,
                file=image_path,
                initial_comment='My initial comment'
                )
    

    2- Get the response from the files_upload and extract the channel id and timestamp of the message.

    channel_id = response['file']['groups'][0]
    ts = response['file']['shares']['private'][channel_id][0]['ts']
    

    3- Use chat update to add text or rich content to the message with the uploaded image:

           response = client.chat_update(
                channel=channel_id,
                text="My Message",
                ts=ts,
                blocks=blocks_list
            )
    

提交回复
热议问题