Skip to main content

asyncio-based Python wrapper for the Rocket.Chat Realtime API.

Project description

⚠️ DEPRECATED

Rocket.Chat's RealTime API is deprecated. As a result, I will not maintain this package going forward.

(If I wrote this library today, I'd probably do it very differently :) I suggest you do not take inspiration from it.)

rocketchat-async

asyncio-based Python wrapper for the Rocket.Chat Realtime API.

Supported Rocket.Chat versions: 7.x. (The library might also work or partially work with other versions.)

When should you use this library?

Use this library if you:

  • want to integrate with Rocket.Chat from Python
  • are using asyncio to drive your code
  • want to use Rocket.Chat's efficient websockets-based Realtime API

Installation

pip install rocketchat-async

Example usage

import asyncio
import random
from rocketchat_async import RocketChat


def handle_message(channel_id, sender_id, msg_id, thread_id, msg, qualifier,
                   unread, repeated):
    """Simply print the message that arrived."""
    print(msg)


async def main(address, username, password):
    while True:
        try:
            rc = RocketChat()
            await rc.start(address, username, password)
            # Alternatively, use rc.resume for token-based authentication:
            # await rc.resume(address, username, token)

            # A possible workflow consists of two steps:
            #
            # 1. Set up the desired callbacks...
            for channel_id, channel_type in await rc.get_channels():
                await rc.subscribe_to_channel_messages(channel_id,
                                                       handle_message)
            # 2. ...and then simply wait for the registered events.
            await rc.run_forever()
        except (RocketChat.ConnectionClosed,
                RocketChat.ConnectCallFailed) as e:
            print(f'Connection failed: {e}. Waiting a few seconds...')
            await asyncio.sleep(random.uniform(4, 8))
            print('Reconnecting...')


# Side note: Don't forget to use the wss:// scheme when TLS is used.
asyncio.run(main('ws://localhost:3000/websocket', 'username', 'password'))

API Overview

Brief overview of the currently implemented methods.

As of now, Rocket.Chat's API is only covered partially (based on my original needs). I am open to both feature requests as well as pull requests.

Methods

RocketChat.get_channels()

Get a list of channels the logged-in user is currently member of.

RocketChat.send_message(text, channel_id, thread_id=None)

Send a text message to a channel.

RocketChat.send_reaction(orig_msg_id, emoji)

Send a reaction to a specific message.

RocketChat.send_typing_event(channel_id, thread_id=None)

Send the "typing" event to a channel or to a specified thread within that channel.

RocketChat.subscribe_to_channel_messages(channel_id, callback)

Subscribe to all messages in the given channel. Returns the subscription ID.

The provided callback should accept eight arguments: channel_id, sender_id, msg_id, thread_id, msg_text, msg_qualifier and repeated. The qualifier can help to determine if e.g. the message is a system message about the user being removed from the channel. The repeated flag assists in distinguishing whether the message has been received again as a result of thread replies or reactions, or if it is a new message post.

RocketChat.subscribe_to_channel_changes(callback)

Subscribe to all changes in channels. Returns the subscription ID.

The provided callback should accept two arguments: channel_id and channel_qualifier. The qualifier helps to determine e.g. if it's a direct message or a normal room.

RocketChat.subscribe_to_channel_changes_raw(callback)

Like RocketChat.subscribe_to_channel_changes except the callback gets passed the raw message object coming from the API.

RocketChat.subscribe_to_channel_messages_raw(channel_id, callback)

Like RocketChat.subscribe_to_channel_messages except the callback gets passed the raw message object coming from the API.

RocketChat.get_channels_raw()

Like RocketChat.get_channels except the method returns the list of raw channel objects coming from the API.

RocketChat.unsubscribe(subscription_id)

Cancel a subscription.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rocketchat_async-4.3.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rocketchat_async-4.3.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file rocketchat_async-4.3.0.tar.gz.

File metadata

  • Download URL: rocketchat_async-4.3.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.5 Linux/6.12.48+deb13-amd64

File hashes

Hashes for rocketchat_async-4.3.0.tar.gz
Algorithm Hash digest
SHA256 733bd4e0b2863b1ffcee97646e77f3d6970e9535e1906bc1cafdcd00ff5a8dba
MD5 a6c2eeca3d1943dd2f083c646004778d
BLAKE2b-256 9251b4691b0aa3d2c39d2accae9dc2f3aff10051ba43f8effbe50c6c969bc33e

See more details on using hashes here.

File details

Details for the file rocketchat_async-4.3.0-py3-none-any.whl.

File metadata

  • Download URL: rocketchat_async-4.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.5 Linux/6.12.48+deb13-amd64

File hashes

Hashes for rocketchat_async-4.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39e8f2543be8328c0e1b730f3103b15c353ed5ffd4c79ef2ac81a1010b1db5c0
MD5 f62d75d49687796ddf7b5543940d1a7c
BLAKE2b-256 b0e3938aaf6575b60ca5e97d27353f722567b0e97b6e162e5b8af9483ce6d9bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page