Skip to main content

A basic bot for Matrix

Project description

python-matrixbot

A Python module meant to act as a base class for a Matrix bot.

Compatibility

Python 3.6+

Installation

pip install python-matrixbot

Usage

The MatrixBot class will connect to the Matrix server, start a listener on each joined room, and listen for room invites from other users. It also includes helper methods you can use to extend the functionality. It is built on the Matrix Python SDK which can be directly accessed via MatrixBot.client

from matrixbot import MatrixBot

# create a new bot
bot = MatrixBot(
    host="example.com",
    display_name="Example Bot",
    token=access_token,
    user_id="@bot:example.com"
)
# run forever
bot.start()

Get a list of joined rooms:

room_ids = bot.rooms.keys()
room_objects = bot.rooms.values()

Leave abandoned 1-on-1 rooms

for room in bot.rooms.values():
    bot.leave_if_empty(room)

Cache results from external API calls

To prevent abuse of commands that rely on external API calls, MatrixBot includes a very basic caching mechanism. Wrap all API calls in MatrixBot.fetch() to return cached results and automatically refresh stale data.

def get_url(url):
  return response.get(url)

result = bot.fetch(
    "cache_key", lambda: get_url("https://example.com", timeout=900)
)

By default the module uses an in-memory cache which is lost between restarts. To create a persistent cache provide the constructor with the path to a sqlite database:

bot = MatrixBot(
    ...
    cache_db="/path/to/sqlite_database"
)

Sub-classing

Create a subclass to extend and override default features

class MyBot(MatrixBot):
    # override `process_message()` to do filter/parse/react to room events:
    def process_message(self, room, event):
        # say hello when a user joins the room
        if event["type"] == "m.room.member":
            if event["content"]["membership"] == "join":
                self.say(room, "Hello!", mention=event["sender"])

    # override `accept_invite()` to disable auto-join:
    def accept_invite(self, room_id):
        pass

bot = MyBot(...)

License

MIT

Maintainer

Brian Ó blacksam@gibberfish.org

Contributions welcome.

About Gibberfish

Gibberfish, Inc is a volunteer nonprofit that develops free tools to promote online privacy for activists and regular people alike. If you like this module and decide to use it in your project, please consider donating $1 to help keep us going. Thanks!

https://gibberfish.org/donate/

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

python-matrixbot-0.0.5.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

python_matrixbot-0.0.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file python-matrixbot-0.0.5.tar.gz.

File metadata

  • Download URL: python-matrixbot-0.0.5.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for python-matrixbot-0.0.5.tar.gz
Algorithm Hash digest
SHA256 9a9a73862d25918398c5431bf0ce660ce661cc82a5b7768fa4c57b8a6bfbe90a
MD5 3cc491a1a227bb2e34741c13c2fdddda
BLAKE2b-256 b8f33c7b3897c62101ce1ae62d62f6595dab89fe72d6814a6281b35a1b795114

See more details on using hashes here.

File details

Details for the file python_matrixbot-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: python_matrixbot-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for python_matrixbot-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8551f289039cd35f6a223284094df2847776de2c924479e8563635b04bd03836
MD5 c3eada000b81cd25ffaeddd59a2a1182
BLAKE2b-256 0d49c06fa49d06601038678db06480a192271390fe14ddec185cc9b773404b9d

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