diff --git a/CHANGELOG.md b/CHANGELOG.md index 461c55e..d5fee89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # xbotlib 0.13.1 (2021-01-19) - Document `Bot` attributes/functions ([#35](https://git.autonomic.zone/decentral1se/xbotlib/issues/35)) +- Provide a `Bot.respond` function ([#34](https://git.autonomic.zone/decentral1se/xbotlib/issues/34)) # xbotlib 0.13.0 (2021-01-18) diff --git a/README.md b/README.md index 9572ee6..81d880c 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,15 @@ Arguments: - **to**: the user to send the reply to - **room**: the room to send the reply to +> Bot.respond(response, content_type="text/html") + +Return a response via the web server. + +Arguments: + +- **response**: the text of the response +- **content_type**: the type of response + Other useful attributes on the `Bot` class are: - **self.db**: The [Redis database](#redis-key-value-storage) if you're using it @@ -318,12 +327,11 @@ Here's a small example that renders a random ASCII letter. ```python from string import ascii_letters -from xbotlib import Response def serve(self, request): letter = choice(ascii_letters) rendered = self.template.render(letter=letter) - return Response(body=rendered, content_type="text/html") + return self.respond(body=rendered) ``` If you want to pass data from your `direct`/`group` functions to the `serve` diff --git a/xbotlib.py b/xbotlib.py index bbf1818..1632b44 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -626,3 +626,7 @@ class Bot(ClientXMPP): return self.reply(cleandoc(self.help), **kwargs) except AttributeError: return self.reply("No help found 🤔️", **kwargs) + + def respond(self, response, content_type="text/html"): + """Send this response back with the web server.""" + return Response(response, content_type=content_type)