Package: gendobot
DescriptionGendo 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.InstallationIn a new project folder for your bot:$mkdirmyslackbot$cdmyslackbotInstallgendobotfrompypi.$pipinstallgendobotNext make another file for your bot’s logic:$touchmybot.pyAlso in your favourate text editor, editmybot.pywith the following:#!/usr/bin/env python# -*- coding: utf-8 -*-fromgendoimportGendogendo=Gendo("xoxb-1234567890-replace-this-with-token-from-slack")@gendo.listen_for('morning')defmorning(user,message):return"mornin' @{user.username}"if__name__=='__main__':gendo.run()Now try running it, run the following command then say “morning” in Slack.$pythonmybot.pyBasic UsageTo start your project, you’ll first need to import gendo by addingfrom gendo import Gendoto 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:importosfromgendoimportGendopath=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 tokenfromgendoimportGendogendo=Gendo("xoxb-1234567890-replace-this-with-token-from-slack")Now its time to write yourresponsefunctions, these functions get wrapped
with thelisten_fordecorator, 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 theuserobject which contains information about the user who triggered the
event (in this case the Slack user who said the word cookies) andmessage,
which is a string of the complete message.@gendo.listen_for('cookies')defcookies(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.defnicks_joke_rule(name,message):is_nick=name=='nficano'is_telling_a_joke=message.lower().count('knock')==2returnis_nickandis_telling_a_jokedefbens_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)defanother_joke(name,message):ifname=='johnbenjaminlewis':return'@johnbenjaminlewis, nice try. But no.'elifname=='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()CrontabSometimes 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 yourmybot.pyfile:@gendo.cron('*/5 * * * *')defsome_task():gendo.speak("Hay Ride!","#general")Seehttps://en.wikipedia.org/wiki/Cron#Configuration_filefor more details on
crontab syntax.DevelopmentDevelopment 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 updatesIf this is the first time you’re releasing to pypi, you’ll need to run:pip install-rtests/dev_requirements.txt.Once complete, execute the following commands:gitcheckoutmaster# Increment the version number and tag the release.bumpversion[major|minor|patch]# Upload the distribution to PyPipythonsetup.pysdistbdist_wheelupload# Since master often contains work-in-progress changes, increment the version
# to a patch release to prevent inaccurate attribution.bumpversion--no-tagpatchgitpushoriginmaster--tags