1

I've been learning how to use Python and Slack for my class project. I'm particularly working on slack threads so I can direct message my entire class.

import os
from slackclient import SlackClient

slack_token = os.environ["xoxb-restofapitoken"]
sc = SlackClient(slack_token)

sc.api_call(
"chat.postMessage",
channel="#general",
text="Hello from Python! :tada:",
thread_ts="1476746830.000003",
reply_broadcast=True

)

When I run the code, the error below shows.

Traceback (most recent call last):
File "chat1.py", line 4, in <module>
slack_token = os.environ["xoxb-restofapitoken"]
File "/home/ubuntu/starterbot/lib/python2.7/UserDict.py", line 40, in __getitem__
  raise KeyError(key)
KeyError: 'xoxb-restofapitoken'

What am I doing wrong?

thenewcoder
  • 51
  • 2
  • 7
  • I made the question clearer. – thenewcoder Nov 19 '17 at 03:39
  • 2
    It looks like the environment variable doesn't exist. Are you sure it does? What's the result of `echo $xoxb-restofapitoken` ? – Sudheesh Singanamalla Nov 19 '17 at 03:39
  • @SudheeshSinganamalla, now it's showing `AttributeError: _Environ instance has no __call__ method` – thenewcoder Nov 19 '17 at 03:46
  • For starters, you could print all the keys in `os.environ` and check if the required key exists. For example, if you put your slack token in a variable by doing `export SLACK_TOKEN='xoxb-restofapitoken'` you should access it by doing `os.environ['SLACK_TOKEN']`. Currently the key error says that the slack token isn't available on your OS Environment variables. – Sudheesh Singanamalla Nov 19 '17 at 03:51
  • @SudheeshSinganamalla I've exported the token already. Is naming a possible error? – thenewcoder Nov 19 '17 at 03:53
  • Can you share the command with which you exported the token? – Sudheesh Singanamalla Nov 19 '17 at 03:54
  • @SudheeshSinganamalla, Sure. I used `export SLACK_BOT_TOKEN='xoxb-restofapitoken'` – thenewcoder Nov 19 '17 at 04:03
  • Then `slack_token = os.environ["xoxb-restofapitoken"]` should be `slack_token = os.environ["SLACK_BOT_TOKEN"]` This is exactly what I explained in the previous comment. – Sudheesh Singanamalla Nov 19 '17 at 04:05
  • @SudheeshSinganamalla Maybe I'm just confused, but that's what I have but I replaced it with my token. What am I missing? – thenewcoder Nov 19 '17 at 04:09
  • You shouldn't replace it with your token, as it's just the name of the variable which holds the token, not its value. So do `export SLACK_BOT_TOKEN="your-api-token"` before you run your script and in your script replace the line with `slack_token = os.environ["SLACK_BOT_TOKEN"]`. – Dunedan Nov 19 '17 at 07:16
  • That actually worked, thank you both! – thenewcoder Nov 19 '17 at 23:28

1 Answers1

2

I've met similar error before. There is nothing wrong with your code. However, kindly follow Slack's guideline when you are using test tokens , "pass tokens in as environment variables".

Change the code to:

import os
from slackclient import SlackClient

slack_token = os.environ["SLACK_BOT_TOKEN"]
sc = SlackClient(slack_token)

sc.api_call(
"chat.postMessage",
channel="#general",
text="Hello from Python! :tada:",
thread_ts="1476746830.000003",
reply_broadcast=True

)

Run it with:

SLACK_BOT_TOKEN="xoxb-restofapitoken" python myapp.py

and you should be good to go.

mcgag
  • 325
  • 2
  • 14