Skip to content

Latest commit

 

History

History
105 lines (80 loc) · 2.91 KB

README.md

File metadata and controls

105 lines (80 loc) · 2.91 KB

rocketbot

Python framework to build RocketChat bot

GitHub license GitHub stars


Table of contents

1. Installation

pip install git+https://github.com/ntk148v/rocketbot.git

2. Usage

Check out the sample.

2.1. Create new bot

import os

from rocketbot import RocketBot

username = os.environ.get('ROCKET_USERNAME')
password = os.environ.get('ROCKET_PASSWORD')
server_url = os.environ.get('ROCKET_SERVER_URL')

rocket = RocketBot(user=username, password=password,
                   server_url=server_url, ssl_verify=False,
                   session=requests.sessions.Session(),
                   threading_updates=True)

2.2. Create a new plugin (command)

You can add a new plugin to initialized bot by using decorator add_command. add_command requires 2 arguments:

  • The regex that bot will respond if matching.
  • The bot's usage.
@rocket.add_command(r'/start', usage='Start working with bot')
def start(message, *args):
    rocket.send_message(
        message['rid'], f"hi @{message['u']['username']}, let's start")

2.3. Custom logger configuration

By default, RocketBot writes log to stderr with default level is "INFO". You can customize it by passing a configuration dict. You can refer to loguru's offical document.

rocket = RocketBot(user=username, password=password,
                   server_url=server_url, ssl_verify=False,
                   session=requests.sessions.Session(),
                   logger_config={
                       "handlers": [
                           {"sink": sys.stdout, "format": "{time} - {message}"},
                           {"sink": "/tmp/file.log", "serialize": True},
                       ],
                   },
                   threading_updates=True)

2.4. Run it

rocket.run(sleep=10)

3. Credits

This library is highly inspired by: