Examples

A Basic Echo Bot

"""
Example Usage:

random_user
      !echo something

echo_bot
      something
"""

import simplematrixbotlib as botlib
import os

creds = botlib.Creds("https://example.org", "echo_bot", "secretpassword")
bot = botlib.Bot(creds)

prefix = '!'


async def echo(room, message):
    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()

A “High-Five” Bot

"""
Example Usage:

random_user
      !count

echo_bot
      The bot has been high-fived 10 times!

random_user
      !high_five

echo_bot
      random_user high-fived the bot!"

random_user
      !count

echo_bot
      The bot has been high-fived 11 times!
"""

import os

creds = botlib.Creds("https://example.org", "hight_five_bot", "secretpassword")
bot = botlib.Bot(creds)

PREFIX = '!'

try:
    with open("high_fives.txt", "r") as f:
        bot.total_high_fives = int(f.read())
except FileNotFoundError:
    bot.total_high_fives = 0


async def bot_help(room, message):
    bot_help_message = f"""
    Help Message:
        prefix: {PREFIX}
        commands:
            help:
                command: help, ?, h
                description: display help command
            give high fives:
                command: high_five, hf
                description: high-five the bot!
            count:
                command: count, how_many, c
                description: show amount of high fives
                """
    match = botlib.MessageMatch(room, message, bot)
    if match.not_from_this_bot() and match.prefix(PREFIX) and (
            match.command("help") or match.command("?") or match.command("h")):
        await bot.api.send_text_message(room.room_id, bot_help_message)


bot.add_message_listener(bot_help)


async def high_five(room, message):
    match = botlib.MessageMatch(room, message, bot)
    if match.not_from_this_bot() and match.prefix(PREFIX) and (
            match.command("high_five") or match.command("hf")):

        bot.total_high_fives += 1
        with open("high_fives.txt", "w") as f:
            f.write(str(bot.total_high_fives))

        await bot.api.send_text_message(
            room.room_id, f"{message.sender} high-fived the bot!")


bot.add_message_listener(high_five)


async def high_five_count(room, message):
    match = botlib.MessageMatch(room, message, bot)
    if match.not_from_this_bot and match.prefix(PREFIX) and (
            match.command("count") or match.command("how_many")
            or match.command("c")):
        await bot.api.send_text_message(
            room.room_id,
            f"The bot has been high-fived {str(bot.total_high_fives)} times!")


bot.add_message_listener(high_five_count)

bot.run()

A Rock Paper Scissors Bot

"""
Example Usage:

random_user
    !help

rock_paper_scissors_bot
    Rock Paper Scissors Bot Help
    ============================
    What is this bot?
        Rock Paper Scissors Bot is a Matrix bot that plays rock paper scissors with room members and is written in Python using the simplematrixbotlib package.
    Commands?
        !help - show this message
        !play <rock/paper/scissors> - play the game by making a choice

random_user
    !play paper

rock_paper_scissors_bot
    You choose paper.
    The bot chose rock.
    You Won!

"""

import simplematrixbotlib as botlib
import os
import random

creds = creds = botlib.Creds("https://example.org", "rock_paper_scissors_bot",
                             "secretpassword")
bot = botlib.Bot(creds)

PREFIX = '!'


async def help_message(room, message):
    match = botlib.MessageMatch(room, message, bot)
    if not (match.not_from_this_bot() and match.prefix(PREFIX)
            and match.command("help")):
        return

    message = (f"""
    Help
    ============================
    What is this bot?
        Rock Paper Scissors Bot is a Matrix bot that plays rock paper scissors with room members and is written in Python using the simplematrixbotlib package.
    Commands?
        {PREFIX}help - show this message
        {PREFIX}play <rock/paper/scissors> - play the game by making a choice
    """)

    await bot.api.send_text_message(room.room_id, message)


bot.add_message_listener(help_message)


async def make_choice(room, message):
    match = botlib.MessageMatch(room, message, bot)
    if not (match.not_from_this_bot() and match.prefix(PREFIX)
            and match.command("play")):
        return

    args = match.args.split(' ')

    temp = True
    if "rock" in args:
        choice = "rock"
    elif "paper" in args:
        choice = "paper"
    elif "scissors" in args:
        choice = "scissors"
    else:
        temp = False

    victory_table = {"rock": "scissors", "scissors": "paper", "paper": "rock"}

    if temp:
        bot_choice = random.choice(["rock", "paper", "scissors"])

        await bot.api.send_text_message(room.room_id, f"You choose {choice}.")
        await bot.api.send_text_message(room.room_id,
                                        f"The bot chose {bot_choice}.")

        if choice == bot_choice:
            await bot.api.send_text_message(room.room_id, "You Tied!")
        if bot_choice == victory_table[choice]:
            await bot.api.send_text_message(room.room_id, "You Won!")
        if choice == victory_table[bot_choice]:
            await bot.api.send_text_message(room.room_id, "You Lost!")

    else:
        await bot.api.send_text_message(
            room.room_id,
            "Invalid choice. Please choose \"rock\", \"paper\", or \"scissors\"."
        )


bot.add_message_listener(make_choice)

bot.run()

Echo Bot within a Docker Container

FROM python:latest

RUN python -m pip install simplematrixbotlib

ADD echo.py echo.py

CMD [ "python", "echo.py" ]