python-slack-sdk: Slack Login Error in rtm_connect
I am following this tutorial here: https://www.fullstackpython.com/blog/build-first-slack-bot-python.html and I am having a problem when I am trying to run the code. The errors I get are:
Traceback (most recent call last): File “C:\Users\n0342338\AppData\Local\Programs\Python\Python36\lib\site-packages\slackclient\client.py”, line 52, in rtm_connect self.server.rtm_connect(use_rtm_start=with_team_state, **kwargs) File “C:\Users\n0342338\AppData\Local\Programs\Python\Python36\lib\site-packages\slackclient\server.py”, line 151, in rtm_connect raise SlackLoginError(reply=reply) slackclient.server.SlackLoginError
The code is never entering the ‘if slack_client.rtm_connect(with_team_state=False)’ statement.
Here is the code:
slack_token = os.environ['SLACK_BOT_TOKEN']
slack_client = SlackClient(slack_token)
#slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
# starterbot's user ID in slack: Value is assigned after the bot starts up
startbot_id = None
#constants
RTM_READ_DELAY = 1 #This is a 1 second RTM_READ_DELAY
EXAMPLE_COMMAND = "do"
MENTION_REGEX = "^@(|[WU].+?)>(.*)"
print('at beggining')
def parse_bot_commands(slack_events):
"""
Parses a list of event coming from the Slack RTM API to find bot commands.
If a bot command is found, this function returns a tuple of command and channel.
If it is not found, then this function returns None, None.
"""
for event in slack_events:
if event["type"] == "message" and not "subtype" in event:
user_id, message = parse_direct_mention(event["text"])
if user_id == starterbot_id:
return message, event["channel"]
return None, None
def parse_direct_mention(message_text):
"""
Finds a direct mention (a mention that is at the begginging) in message message
and returns the user ID which was mentioned. If there is no direct mention
it returns None
"""
matches = re.search(MENTION_REGEX, message_text)
#The first group contains the username, the second groups contains the
#remaining message return (matches.group(1), matches.group(2).strip()) if
#matches else (None, None)
def handle_command(command, channel):
"""
Executes bot command if the command is known
"""
#Default response is help text for the user
default_response = "Not sure what you mean. Try *{}*.".format(EXAMPLE_COMMAND)
#Finds and executes the given command, filling in response
response = None
#Other commands implement there
if command.startwith(EXAMPLE_COMMAND):
response = "Sure...write stuff here"
#Sends the response back to the channel
print('before api call')
slack_client.api_call(
"chat.postMessage",
channel=channel,
text=respones or default_response
)
print('at name=main')
if __name__ == "__main__":
print('in name = mian')
if slack_client.rtm_connect(with_team_state=False):
print("Starter Bot connected and running!")
#Read bot's user ID by calling Web API method 'auth.test'
print('here')
starterbot_id = slack_client.api_call("auth_test")["user_id"]
while True:
command, channel = parse_bot_commands(slack_client.rtm_read())
if command:
handle_command(command, channel)
time.sleep(RTM_READ_DELAY)
else:
print("Connection failed. Exception taceback printed above")
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 3
- Comments: 16 (4 by maintainers)
OK I fixed the issue , rather than using pybot token use user token . it worked for me.
@jimmy927, the new granular scopes (new xoxb-* token) don’t support RTM. You can create a classic slack app if you need to create a new token. But keep in mind that the Slack App Directory will stop accepting new app submissions for classic slack apps on Feb 21, 2020.
Instead of RTM, we recommend using the Events API. If there is a specific reason or use case you need RTM over Events API, I’d love to hear about it.
Looks great, but it uses Flask which i don’t have or want to introduce on my side.
thanks @aoberoi ! I’ve just found the error: I was using a method that doesn’t work with bot token. Just with user token or workplace token! Thanks anyway 👍 !