slackapi/python-slack-sdk

Unable to update messages

patrikpihlstrom opened this issue · 2 comments

I'm using the slack_sdk package to post messages in a channel. I've granted the appropriate scopes according to the documentation. From what I understand the same scopes are required for posting and updating messages in a channel (chat:write). However, when I try to update a previously sent message using the ts value, I get an error response saying that the channel wasn't found.

Reproducible in:

The Slack SDK version

slack-sdk==3.23.0

Python runtime version

Python 3.8.10

OS info

Linux dev 5.4.0-1093-raspi #104-Ubuntu SMP PREEMPT Thu Aug 24 08:54:40 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

Steps to reproduce:

pip install slack_sdk

from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

slack = WebClient(token='my-token')
try:
    response = slack.chat_postMessage(
        channel='imager',
        text='test'
    )
    response = slack.chat_update(
        channel='imager',
        ts=response['ts'],
        text='edited'
    )
    logger.debug(response)
except SlackApiError as e:
    logger.exception(e)

Expected result:

I expect the message to be updated to read "edited".

Actual result:

I receive an error response:

ERROR:imager:The request to the Slack API failed. (url: https://www.slack.com/api/chat.update)
The server responded with: {'ok': False, 'error': 'channel_not_found'}
Traceback (most recent call last):
  File "./imager.py", line 83, in main
    response = slack.chat_update(
  File "/usr/local/lib/python3.8/dist-packages/slack_sdk/web/client.py", line 2597, in chat_update
    return self.api_call("chat.update", json=kwargs)
  File "/usr/local/lib/python3.8/dist-packages/slack_sdk/web/base_client.py", line 156, in api_call
    return self._sync_send(api_url=api_url, req_args=req_args)
  File "/usr/local/lib/python3.8/dist-packages/slack_sdk/web/base_client.py", line 187, in _sync_send
    return self._urllib_api_call(
  File "/usr/local/lib/python3.8/dist-packages/slack_sdk/web/base_client.py", line 309, in _urllib_api_call
    return SlackResponse(
  File "/usr/local/lib/python3.8/dist-packages/slack_sdk/web/slack_response.py", line 199, in validate
    raise e.SlackApiError(message=msg, response=self)
slack_sdk.errors.SlackApiError: The request to the Slack API failed. (url: https://www.slack.com/api/chat.update)
The server responded with: {'ok': False, 'error': 'channel_not_found'}

Hi @patrikpihlstrom, thanks for writing in.

channel='imager',

It seems you might be using a channel name instead of a channel ID in the code. This can be confusing but only the chat.postMessage API can accept both a channel ID and name. This is not consistently the same across all other APIs. Therefore, please use the 'imager' channel's ID, which begins with 'C'.

I hope this is helpful.

Hi @patrikpihlstrom, thanks for writing in.

channel='imager',

It seems you might be using a channel name instead of a channel ID in the code. This can be confusing but only the chat.postMessage API can accept both a channel ID and name. This is not consistently the same across all other APIs. Therefore, please use the 'imager' channel's ID, which begins with 'C'.

I hope this is helpful.

That solved it, thanks! 👍