errbotio / err-backend-slackv3

Slack Events and RTM backend for Errbot
GNU General Public License v3.0
27 stars 28 forks source link

Error trying uload file on chats slack with new backend #71

Open litr0x opened 2 years ago

litr0x commented 2 years ago

i triying to upload a file to a slack chat. This is a complete log

2022-07-18 17:13:51,398 ERROR errbot.backends.slackv3 Upload of archivos-R107-H02.tar.gz to coordinación-entregas failed.
Traceback (most recent call last):
File "/errbot-dir/backend/err-backend-slackv3/slackv3.py", line 752, in _slack_upload
resp = self.slack_web.files_upload(
File "/usr/lib/python3.9/site-packages/slack_sdk/web/client.py", line 2975, in files_upload
return self.api_call("files.upload", files={"file": file}, data=kwargs)
File "/usr/lib/python3.9/site-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/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 187, in _sync_send
return self._urllib_api_call(
File "/usr/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 309, in _urllib_api_call
return SlackResponse(
File "/usr/lib/python3.9/site-packages/slack_sdk/web/slack_response.py", line 189, 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/files.upload)
The server responded with: {'ok': False, 'error': 'invalid_channel', 'channel': '<#GCP4E5A1G|coordinación-entregas>'}
sijis commented 2 years ago

Could you include an example of what the botcmd you were using to do this? If it's a custom botcmd, would also be able to include a snippet of it?

drjeep commented 7 months ago

I have the same issue. My botcmd looks something like this

@arg_botcmd("job_id", type=int, name="get-jenkins-log")
def get_jenkins_log(self, msg, job_id):
    try:
        to = msg.frm.room
    except AttributeError:
        to = msg.frm

    body = Command.get_jenkins_job_log(job_id)

    self.send_stream_request(
        user=to,
        fsource=BytesIO(
            bytes(
                body,
                "utf-8",
            )
        ),
    )

The issue seems to be that to.channelid contains <#C12345|channel-name> instead of just C12345 that Slack expects. In my case the fix is changing stream.identifier.channelid to stream.identifier.id in _slack_upload

def _slack_upload(self, stream: Stream) -> None:
    """
    Performs an upload defined in a stream
    :param stream: Stream object
    :return: None
    """
    try:
        stream.accept()
        resp = self.slack_web.files_upload(
            channels=stream.identifier.id, filename=stream.name, file=stream
        )
        if resp.get("ok"):
            stream.success()
        else:
            stream.error()
    except Exception:
        log.exception(f"Upload of {stream.name} to {stream.identifier.channelname} failed.")