rocketchat_API Python Wrapper

repository·master·Indexed 43 days ago

https://github.com/jadolg/rocketchat_api

A Python API wrapper for the Rocket.Chat REST API that allows developers to interact with Rocket.Chat instances programmatically. It provides a RocketChat client class with organized sections for users, chat, channels, rooms, and server settings, supporting both Basic and Token-Based authentication.

Tokens
2.1K
Snippets
8
Records
10
Agent score
55%

What's inside rocketchat_API

  1. Pass additional parameters to Rocket.Chat methods

    master
    While only required parameters are explicitly defined in the RocketChat class methods, you can pass any additional parameters supported by the official Rocket.Chat REST API directly into the method calls. This allows you to utilize features not explicitly wrapped by the library.
  2. Install rocketchat_API

    master

    You can install the library via PyPI (recommended) or directly from the source code.

    Requirements:

    • Python 3.10 or higher
    • requests library
    # From PyPI
    pip install rocketchat_API
    
    # From source
    git clone https://github.com/jadolg/rocketchat_API.git
    cd rocketchat_API
    pip install .
  3. Access RocketChat API sections

    master

    The RocketChat client provides access to a wide range of specialized API modules through its attributes. Instead of calling a single flat API, functionality is organized into logical sections. Available sections include:

    • users: User management and engagement
    • chat: Chatting and messaging
    • channels: Channel management
    • rooms: Room operations
    • dm: Direct messages
    • groups: Group management
    • roles & permissions: Access control
    • integrations: Integration settings
    • teams: Team management
    • livechat: Livechat features
    • moderation: Moderation tools
    • settings: Server settings
    • statistics: Server statistics
    • assets, banners, custom_emoji, custom_sounds, custom_user_status: Asset and customization management
  4. Authenticate using Token-Based Authentication

    master

    If you have a pre-existing user ID and authentication token, initialize the RocketChat class using the user_id and auth_token parameters instead of credentials.

    from rocketchat_API.rocketchat import RocketChat
    
    rocket = RocketChat(
        user_id='WPXGmQ64S3BXdCRb6',
        auth_token='jvNyOYw2f0YKwtiFS06Fk21HBRBBuV7zI43HmkNzI_s',
        server_url='https://demo.rocket.chat'
    )
  5. Authenticate using Basic Authentication

    master

    To use Basic Authentication, initialize the RocketChat class with your username, password, and the server_url of your Rocket.Chat instance.

    from rocketchat_API.rocketchat import RocketChat
    
    rocket = RocketChat('user', 'pass', server_url='https://demo.rocket.chat')
  6. Get current user info, list channels, post messages, and get history

    master

    Commonly used methods in the RocketChat class:

    • me(): Retrieves information about the currently authenticated user.
    • channels_list(): Returns a list of available channels. Each channel is returned as a dictionary.
    • chat_post_message(text, channel, alias=None): Posts a message to a specific channel. You can optionally provide an alias.
    • channels_history(channel, max_count=None): Retrieves the message history for a specific channel.
    from rocketchat_API.rocketchat import RocketChat
    
    rocket = RocketChat('user', 'pass', server_url='https://demo.rocket.chat')
    
    # Get current user info
    rocket.me()
    
    # List all channels
    for channel in rocket.channels_list():
        print(channel.get("name"))
    
    # Post a message
    rocket.chat_post_message('good news everyone!', channel='GENERAL', alias='Farnsworth')
    
    # Get channel history
    rocket.channels_history('GENERAL', max_count=5)
  7. Configure MongoDB for Rocket.Chat replica set

    master

    The mongo service uses bitnamilegacy/mongodb:8.0 and must be configured as a replica set to support Rocket.Chat's requirements.

    Required environment variables for the MongoDB service:

    • MONGODB_REPLICA_SET_MODE: Set to primary.
    • MONGODB_REPLICA_SET_NAME: Set to rs0 (must match the replicaSet parameter in Rocket.Chat's MONGO_URL).
    • MONGODB_ADVERTISED_HOSTNAME: Set to mongo to ensure the service is reachable by the Rocket.Chat container.
    • ALLOW_EMPTY_PASSWORD: Set to yes for development/local setups.
    services:
      mongo:
        image: docker.io/bitnamilegacy/mongodb:8.0
        environment:
          MONGODB_REPLICA_SET_MODE: primary
          MONGODB_REPLICA_SET_NAME: rs0
          MONGODB_ADVERTISED_HOSTNAME: mongo
          ALLOW_EMPTY_PASSWORD: yes
  8. Configure Rocket.Chat via Docker Compose environment variables

    master

    When deploying Rocket.Chat using Docker Compose, you can control various application behaviors and security settings using environment variables in the rocketchat service.

    Key configuration options include:

    • ROOT_URL: The base URL for the Rocket.Chat instance (e.g., http://localhost:3000).
    • MONGO_URL: The connection string for the primary MongoDB database.
    • MONGO_OPLOG_URL: The connection string for the MongoDB oplog, required for replica set operations.
    • CREATE_TOKENS_FOR_USERS: Set to true to automatically generate tokens for users.
    • CREATE_TOKENS_FOR_USERS_SECRET: The secret used for generating user tokens.
    • ROCKETCHAT_LICENSE: The license key for Rocket.Chat.
    • OVERWRITE_SETTING_API_Enable_Rate_Limiter: Set to false to disable the API rate limiter.
    • OVERWRITE_SETTING_Accounts_TwoFactorAuthentication_Enforce_Password_Fallback: Set to false to disable enforced password fallback for 2FA.
    services:
      rocketchat:
        image: rocketchat/rocket.chat:${ROCKET_CHAT_VERSION:-latest}
        environment:
          - PORT=3000
          - ROOT_URL=http://localhost:3000
          - MONGO_URL=mongodb://mongo:27017/rocketchat?replicaSet=rs0
          - MONGO_OPLOG_URL=mongodb://mongo:27017/local?replicaSet=rs0
          - CREATE_TOKENS_FOR_USERS=true
          - CREATE_TOKENS_FOR_USERS_SECRET=1234567890abcdef
          - ROCKETCHAT_LICENSE=${ROCKETCHAT_LICENSE}
  9. Skip the Rocket.Chat onboarding wizard via MongoDB

    master

    If the standard flag to disable the onboarding/registration wizard fails, you can bypass it by manually updating the rocketchat_settings collection in MongoDB. Run the following command in the MongoDB shell to mark the setup wizard as completed:

    use rocketchat
    db.rocketchat_settings.updateOne(
      {"_id": "Show_Setup_Wizard"},
      {$set: {"value": "completed"}},
      {upsert: true}
    );
  10. Initialize the RocketChat client

    master

    The RocketChat class is the primary entry point for interacting with the Rocket.Chat REST API. It uses multiple inheritance to aggregate various API sections (such as RocketChatUsers, RocketChatChat, RocketChatChannels, etc.) into a single client object. To use it, you must instantiate the RocketChat class, which typically requires authentication credentials (though specific constructor arguments are defined in the inherited base classes).

    from rocketchat_API.rocketchat import RocketChat
    
    # Note: Initialization arguments depend on the underlying implementation
    # of the inherited base classes (e.g., URL, auth tokens, etc.)
    client = RocketChat(
        # parameters go here
    )