Python-slack-sdk: App quit after single task

Created on 26 Jul 2019  路  2Comments  路  Source: slackapi/python-slack-sdk

Description

My app quit after single task.

My code:

import os
from slack import RTMClient, WebClient

# run following command to add token to local env
# export SLACK_API_TOKEN = "SLACK TOKEN HERE"


@RTMClient.run_on(event='message')
def say_hello(**payload):
    data = payload['data']
    web_client = payload['web_client']
    rtm_client = payload['rtm_client']

    if 'Hello' in data['text']:
        channel_id = data['channel']
        thread_ts = data['ts']
        user = data['user']

        web_client.chat_postMessage(
            channel=channel_id,
            text=f"Hi <@{user}>!",
            thread_ts=thread_ts
        )

if __name__ == '__main__':
    slack_token = os.environ["SLACK_API_TOKEN"]
    client = WebClient(token=slack_token)
    response = client.chat_postMessage(
        channel='#random',
        text="Robot is now ready!")
    rtm_client = RTMClient(token=slack_token)
    rtm_client.start()

error

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [x ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

slackclient version: 2.1.0

python version: 3.6.8 & 3.7.1

OS version(s): Windows 10 version 1809, Ubuntu 18.04

Steps to reproduce:

1.
2.
3.

Expected result:

I expect the app to be running until being interrupted

Actual result:

john@myLaptop:~$ python3 miya.py

Traceback (most recent call last):
  File "miya.py", line 30, in <module>
    rtm_client.start()
  File "/home/john/.local/lib/python3.6/site-packages/slack/rtm/client.py", line 197, in start
    return self._event_loop.run_until_complete(future)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "/home/john/.local/lib/python3.6/site-packages/slack/rtm/client.py", line 339, in _connect_and_read
    await self._read_messages()
  File "/home/john/.local/lib/python3.6/site-packages/slack/rtm/client.py", line 390, in _read_messages
    await self._dispatch_event(event, data=payload)
  File "/home/john/.local/lib/python3.6/site-packages/slack/rtm/client.py", line 440, in _dispatch_event
    self._execute_in_thread(callback, data)
  File "/home/john/.local/lib/python3.6/site-packages/slack/rtm/client.py", line 465, in _execute_in_thread
    future.result()
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 425, in result
    return self.__get_result()
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "miya.py", line 12, in say_hello
    if 'Hello' in data['text']:
KeyError: 'text'

Attachments:

Most helpful comment

The replied message structure from bot doesn't have 'text' keyword, which causes the KeyError error. You can replace if 'Hello' in data['text']: with if 'Hello' in data.get('text', ''): to fix it.

All 2 comments

The replied message structure from bot doesn't have 'text' keyword, which causes the KeyError error. You can replace if 'Hello' in data['text']: with if 'Hello' in data.get('text', ''): to fix it.

The replied message structure from bot doesn't have 'text' keyword, which causes the KeyError error. You can replace if 'Hello' in data['text']: with if 'Hello' in data.get('text', ''): to fix it.

Thank you! It works! 馃憤 馃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ErikKalkoken picture ErikKalkoken  路  3Comments

Dwyte picture Dwyte  路  3Comments

tinoargentino picture tinoargentino  路  3Comments

naveenjafer picture naveenjafer  路  4Comments

seratch picture seratch  路  3Comments