From ac69c50b807d6bde1987afa78903add7f811c39a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 7 Jan 2019 08:14:29 +0200 Subject: [PATCH] Update hello world example and add config example --- example-plugin/build.sh | 2 -- {example-plugin => examples}/LICENSE | 0 examples/README.md | 6 +++++ examples/config/base-config.yaml | 2 ++ examples/config/configurablebot.py | 25 +++++++++++++++++++ examples/config/maubot.yaml | 12 +++++++++ .../helloworld}/helloworld.py | 10 +++----- .../helloworld}/maubot.yaml | 2 +- 8 files changed, 49 insertions(+), 10 deletions(-) delete mode 100644 example-plugin/build.sh rename {example-plugin => examples}/LICENSE (100%) create mode 100644 examples/README.md create mode 100644 examples/config/base-config.yaml create mode 100644 examples/config/configurablebot.py create mode 100644 examples/config/maubot.yaml rename {example-plugin => examples/helloworld}/helloworld.py (53%) rename {example-plugin => examples/helloworld}/maubot.yaml (98%) diff --git a/example-plugin/build.sh b/example-plugin/build.sh deleted file mode 100644 index 1e83037..0000000 --- a/example-plugin/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -zip -9r helloworld.mbp maubot.yaml helloworld.py diff --git a/example-plugin/LICENSE b/examples/LICENSE similarity index 100% rename from example-plugin/LICENSE rename to examples/LICENSE diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..1837fec --- /dev/null +++ b/examples/README.md @@ -0,0 +1,6 @@ +# Maubot examples +All examples are published under the [MIT license](LICENSE). + +* [Hello World](helloworld/) - Very basic event handling bot that responds "Hello, World!" to all messages. +* [Echo bot](https://github.com/maubot/echo) - Basic command handling bot with !echo and !ping commands +* [Config example](config/) - Simple example of using a config file diff --git a/examples/config/base-config.yaml b/examples/config/base-config.yaml new file mode 100644 index 0000000..c621847 --- /dev/null +++ b/examples/config/base-config.yaml @@ -0,0 +1,2 @@ +# Message to send when user sends !getmessage +message: Default configuration active diff --git a/examples/config/configurablebot.py b/examples/config/configurablebot.py new file mode 100644 index 0000000..13624be --- /dev/null +++ b/examples/config/configurablebot.py @@ -0,0 +1,25 @@ +from typing import Type + +from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper +from maubot import Plugin, MessageEvent +from maubot.handlers import command + + +class Config(BaseProxyConfig): + def do_update(self, helper: ConfigUpdateHelper) -> None: + helper.copy("message") + + +class DatabaseBot(Plugin): + async def start(self) -> None: + await super().start() + self.config.load_and_update() + + @classmethod + def get_config_class(cls) -> Type[BaseProxyConfig]: + return Config + + @command.new("getmessage") + async def handler(self, event: MessageEvent) -> None: + if event.sender != self.client.mxid: + await event.reply(self.config["message"]) diff --git a/examples/config/maubot.yaml b/examples/config/maubot.yaml new file mode 100644 index 0000000..b049dba --- /dev/null +++ b/examples/config/maubot.yaml @@ -0,0 +1,12 @@ +maubot: 0.1.0 +id: xyz.maubot.databasebot +version: 1.0.0 +license: MIT +modules: +- configurablebot +main_class: ConfigurableBot +database: false + +# Instruct the build tool to include the base config. +extra_files: +- base-config.yaml diff --git a/example-plugin/helloworld.py b/examples/helloworld/helloworld.py similarity index 53% rename from example-plugin/helloworld.py rename to examples/helloworld/helloworld.py index f91e168..90569e8 100644 --- a/example-plugin/helloworld.py +++ b/examples/helloworld/helloworld.py @@ -1,14 +1,10 @@ -from maubot import Plugin, MessageEvent from mautrix.types import EventType +from maubot import Plugin, MessageEvent +from maubot.handlers import event class HelloWorldBot(Plugin): - async def start(self) -> None: - self.client.add_event_handler(self.handler, EventType.ROOM_MESSAGE) - - async def stop(self) -> None: - self.client.remove_event_handler(self.handler, EventType.ROOM_MESSAGE) - + @event.on(EventType.ROOM_MESSAGE) async def handler(self, event: MessageEvent) -> None: if event.sender != self.client.mxid: await event.reply("Hello, World!") diff --git a/example-plugin/maubot.yaml b/examples/helloworld/maubot.yaml similarity index 98% rename from example-plugin/maubot.yaml rename to examples/helloworld/maubot.yaml index 0665091..a4082bd 100644 --- a/example-plugin/maubot.yaml +++ b/examples/helloworld/maubot.yaml @@ -5,7 +5,7 @@ maubot: 0.1.0 # The unique ID for the plugin. Java package naming style. (i.e. use your own domain, not xyz.maubot) -id: xyz.maubot.example +id: xyz.maubot.helloworld # A PEP 440 compliant version string. version: 1.0.0