BigSlacker – Plugin-Based RTM Slack client

I suppose most of you have heard of Slack – the nice chat application.

They have both a HTTP API and a RTM API based over WebSockets. I wrote BigSlacker  for an ongoing communicatio project. BigSlacker tries to make it easy to use the RTM API. You can write plugins simply by inheriting from the class BasePlugin and start getting events and replying messages. It supports both Python 2 and 3.

A few examples below:

Creating a Bot with a plugin to listen on channel_created only

from bigslacker import BigSlacker, BasePlugin
 
class ChannelAnnouncer(BasePlugin):
    def channel_created(self, data):
        print('I see a channel is created, gonna do something')
 
token = '1239182918sxxusus'
bs = BigSlacker(token)
bs.slack()

By simply inheriting from BasePlugin you create a plugin. You can listen on events by simply defining methods that have the same name as the name of the event – you can see a list here.

Sending a message back

We analyze every returned data from any Plugin we call. If you return None we do nothing. However, if you reply with [(channel, message)], we will automatically send that message to the specified channel.

from bigslacker import BigSlacker, BasePlugin
 
class ChannelAnnouncer(BasePlugin):
    def channel_created(self, data):
        print('I see a channel is created, gonna reply')
        return [('C1K4BBY8L', 'Hey guys, a channel has been created...')]
 
token = '1239182918sxxusus'
bs = BigSlacker(token)
bs.slack()

Of course, if you add more messages to the list, we’ll send them all – you might want to broadcast multiple messages upon an event.

Catching all events

You just have to define a catch_all method.

from bigslacker import BigSlacker, BasePlugin
 
class ChannelAnnouncer(BasePlugin):
    def catch_all(self, data):
        print('gonna inspect that data...')
 
token = '1239182918sxxusus'
bs = BigSlacker(token)
bs.slack()

Can I use Gevent ?

Yes, all you have to do is monkey-patch it before on your application. Nothing else changes.

from gevent import monkey
monkey.patch_all()

Calling the Web API

We also expose the Web API on it. So you can simply:

from bigslacker import BigSlacker
 
token = '1239182918sxxusus'
bs = BigSlacker(token)
bs.api_call("api.test")

 

Check the source code on GitHub or simply install it with `pip install bigslacker`.

Fleepy – A Pythonic Fleep API Client

For whoever doesn’t know, Fleep is a chat application that focuses on being simple, fast and has the ambition of replacing your email. Having used Fleep, Slack and Hipchat, they all have their places. But if you or your team deal with a lot of contacts outside your organization, Fleep it’s certainly something to consider as E-Mail integration is quite a powerful feature as it allows you to add email-based guests to conversation without requiring them to have a Fleep account.

That said, I am developing a ongoing project and for that I would need access to Fleep API. It is a simple RESTful API, however I could not find a proper Python client for it. So I coded this library in a couple days that access Fleep.

Its usage is quite simple and tries to stay as closely to the API as possible.

Here are a few examples:

Creating a chat room with topic and an initial message.

The example below logins, creates a chat room with three people and sends a message. The message uses some of Fleep styling features, check them out here.

 

from fleepy import Fleepy
 
api = Fleepy()
api.account.login("your@email.com", "yourpassword")
 
 
api.conversation.create(
    topic='This is a Room Topic',
    emails=['your@email.com', 'guest1@email.com', 'guest2@email.com'],
    message="""*Hello*, everyone!
 
    Something has just been posted in our Issue tracker.
 
    http://issue.tracker.com<>
    """)
 
api.account.logout()

Uploading a file

from fleepy import Fleepy
 
api = Fleepy()
api.account.login("your@email.com", "yourpassword")
 
api.file.upload('/path/to/afile.jpg')
api.account.logout()

Check out the code at https://github.com/nicholasamorim/fleepy and let me know of any issues or features you might like. :)

Software Engineering/Architecture Resources

Disclaimer: Strong focus on Python.

So, I thought I could share an amazing collection of videos from conferences and engineering blogs from big companies (think Instagram, PayPal, Dropbox, etc…). No secrets or big deal, just a centralized place with some rather nice resources.

There is not a lot of videos on tool “X” or “Y”, but rather a lot of high-level conceptual talks in terms of videos. That’s my particular choice when looking for something to watch. I like to believe that when we understand the deeper roots of a particular issue, all the rest are just tools. In another words, be on the lookout if it’s really a nail, only then, run for the hammer.

However, the engineering blogs usually get a bit more into how they used a specific tool or another. But the important part is always try to understand why they chose something over another thing. It’s funny to realize the mistakes they make sometimes when we could have thought that in those big flashy nice companies, things like that would hardly happen.

A big role is also played in the balance of risk-taking when going for new (although proven) technologies. That can be a real advantage in such competitive times and they know that.

And well, I just thought someone might make good use of all of this. This is knowledge in what I believe is its purest form: community-driven. So I’m trying to pass it along as well. 

If you know of something else, let me know in the comments and I’ll update the list.

For the videos, I’ve created a playlist (which I will keep adding stuff as I find/watch):

https://www.youtube.com/playlist?list=PLm5JiQ4yORZoml627aVKiNa4SO3g9p0l8

And here is the engineering blog list (I use Feedly to keep track of the updates):

http://nerds.airbnb.com/
http://techblog.netflix.com/
https://www.paypal-engineering.com/
http://blog.echen.me/
https://codeascraft.com/
https://blogs.dropbox.com/tech/
http://engineering.tumblr.com/
http://code.flickr.net/
http://engineering.quora.com/
https://blog.twitter.com/
http://engineering.foursquare.com/
http://engineering.heroku.com/
http://highscalability.com/
http://instagram-engineering.tumblr.com/
http://engineering.linkedin.com/blog
http://engineering.pinterest.com/
http://feeds.feedburner.com/soundcloudapiblog
https://corner.squareup.com/
https://github.com/blog
http://engineeringblog.yelp.com/

Here, we have two Planets (blog aggregators), useful for Python dev:

http://planetpython.org/
http://planet.twistedmatrix.com/

A conference video aggregator of Python stuff:

http://pyvideo.org/

And just in any case, if you don’t know there are a few free (very) good Python books online:

Dive into Python
Think Python
The Hitchhiker’s Guide to Python
Think Complexity
Think Stats
Guide to Data Mining
Test-Driven Development with Python
Text Processing in Python
BioPython Tutorial and Cookbook
Guide to Python Standard Library
Natural Language Processing with Python
Programming Computer Vision with Python

Not Python, but awesome:

The Architecture of Open Source Applications

Python and Algorithms:

Problem Solving with Algorithms and Data Structures
Data Structures and Algorithms with Object-Oriented Design Patterns in Python

Programming in general:

Free Programming Books

txsh 0.2

txsh is a dynamic wrapper around Twisted ProcessProtocol and spawnProcess that allows you to call any program as if it were a function and return a deferred with its exit code and output. If you wanna know what it does, check out my first release post.

Version 0.2 introduces a few new features and bug fixes.

– Support to subcommands. It looks nice to be able to do “git.branch(“-v”)” instead of  “git(“branch”, “-v”)”

– Redirection of stderr and stdout to any file-like object, deferred, deffered queue or callable. If a string is passed, we will assume it’s a filename we can open and write to it.

– Added a few unit tests.

– Fixed a bug where the subcommand was not being properly passed on .bake call.

Examples:

from txsh import ls, curl, wc, git, sudo
 
# arguments should go separated
d = ls("-l", "-h") # ls -l -h
 
# Keyword arguments are also supported
d = ls(help=True)  # ls --help
 
# Underscores will be replaced by dashes
d = curl(connect_timeout=10, url="http://something")
# curl --connect-timeout 10 --url http:/something
 
# You can pipe
d = wc(ls())
 
# You can have subcommands
d = git.branch()  # Same as git("branch")
d = sudo.ls("-h")  # Same as sudo("ls", "-h")
 
# You can bake
ll = ls.bake("-l", "-h")
d = ll()  # Now ll will always output ls -l -h
 
# You can redirect stderr or stdout to a file using special args _out and _err
d = ls("-l", _out=open('output.log', 'wb'))
 
# In fact, you can use any file-like object like a StringIO.
 
# A callabble.
def alert(error):
    pass  # Do something
 
d = ls("-l", _err=alert) # Will redirect stderr to alert function.
 
# If you pass a string, we will simply assume it's a filename.
d = ls("-l", _out="output.log", _err="error.log")
 
# You can also pass a DeferredQueue or a simple Deferred.
queue = DeferredQueue()
my_defer = Deferred()
d = ls("-l", _out=queue, _err=my_defer)
# When stdout is ready, it will call queue.put
# When stderr is ready, it will call my_defer.callback

The new release is on PyPI, so you can install it with pip. Check out the project page! Hope it’s useful for some.

txsh – A Twisted Wrapper to Processes

txsh is a project *largely* inspired by sh.

txsh is a dynamic wrapper around Twisted ProcessProtocol and spawnProcess that allows you to call any program as if it were a function and return a deferred with its exit code and output. A basic example:

from twisted.internet import reactor
from txsh import ls
 
def my_callback(exc_info):
    print 'Exit Code:', exc_info.status
    print 'Output:', exc_info.stdout
    print 'Errors:', exc_info.stderr
    reactor.stop()
 
d = ls()
d.addCallback(my_callback)
 
reactor.run()

txsh does not implement “ls”. The lookup is dynamic. You can import curl, wc, whatever – well, provided you have an application called “whatever”.

Twisted API for processes is great but not necessarily practical and I believe this abstracts a lot of use cases. txsh, so far, supports arguments, named arguments, piping and baking. But there is still a lot to be done.

Some more examples:

from txsh import ls, curl, wc
 
# arguments should go separated
d = ls("-l", "-h") # ls -l -h
 
# Keyword arguments are also supported
d = ls(help=True)  # ls --help
 
# Underscores will be replaced by dashes
d = curl(connect_timeout=10, url="http://something")  # curl --connect-timeout 10 --url http:/something
 
# You can pipe
d = wc(ls())
 
# You can bake
ll = ls.bake("-l", "-h")
d = ll()  # Now ll will always output ls -l -h

Using pip ?

pip install txsh

If someone has any suggestions or critiques, please leave them in the comments. Even better, open an issue in the project.