diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a65818..a873d14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # xbotlib x.x.x (UNRELEASED) +# xbotlib x.x.x (UNRELEASED) + +- Add `setup` function hook ([#36](https://git.autonomic.zone/decentral1se/xbotlib/issues/36)) + # xbotlib 0.15.1 (2021-01-24) - Save to file based storage on all writes ([#39](https://git.autonomic.zone/decentral1se/xbotlib/issues/39)) diff --git a/README.md b/README.md index 0cf1044..e73db41 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ you want to chat or just invite your bots for testing. - [Bot.direct(message)](#bot-direct-message) - [Bot.group(message)](#bot-group-message) - [Bot.serve(request)](#bot-serve-request) + - [Bot.setup()](#bot-setup) - [SimpleMessage](#simplemessage) - [Bot](#bot) - [Working with your bot](#working-with-your-bot) @@ -114,6 +115,10 @@ Arguments: - **request**: the web request +### Bot.setup() + +Run some setup logic before starting your bot. + ### SimpleMessage A simple message interface. diff --git a/xbotlib.py b/xbotlib.py index e8d750f..a51aaf4 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -673,6 +673,15 @@ class Bot(ClientXMPP): if self.serve: self.log.info("Turning on the web server") self.serve_web() + + if hasattr(self, "setup"): + try: + self.setup() + self.log.info("Finished running Bot.setup") + except Exception as exception: + message = f"Bot.setup failed: {exception}" + self.log.info(message) + self.process(forever=False) except (KeyboardInterrupt, RuntimeError): pass