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`.