Rocket.Chat API Wrapper

3.5.0 · active · verified Thu Apr 16

rocketchat-api is a Python API wrapper for Rocket.Chat, allowing developers to programmatically interact with Rocket.Chat instances. It provides a convenient interface to various Rocket.Chat API endpoints, abstracting away direct HTTP requests. The library is actively maintained, with a typical release cadence of minor versions every 1-2 months, and its current version is 3.5.0.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the RocketChatAPI client using environment variables for credentials and connects to a Rocket.Chat instance. It then demonstrates fetching the current user's information and sending a public message. Ensure you replace 'your_username' and 'your_password' with actual credentials or set the corresponding environment variables.

import os
from rocketchat_API.rocketchat import RocketChatAPI

# Configure connection details and credentials via environment variables for security
ROCKETCHAT_SCHEME = os.environ.get('ROCKETCHAT_SCHEME', 'https')
ROCKETCHAT_HOST = os.environ.get('ROCKETCHAT_HOST', 'demo.rocket.chat')
ROCKETCHAT_PORT = os.environ.get('ROCKETCHAT_PORT', '443') # Note: Port should be string for consistent config
ROCKETCHAT_USER = os.environ.get('ROCKETCHAT_USER', 'your_username')
ROCKETCHAT_PASS = os.environ.get('ROCKETCHAT_PASS', 'your_password')

rocket = RocketChatAPI(settings={
    'scheme': ROCKETCHAT_SCHEME,
    'host': ROCKETCHAT_HOST,
    'port': int(ROCKETCHAT_PORT), # Ensure port is int if expected by library
    'username': ROCKETCHAT_USER,
    'password': ROCKETCHAT_PASS
})

# Example: Get information about the current user
try:
    me_info = rocket.me().json()
    print(f"Logged in as: {me_info.get('username')}")
except Exception as e:
    print(f"Error connecting or authenticating: {e}")

# Example: Send a direct message
try:
    if me_info and 'username' in me_info:
        # Replace 'otheruser' with a valid Rocket.Chat username to send a message to
        response = rocket.chat_post_message(room_id='GENERAL', text='Hello from rocketchat-api!').json()
        print(f"Message sent status: {response.get('success')}")
except Exception as e:
    print(f"Error sending message: {e}")

view raw JSON →