Skip to main content

Gendo for Slack

https://img.shields.io/pypi/v/gendobot.svg https://img.shields.io/pypi/pyversions/gendobot.svg https://travis-ci.org/nficano/gendo.svg?branch=master https://coveralls.io/repos/nficano/gendo/badge.svg?branch=master&service=github&cb=321

Description

Gendo is a lightweight Slackbot framework that abstracts away all the boilerplate code required to write bots, allowing you to focus on the problem at hand.

Installation

  1. In a new project folder for your bot:

$ mkdir myslackbot
$ cd myslackbot
  1. Install gendobot from pypi.

$ pip install gendobot
  1. Next make another file for your bot’s logic:

$ touch mybot.py
  1. Also in your favourate text editor, edit mybot.py with the following:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gendo import Gendo
gendo = Gendo("xoxb-1234567890-replace-this-with-token-from-slack")


@gendo.listen_for('morning')
def morning(user, message):
    return "mornin' @{user.username}"

if __name__ == '__main__':
   gendo.run()
  1. Now try running it, run the following command then say “morning” in Slack.

$ python mybot.py

Basic Usage

To start your project, you’ll first need to import gendo by adding from gendo import Gendo to the top of your file.

Next you’ll need to create an instance of Gendo and configure your Slack token. This can be done using a yaml config file or passing it explicitly to the initialization.

# Option 1: YAML config:
import os
from gendo import Gendo

path = os.path.dirname(os.path.abspath(__file__))
path_to_yaml = os.path.join(path, 'config.yaml')
gendo = Gendo.config_from_yaml(path_to_yaml)
# Option 2: Hardcoded slack token
from gendo import Gendo
gendo = Gendo("xoxb-1234567890-replace-this-with-token-from-slack")

Now its time to write your response functions, these functions get wrapped with the listen_for decorator, which registers a pattern to watch the slack conversation for and which python method should handle it once its said.

In the following example, the method is setup to listen for the word “cookies”. Notice that the decorator passes two arguments to the function, first the user object which contains information about the user who triggered the event (in this case the Slack user who said the word cookies) and message, which is a string of the complete message.

@gendo.listen_for('cookies')
def cookies(user, message):
    # do something when someone say's "cookies" here.

You can also set more complicated rules with callables, and you can stack them! Here’s an example.

def nicks_joke_rule(name, message):
    is_nick = name == 'nficano'
    is_telling_a_joke = message.lower().count('knock') == 2
    return is_nick and is_telling_a_joke


def bens_joke_rule(name, message):
    is_ben = name == 'johnbenjaminlewis'
    is_telling_a_joke = message.lower().count('knock') == 2


@gendo.listen_for(nicks_joke_rule)
@gendo.listen_for(bens_joke_rule)
def another_joke(name, message):
    if name == 'johnbenjaminlewis':
        return '@johnbenjaminlewis, nice try. But no.'
    elif name == 'nficano':
        return "@here Nick's telling a joke! Who's there?!?"

Finally your script needs to sit inside a loop, monitor whats said in a slack channel and respond to the messages accordingly. To do this we add the following to the end of your script:

if __name__ == '__main__':
   gendo.run()

Crontab

Sometimes you’ll run into situations where you want Slack messages to be sent periodically rather than in direct response to a keyword, for this Gendo ships with a single-threaded Python implementation of Cron.

Let’s pretend we want to send a message to everyone in a channel every five minutes, simply add the following to your mybot.py file:

@gendo.cron('*/5 * * * *')
def some_task():
    gendo.speak("Hay Ride!", "#general")

See https://en.wikipedia.org/wiki/Cron#Configuration_file for more details on crontab syntax.

Development

Development of “gendo” is facilitated exclusively on GitHub. Contributions in the form of patches, tests and feature creation and/or requests are very welcome and highly encouraged. Please open an issue if this tool does not function as you’d expect.

How to release updates

If this is the first time you’re releasing to pypi, you’ll need to run: pip install -r tests/dev_requirements.txt.

Once complete, execute the following commands:

git checkout master

# Increment the version number and tag the release.
bumpversion [major|minor|patch]

# Upload the distribution to PyPi
python setup.py sdist bdist_wheel upload

# Since master often contains work-in-progress changes, increment the version
# to a patch release to prevent inaccurate attribution.
bumpversion --no-tag patch

git push origin master --tags

Release files for gendobot 4.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gendobot 4.1.4
File Size Uploaded
gendobot-4.1.4.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gendobot 4.1.4
File Interpreter ABI Platform
gendobot-4.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.8 kB

Release files / gendobot-4.1.4.tar.gz

Download URL gendobot-4.1.4.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bb9e4ba0ae88a3f04a950e8c7510d735ae91183a48ff381710351138f37537d5
BLAKE2b-256 checksum
How to use checksums
9af7c43fb04aa3f906462b4514d0ac2131bcb60deac05d7b3749d7198a406350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / gendobot-4.1.4-py2.py3-none-any.whl

Download URL gendobot-4.1.4-py2.py3-none-any.whl
Size 12.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
879f4f3b1c4c6ad69184580f85df3fe91d6887dc4b394376186eb27dfc35135f
BLAKE2b-256 checksum
How to use checksums
404d1cfabd6f3b58a60a3c07e34a4dd2c82e2214b5c69ce361c687b5bf18acee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

4.1.4 This release

2 release files

4.1.2

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.2

2 release files

3.0.0

2 release files

2.5.4

2 release files

2.5.2

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.3

1 release file

2.2.2

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.4

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.0

1 release file

0.1.7

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page