Support zform-based widget content in the server.

API users, particularly bots, can now send a field
called "widget_content" that will be turned into
a submessage for the web app to look at.  (Other
clients can still rely on "content" to be there,
although it's up to the bot author to make the
experience good for those clients as well.)

Right now widget_content will be a JSON string that
encodes a "zform" widget with "choices."  Our first
example will be a trivia bot, where users will see
something like this:

    Which fruit is orange in color?

        [A] orange
        [B] blackberry
        [C] strawberry

The letters will be turned into buttons on the webapp
and have canned replies.

This commit has a few parts:
    - receive widget_content in the request (simply
        validating that it's a string)
    - parse the JSON in check_message and deeply
        validate its structure
    - turn it into a submessage in widget.py
This commit is contained in:
Steve Howell
2018-05-21 13:23:46 +00:00
committed by Tim Abbott
parent 1b57e568ff
commit 69517f5ac5
5 changed files with 130 additions and 5 deletions

View File

@@ -46,6 +46,13 @@ def do_widget_post_save_actions(message: MutableMapping[str, Any]) -> None:
if content in ['/poll', '/tictactoe']:
widget_type = content[1:]
widget_content = message.get('widget_content')
if widget_content is not None:
# Note that we validate this data in check_message,
# so we can trust it here.
widget_type = widget_content['widget_type']
extra_data = widget_content['extra_data']
if widget_type:
content = dict(
widget_type=widget_type,