DEV Community

Jorge Alberto Díaz Orozco (Akiel)
Jorge Alberto Díaz Orozco (Akiel)

Posted on

Talking to Rocket.Chat with Python

I meet Rocket.Chat about a year ago and in my opinion it is one of the best team oriented chat software I've ever tried. It has a clean and beautiful interface, supports Markdown and emojis. It's awesome to share code and let's you pin important messages on your channels. It is an open alternative to software like Slack or Mattermost and you can perfectly run it on your own infrastructure. I fell in love on how easily I could deploy it and started to play with it and to hang around demo.rocket.chat where I received great support. Since I'm a developer and I'm interested on automating things, I wanted to make bots to make life easier to my friends, and I started to dig searching for a good Rocket.Chat API wrapper for Python or a bot API like Telegram's but failed.
Turns out that there was no mechanism to make a bot but using the internal Hubot and API wrappers were old, lack of coverage or were complex to develop in, so I decided to start writing my own and use it to write a bot.
So this is the result: https://github.com/jadolg/rocketchat_API. It's nothing fancy, believe me. It just took time to read the wonderful documentation about the REST API and wrap it the must lazy way I could so I didn't have to review the docs every 5 minutes.
Want to give it a try?
Lets start installing from pypi pip3 install rocketchat_API and after creating an account on the demo instance let's try to connect and write a message on the #general room.

from rocketchat_API.rocketchat import RocketChat

rocket = RocketChat('user', 'pass', server_url='https://demo.rocket.chat')
rocket.chat_post_message('good news everyone!', channel='GENERAL')
Enter fullscreen mode Exit fullscreen mode

Awesome right? And since it's built on top of the requests library, every call to the API will return a requests Response object and has included proxy support.
Do you want to see the list of channels? It's as simple as this:

from pprint import pprint

pprint(rocket.channels_list().json())
Enter fullscreen mode Exit fullscreen mode

Every method has the same documentation string as in the official Rocket.Chat API documentation and it's called similar so you can actually use the official docs while working with the library transparently.
And that's all folks. I hope it's of good use for you and if you are interested in cuntributing, please, send me pull requests, issues, stars :-)

Top comments (0)