Provide a respond function

Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/34.
This commit is contained in:
Luke Murphy 2021-01-19 08:29:56 +01:00
parent 73df47eb45
commit e7adfeaa51
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 15 additions and 2 deletions

View File

@ -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)

View File

@ -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`

View File

@ -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)