Error occurred while updating the thread in slack: 'file'
(Filling out the following details about bugs will help us solve your issue sooner.)
Reproducible in:
python version 3.8
The Slack SDK version
Not using slack sdk
Python runtime version
(Paste the output of python --version)
3.8
OS info
(Paste the output of sw_vers && uname -v on macOS/Linux or ver on Windows OS)
Steps to reproduce:
(Share the commands to run, source code, and project settings (e.g., setup.py))
def update_slack_message(channel_id, thread_ts, text):
slack_updatemessage_url = "https://slack.com/api/chat.update"
update_data = {"channel": channel_id, "text": text, "ts": thread_ts}
slack_data = text if is_final else update_data
try:
headers = {
"Authorization": f"Bearer {get_bot_user_token()}",
"Content-Type": "application/json; charset=utf-8",
}
logger.info(f"Updating the slack message: {json.dumps(slack_data)}")
response = requests.post(url=slack_updatemessage_url, json=slack_data, headers=headers)
response.raise_for_status()
if response.status_code == 200:
logger.info(f"Slack Message updated Successfully! {response.text}")
thread_update_ts = json.loads(response.text).get("ts")
upload_info = upload_files_to_slack(thread_update_ts, channel_id)
logger.info(f"File id fetched Successfully! {upload_info}")
if upload_info:
logger.info(f"inside upload info! {upload_info}")
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, 'file_name.xlsx')
upload_url = upload_info['upload_url']
file_id = upload_info['file']['id']
logger.info(f"upload_url! {upload_url}")
logger.info(f"file_id: {file_id}")
with open(file_path, "rb") as file_content:
logger.info(f"inside file path! {file_path}")
upload_response = requests.put(upload_url, data = file_content)
logger.info(f"FILE upload to url Upload response {upload_response}")
if upload_response.status_code == 200:
complete_response = complete_upload(file_id, channel_id, thread_update_ts)
if complete_response:
logger.info("COMPELETD FILE UPLOAD")
else:
logger.error("FAILED FILE UPLOAD")
return response
except HTTPError as http_err:
logger.error(f"Error occurred: {http_err}")
except Exception as err:
logger.error(f"Error occurred while updating the thread in slack: {err}")
return
def upload_files_to_slack(thread_ts, channel_id):
slack_get_upload_url = "https://slack.com/api/files.getUploadURLExternal"
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, 'file_name.xlsx')
filename = os.path.basename(file_path)
file_length = os.path.getsize(file_path)
try:
headers = {
"Authorization": f"Bearer {get_bot_user_token()}",
"Content-Type": "application/x-www-form-urlencoded",
}
params = {
"filename": filename,
"length": file_length
}
response = requests.get(url=slack_get_upload_url, headers=headers, params=params)
response.raise_for_status()
logger.info(f"Slack file uploaded Successfully! {response.text}")
return response.json()
except HTTPError as http_err:
logger.error(f"Http Error occurred: {http_err}")
except Exception as err:
logger.error(f"Error occurred while fetching bot_id: {err}")
return None
def complete_upload(file_id, channel_id, thread_ts):
logger.info(f"triggered complete uploadß")
url = "https://slack.com/api/files.completeUploadExternal"
data = {
"files": [
{
"id": file_id
}
],
"channel_id": channel_id,
"thread_ts": thread_ts,
"initial_comment": "hi file!"
}
try:
headers = {
"Authorization": f"Bearer {get_bot_user_token()}",
"Content-Type": "application/json; charset=utf-8",
}
response = requests.post(url=url, headers=headers, json=data)
response.raise_for_status()
logger.info(f"Slack file upload completed Successfully! {response.text}")
return response.json()
except HTTPError as http_err:
logger.error(f"Http Error occurred: {http_err}")
except Exception as err:
logger.error(f"Error occurred while fetching bot_id: {err}")
return None
Expected result:
--- file uploaded successfully
Actual result:
--- Error occured while posting to slack thread: 'file'
Requirements
For general questions/issues about Slack API platform or its server-side, could you submit questions at
Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.
Hi @divyakrishna-devisetty, thanks for asking the question.
Error occured while posting to slack thread: 'file'
What error did you observe in the situation? Did you mean the uploaded files were not included in the message? Did you receive an error code from the Slack API server?
Either way, we recommend using the files_upload_v2 method instead: https://slack.dev/python-slack-sdk/api-docs/slack_sdk/web/client.html#slack_sdk.web.client.WebClient.files_upload_v2
If you want to post a simple message like "Now working on your files..." and then update the same message to include the uploaded files later on, the only way to achieve this goal is by including the files' permalinks in the message text when calling the chat.update API. However, simply using the files_upload_v2 is definitely easier.
I hope this helps.
👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.
As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.