scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 394 forks source link

KeyError in SlackClient.get_channel() when in a private chat #120

Open tehranian opened 7 years ago

tehranian commented 7 years ago

Trace:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/slackbot/dispatcher.py", line 55, in _dispatch_msg_handler
    func(Message(self._client, msg), *args)
  File "/home/vmagent/app/command_tracking.py", line 31, in func_wrapper
    func(message, *args)
  File "/home/vmagent/app/commands/x00s_graph.py", line 92, in x00s_graph
    args)
  File "/home/vmagent/app/helpers.py", line 72, in func_wrapper
    func(message, *args)
  File "/home/vmagent/app/commands/x00s_graph.py", line 158, in _x00s_graph
    message.channel.upload_file(
  File "/usr/local/lib/python2.7/dist-packages/slackbot/dispatcher.py", line 262, in channel
    return self._client.get_channel(self._body['channel'])
  File "/usr/local/lib/python2.7/dist-packages/slackbot/slackclient.py", line 131, in get_channel
    return Channel(self, self.channels[channel_id])
KeyError: u'G0A0FG9U0'

I'm trying to call message.channel.upload_file() and it occasionally fails with a KeyError. This seems to happen when a user is talking directly to the Bot (ex: a 1:1, private conversation). It does not seem to happen in a public channel.

Let me know if there is any further info I can provide. Thanks!

elv1z commented 7 years ago

@tehranian , i got the same problem.

It is possible to send file by using direct api requests with 'files.upload' method. All you need is channel id.

import requests channel_id = message.channel._body['id'] api_token = %your_token_here% file_params = { 'token' : api_token ,'initial_comment': 'comment' ,'title': 'file_title' ,'channels': channel_id } file_object = { 'file': open(%path_to_file%, 'rb') } requests.post('https://slack.com/api/files.upload', params = file_params, files = file_object)

tehranian commented 7 years ago

@elv1z Yea, that'll work. Kind of annoying to have to use requests to POST to a URL, deal with API token, etc. I was thinking to grab the slackclient object at message._client and use that to POST.

jtatum commented 7 years ago

I believe this is a dupe of #61. We have a list of users and channels that's populated only once, on bot start. If you get added to a new channel or a new user is added, various things break. Is the channel/group in question new, @tehranian?

tehranian commented 7 years ago

Hi @jtatum ,

Yes - The channel/groups in question were new, and yes - Looking through the code, that was my assessment as well: That the list of uses & channels is populated once, upon startup.

Thanks! Dan