Skip to main content

A simple chat bot for Slack

Project description

PyPI CI

This is a fork of scrapinghub/slackbot due to the lack of the activity from the original author. Python package was renamed to slackbotng and it's being publishing to: https://pypi.org/project/slackbotng/

A chat bot for Slack inspired by llimllib/limbo and will.

Features

Installation

pip install slackbotng

Usage

Generate the slack api token

First you need to get the slack api token for your bot. You have two options:

  1. If you use a bot user integration of slack, you can get the api token on the integration page.
  2. If you use a real slack user, you can generate an api token on slack web api page.

Configure the bot

First create a slackbot_settings.py and a run.py in your own instance of slackbot.

Configure the api token

Then you need to configure the API_TOKEN in a python module slackbot_settings.py, which must be located in a python import path. This will be automatically imported by the bot.

slackbot_settings.py:

API_TOKEN = "<your-api-token>"

Alternatively, you can use the environment variable SLACKBOT_API_TOKEN.

Run the bot
from slackbot.bot import Bot
def main():
    bot = Bot()
    bot.run()

if __name__ == "__main__":
    main()
Configure the default answer

Add a DEFAULT_REPLY to slackbot_settings.py:

DEFAULT_REPLY = "Sorry but I didn't understand you"
Configure the docs answer

The message attribute passed to your custom plugins has an special function message.docs_reply() that will parse all the plugins available and return the Docs in each of them.

Send all tracebacks directly to a channel, private channel, or user

Set ERRORS_TO in slackbot_settings.py to the desired recipient. It can be any channel, private channel, or user. Note that the bot must already be in the channel. If a user is specified, ensure that they have sent at least one DM to the bot first.

ERRORS_TO = 'some_channel'
# or...
ERRORS_TO = 'username'
Configure the plugins

Add your plugin modules to a PLUGINS list in slackbot_settings.py:

PLUGINS = [
    'slackbot.plugins',
    'mybot.plugins',
]

Now you can talk to your bot in your slack client!

Attachment Support

from slackbot.bot import respond_to
import re
import json


@respond_to('github', re.IGNORECASE)
def github(message):
    attachments = [
    {
        'fallback': 'Fallback text',
        'author_name': 'Author',
        'author_link': 'http://www.github.com',
        'text': 'Some text',
        'color': '#59afe1'
    }]
    message.send_webapi('', json.dumps(attachments))

Create Plugins

A chat bot is meaningless unless you can extend/customize it to fit your own use cases.

To write a new plugin, simply create a function decorated by slackbot.bot.respond_to, slackbot.bot.listen_to, slackbot.bot.run_at_times:

  • 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)
  • A function decorated with run_at_times is called periodically at a given amount of seconds
from slackbot.bot import respond_to, listen_to, run_at_times
import re

@respond_to('hi', re.IGNORECASE)
def hi(message):
    message.reply('I can understand hi or HI!')
    # react with thumb up emoji
    message.react('+1')

@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!')

    # Start a thread on the original message
    message.reply("Here's a threaded reply", in_thread=True)
    
@run_at_times(run_once_at=60)
def run_once_at_60s(client):
    client.rtm_send_message('channel_name_or_username', 'This runs once at 60s!')

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 {}'.format(something))

If you would like to have a command like 'stats' and 'stats start_date end_date', you can create reg ex like so:

from slackbot.bot import respond_to
import re


@respond_to('stat$', re.IGNORECASE)
@respond_to('stat (.*) (.*)', re.IGNORECASE)
def stats(message, start_date=None, end_date=None):

And add the plugins module to PLUGINS list of slackbot settings, e.g. slackbot_settings.py:

PLUGINS = [
    'slackbot.plugins',
    'mybot.plugins',
]

The @default_reply decorator

Added in slackbot 0.4.1

Besides specifying DEFAULT_REPLY in slackbot_settings.py, you can also decorate a function with the @default_reply decorator to make it the default reply handler, which is more handy.

@default_reply
def my_default_handler(message):
    message.reply('...')

Here is another variant of the decorator:

@default_reply(r'hello.*)')
def my_default_handler(message):
    message.reply('...')

The above default handler would only handle the messages which must (1) match the specified pattern and (2) can't be handled by any other registered handler.

List of third party plugins

You can find a list of the available third party plugins on this page.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slackbotng-1.1.3.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slackbotng-1.1.3-py2.py3-none-any.whl (16.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file slackbotng-1.1.3.tar.gz.

File metadata

  • Download URL: slackbotng-1.1.3.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for slackbotng-1.1.3.tar.gz
Algorithm Hash digest
SHA256 26db1f12560474657dcf205747c12438d5cee5aea2505b2850963c458e7ed2ff
MD5 cc4134335d2970bb804aafc7035e92cc
BLAKE2b-256 8130a43c3581f27ff38c8f0e73cc6db9ac59191bb71670834f5e0bc8ff7055ff

See more details on using hashes here.

File details

Details for the file slackbotng-1.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: slackbotng-1.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for slackbotng-1.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b2a58115830c05dd0f7571fd3292f46781e9dda7119455b8aa606ded2570e830
MD5 fe39fe73e7be84f38b5882908094330e
BLAKE2b-256 34d01df3b8d3b0ed145fd8b0cccee91c1aaba21fba5125bb5e9737514cc6cd1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page