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 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!
Metadata
Release files for python-matrixbot 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-matrixbot-0.0.7.tar.gz | 8.7 kB | Without provenance | Details for python-matrixbot-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_matrixbot-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for python_matrixbot-0.0.7-py3-none-any.whl |
Total release size: 19.9 kB
Release files / python-matrixbot-0.0.7.tar.gz
| Download URL | python-matrixbot-0.0.7.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9412981b14ff3ab7ffbb1bfc1691758113ab8d71f731b3093d8808c286b69c71
|
|
BLAKE2b-256 checksum How to use checksums |
5f6f9487ea2214593bdbec5b12143aa21d90553f0763da7e0efb170297e2c0b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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.4
|
Release files / python_matrixbot-0.0.7-py3-none-any.whl
| Download URL | python_matrixbot-0.0.7-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
27a6ee03548e07a56d06b2842e8407fcdf7f1841c3d6bd64a26e46516f43d524
|
|
BLAKE2b-256 checksum How to use checksums |
79252b46ca246d79523d6c26e368ce9f4be811788841da7578c56e634209acea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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.4
|
Security for python-matrixbot 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.