The first iteration of this requires the following:¶
-The Slack python API, available at https://github.com/slackapi/python-slackclient or via `pip install slackclient==1.0.0`. Makes sure to install version 1.0.0, as the newest version 1.0.9 will not work with Python 2.6.
-A working slack api token for your "workspace", as they are now calling it.
A slack.cfg file. You can put it in any of the places that a leginon.cfg file can go. For now, I recommend putting it in /etc/myami (on the leginon workstation) , so that users cannot tamper with it, the api token should not be shared. The contents of the slack.cfg look like
[config]
virtualenv_path: /gpfs/sw/envs/slackenv/bin/
slack_token: xoxp-68467417859-68511110690-86524545842-f2e0fsaasf222a5ade163909a4ec76f
I was initially getting errors with python 2.6 and thought it was necessary to use a virtual environment. I downgraded slackclient to version 1.0.0 and sending messages works. I will leave the virtualenv_path variable there for now since it is not hurting anything and may be necessary in the future, just leave it blank if you are not using it.
-You have to register a custom app in your slack workspace. From there, you can generate a tester token, I will soon add authentication via oauth2 which is the slack recommended form of authentication.
Send a message:¶
From the command line, do
python slack_interface.py -c <CHANNEL> -m <MESSAGE>
...that's it. The channel indicated in CHANNEL must already exist in the workspace or the slack api will return an error. The channel name can have an optional # at the beginning.
Programmatically:
import slack_interface
sc = slack_interface.slack_interface()
sc.send_message("#apitest","Hello, channel!")