Skip to main content

An easy to use bot library for the Matrix ecosystem written in Python.

Project description

Simple-Matrix-Bot-Lib

(Version 1.5.x)

simplematrixbotlib is a Python 3 library for quickly building Matrix bots. It uses matrix-nio as its Matrix client library.

View on Github or View on PyPi or View docs on readthedocs.io

Installation

To use it, simplematrixbotlib can be either installed from pip or downloaded from github.

Installation from pip:

python -m pip install simplematrixbotlib

Download from github:

git clone --branch master https://github.com/KrazyKirby99999/simple-matrix-bot-lib.git

Example Usage

import simplematrixbotlib as botlib
import os

creds = botlib.Creds("https://home.server", "user", "pass")
bot = botlib.Bot(creds)

prefix = '!'

async def echo(room, message):
    """
    Example function that "echoes" arguements.
    Usage:
    example_user- !echo say something
    echo_bot- say something
    """
    match = botlib.MessageMatch(room, message, bot)
    if match.not_from_this_bot() and match.prefix(prefix) and match.command("echo"):
        await bot.api.send_text_message(room.room_id, match.args)

bot.add_message_listener(echo)

bot.run()

More examples can be found here.

Features

Complete:

  • Login to homeserver - bot automatically login upon the execution of bot.run()

    import simplematrixbotlib as botlib
    
    creds = botlib.Creds("home.server", "user", "pass")
    bot = botlib.Bot(creds)
    bot.run() #Logs in during the execution of this line
    
  • Join room on invite - bot automatically join rooms that the bot is invited to upon execution of bot.run(), or upon invite if the bot is running

  • Send message - bot can send messages in response to other messages, and can also run other code in response to messages as well as filter the messages that the bot responds to

    import simplematrixbotlib as botlib
    
    creds = botlib.Creds("https://home.server", "user", "pass")
    bot = botlib.Bot(creds)
    
    async def say_something_to_a_message_not_from_bot(room, message): #Must be an "async" function with (room, message) arguments
        if not message.sender == bot.async_client.user_id: #Optional, prevents the bot from reacting to its own messages
            await bot.api.send_text_message(room.room_id, "something") #Send a message containing "something" to room
    bot.add_message_listener(say_something_to_a_message_not_from_bot) #Listen for messages, can have as many message listeners as needed, each added using bot.add_message_listener
    
    bot.run()
    
  • Execute action based on criteria - "match filters" can be used to specify which messages for the bot to respond to

    import simplematrixbotlib as botlib
    import os
    
    creds = botlib.Creds("https://home.server", "user", "pass")
    bot = botlib.Bot(creds)
    
    prefix = '!' #Create prefix for commands
    
    async def echo(room, message):
        """
        Example function that "echoes" arguements.
        Usage:
        example_user- !echo say something
        echo_bot- say something
        """
        match = botlib.MessageMatch(room, message, bot) #Create an object of the botlib.MessageMatch class
        if match.not_from_this_bot() and match.prefix(prefix) and match.command("echo"): #Add match filters
            await bot.api.send_text_message(room.room_id, match.args)#Execute action
    bot.add_message_listener(echo)
    
    bot.run()
    
  • Preserve sessions - Sessions are now preserved between logins. The access token and device id are now saved in sessions.txt, unless specified otherwise.

    import simplematrixbotlib as botlib
    import os
    
    creds = botlib.Creds("https://home.server", "user", "pass", None) #Disable preserved sessions
    bot = botlib.Bot(creds)
    
    prefix = '!'
    
    async def echo(room, message):
        """
        Example function that "echoes" arguements.
        Usage:
        example_user- !echo say something
        echo_bot- say something
        """
        match = botlib.MessageMatch(room, message, bot)
        if match.not_from_this_bot() and match.prefix(prefix) and match.command("echo"):
            await bot.api.send_text_message(room.room_id, match.args)
    bot.add_message_listener(echo)
    
    bot.run()
    
  • Add choice of actions to execute at bot login - Execute action after logging in

    import simplematrixbotlib as botlib
    import os
    
    creds = botlib.Creds("https://home.server", "random_bot", "pass")
    bot = botlib.Bot(creds)
    
    prefix = '!'
    
    async def hello(room_id): #Must be an "async" function with a (room_id) argument
        """
        Example function that says "hello" when the bot is started.
        Usage:
        (start random_bot)
        random_bot - hello
        """
        message = "hello"
        await bot.api.send_text_message(room_id, message) #Example of sending a message
    
    bot.add_startup_action(hello) #Add "hello" action to action to execute at login
    
    bot.run()
    

In Progress:

  • Add more examples

  • Improve Documentation

Planned:

  • Add more match filters

  • Support for Encrypted Rooms

  • More

Dependencies

Python:

  • matrix-nio >= 0.18.2

External:

  • Python >= 3.7

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

simplematrixbotlib-1.5.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

simplematrixbotlib-1.5.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file simplematrixbotlib-1.5.2.tar.gz.

File metadata

  • Download URL: simplematrixbotlib-1.5.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for simplematrixbotlib-1.5.2.tar.gz
Algorithm Hash digest
SHA256 cd60ca93e48df4abc3a0542fbf83af0b655f941abec6debffec84f14b641a052
MD5 5d08c086f808c6ab3616073ca0b09cd6
BLAKE2b-256 499a44ac6555058c6cd9ce4af5c93886d9c42c083b222310cb9f109eebbeba2d

See more details on using hashes here.

File details

Details for the file simplematrixbotlib-1.5.2-py3-none-any.whl.

File metadata

  • Download URL: simplematrixbotlib-1.5.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for simplematrixbotlib-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8f15f33fd79c5342cb2284f09a51f6d45dc9098963c4d007d4b2955ce1a6a60
MD5 203e6576b72e8600056d40fe438af7cf
BLAKE2b-256 c82b0a0a5e4107e8f7f49e64643e01a669e7c522c986eacd74919e4cf8eb2842

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