A chat bot for Slack based on llimllib/slask.
- Based on slack Real Time Messaging API
- Simple plugins mechanism
- Messages can be handled concurrently
- Automatically reconnect to slack when connection is lost
sudo pip install slackbot
First you need to get the slack api token for your bot. You have two options:
- If you use a bot user integration of slack, you can get the api token on the integration page.
- If you use a real slack user, you can generate an api token on slack web api page.
This uses a configuration file and looks for a section called slackbot. Both the config file and its spec are specified using Panoptez-specific names:
PZBOT_CONFIG
PZBOT_CONFIG_SPEC
If these don't exist, the bot fails startup.
The available keys include:
- debug (default: False)
- api_token (default: '')
- plugins (default: ['slackbot.plugins'])
Here's an example:
[slackbot]
debug = False
api_token = ''
plugins = ['slackbot.plugins',]
Note the trailing comma, to force the ConfigObj to parse as a list.
Under slackbot, a [[handlers]]
subsection can be added that specifies
how to handle bot messages. The format is bot_id = handler module
.
from slackbot.bot import Bot
def main():
bot = Bot()
bot.run()
if __name__ == "__main__":
main()
Now you can talk to your bot in your slack client!
A chat bot is meaningless unless you can extend/customize it to fit your own use cases.
To write a new plugin, simplely create a function decorated by slackbot.bot.respond_to
or slackbot.bot.listen_to
:
- A function decorated with
respond_to
is called when a message matching the pattern is sent to the bot (direct message or @botname in a channel/group chat) - A function decorated with
listen_to
is called when a message matching the pattern is sent on a channel/group chat (not directly sent to the bot)
from slackbot.bot import respond_to
from slackbot.bot import listen_to
@respond_to('I love you')
def love(message):
message.reply('I love you too!')
@listen_to('Can someone help me?')
def help(message):
# Message is replied to the sender (prefixed with @user)
message.reply('Yes, I can!')
# Message is sent on the channel
# message.send('I can help everybody!')
To extract params from the message, you can use regular expression:
from slackbot.bot import respond_to
@respond_to('Give me (.*)')
def giveme(message, something):
message.reply('Here is %s' % something)
And add the plugins module to PLUGINS
list of slackbot settings, e.g. slackbot_settings.py:
PLUGINS = [
'slackbot.plugins',
'mybot.plugins',
]