2021-07-01 11:49:45 +02:00
|
|
|
# Widgets
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
## What is a widget?
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
Widgets are special kinds of messages. These include:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
- polls
|
|
|
|
- TODO lists
|
|
|
|
- `/me` messages
|
|
|
|
- Trivia bot
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
Some widgets are used with a leading `/` (like `/poll Tea or coffee?`), similar
|
|
|
|
to [slash commands](/subsystems/slash-commands.md), but these two concepts
|
|
|
|
are very different. Slash commands have nothing to do with message sending.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
The trivia_quiz_bot does not use `/`'s. Instead, it sends "extra_data"
|
|
|
|
in messages to invoke **zforms** (which enable button-based UIs in the
|
|
|
|
messages).
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
## `/me` messages
|
|
|
|
|
|
|
|
These are the least complex. We use our markdown processors to
|
|
|
|
detect if a message is a `/me` message, plumb the flag through
|
|
|
|
the message object (as `is_me_message`) and have the clients
|
|
|
|
format it correctly. Related code (for the web app) lies in
|
|
|
|
`message_list_view.js` in `_maybe_format_me_message`.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
## Polls, todo lists, and games
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
The most interactive widgets that we built during
|
|
|
|
2018 are for polls, todo lists, and games. You
|
|
|
|
launch widgets by sending one of the following messages:
|
|
|
|
|
|
|
|
- /poll
|
|
|
|
- /todo
|
|
|
|
|
2021-05-14 00:16:30 +02:00
|
|
|
The web app client provides the "widget experience" by
|
2019-01-29 16:57:45 +01:00
|
|
|
default. Other clients just show raw messages like
|
2020-10-31 17:23:08 +01:00
|
|
|
"/poll", and should be adding support
|
2019-01-29 16:57:45 +01:00
|
|
|
for widgets soon.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
Our customers have long requested a poll/survey widget.
|
|
|
|
See [this issue](https://github.com/zulip/zulip/issues/9736).
|
|
|
|
There are workaround ways to do polls using things like
|
|
|
|
emoji reactions, but our poll widget provides a more
|
|
|
|
interactive experience.
|
|
|
|
|
|
|
|
### Data flow
|
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
Some important code entities for the widget implementation are:
|
|
|
|
|
|
|
|
- `SubMessage` database table
|
|
|
|
- `/json/submessage` API endpoint
|
2023-02-22 23:03:47 +01:00
|
|
|
- `web/src/submessage.js`
|
|
|
|
- `web/src/poll_widget.js`
|
2024-02-16 21:28:44 +01:00
|
|
|
- `web/src/widgetize.ts`
|
2023-02-22 23:03:47 +01:00
|
|
|
- `web/src/zform.js`
|
|
|
|
- `web/templates/widgets/`
|
2021-07-01 11:49:45 +02:00
|
|
|
- `zerver/lib/widget.py`
|
|
|
|
- `zerver/views/submessage.py`
|
|
|
|
|
|
|
|
Both **poll** and **todo** widgets use the "submessage" architecture.
|
|
|
|
We'll use the poll widget as an example.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
The `SubMessage` table, as the name indicates, allows
|
|
|
|
you to associate multiple submessages to any given
|
2021-08-20 21:53:28 +02:00
|
|
|
`Message` row. When a message gets sent, there's a
|
2021-07-01 11:49:45 +02:00
|
|
|
hook inside of `widget.py` that will detect widgets
|
2021-08-20 21:53:28 +02:00
|
|
|
like "/poll". If a message needs to be
|
2018-08-09 01:08:59 +02:00
|
|
|
widgetized, an initial `SubMessage` row will be
|
|
|
|
created with an appropriate `msg_type` (and persisted
|
2021-08-20 21:53:28 +02:00
|
|
|
to the database). This data will also be included
|
|
|
|
in the normal Zulip message event payload. Clients
|
2018-08-09 01:08:59 +02:00
|
|
|
can choose to ignore the submessage-related data, in
|
|
|
|
which case they'll gracefully degrade to seeing "/poll".
|
2021-05-14 00:16:30 +02:00
|
|
|
Of course, the web app client actually recognizes the
|
2018-08-09 01:08:59 +02:00
|
|
|
appropriate widgets.
|
|
|
|
|
2021-05-14 00:16:30 +02:00
|
|
|
The web app client will next collect poll options and votes
|
2021-08-20 21:53:28 +02:00
|
|
|
from users. The web app client has
|
2018-08-09 01:08:59 +02:00
|
|
|
code in `submessage.js` that dispatches events
|
2024-02-16 21:28:44 +01:00
|
|
|
to `widgetize.ts`, which in turn sends events to
|
2021-08-20 21:53:28 +02:00
|
|
|
individual widgets. The widgets know how to render
|
2018-08-09 01:08:59 +02:00
|
|
|
themselves and set up click/input handlers to collect
|
2021-08-20 21:53:28 +02:00
|
|
|
data. They can then post back to `/json/submessage`
|
2018-08-09 01:08:59 +02:00
|
|
|
to attach more data to the message (and the
|
2021-08-20 21:53:28 +02:00
|
|
|
details are encapsulated with a callback). The server
|
2018-08-09 01:08:59 +02:00
|
|
|
will continue to persist `SubMessage` rows in the
|
2021-08-20 21:53:28 +02:00
|
|
|
database. These rows are encoded as JSON, and the
|
2018-08-09 01:08:59 +02:00
|
|
|
schema of the messages is driven by the individual widgets.
|
|
|
|
Most of the logic is in the client; things are fairly opaque
|
|
|
|
to the server at this point.
|
|
|
|
|
|
|
|
If a client joins Zulip after a message has accumulated
|
|
|
|
several submessage events, it will see all of those
|
2021-08-20 21:53:28 +02:00
|
|
|
events the first time it sees the parent message. Clients
|
2018-08-09 01:08:59 +02:00
|
|
|
need to know how to build/rebuild their state as each
|
2021-08-20 21:53:28 +02:00
|
|
|
submessage comes in. They also need to tolerate
|
2018-08-09 01:08:59 +02:00
|
|
|
misformatted data, ideally just dropping data on the floor.
|
|
|
|
If a widget throws an exception, it's caught before the
|
|
|
|
rest of the message feed is affected.
|
|
|
|
|
|
|
|
As far as rendering is concerned, each widget module
|
|
|
|
is given a parent `elem` when its `activate` function
|
2021-08-20 21:53:28 +02:00
|
|
|
is called. This is just a `<div>` inside of the parent
|
|
|
|
message in the message pane. The widget has access to
|
2018-08-09 01:08:59 +02:00
|
|
|
jQuery and template.render, and the developer can create
|
2023-02-22 23:03:47 +01:00
|
|
|
new templates in `web/templates/widgets/`.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
A good way to learn the system is to read the code
|
2023-02-22 23:03:47 +01:00
|
|
|
in `web/src/poll_widget.js`. It is worth noting that
|
2018-08-09 01:08:59 +02:00
|
|
|
writing a new widget requires only minor backend
|
2021-08-20 21:53:28 +02:00
|
|
|
changes in the current architecture. This could change
|
2021-07-01 11:49:45 +02:00
|
|
|
in the future, but for now, a frontend developer mostly
|
2018-08-09 01:08:59 +02:00
|
|
|
needs to know JS, CSS, and HTML.
|
|
|
|
|
|
|
|
It may be useful to think of widgets in terms of a
|
2021-08-20 21:53:28 +02:00
|
|
|
bunch of clients exchanging peer-to-peer messages. The
|
2018-08-09 01:08:59 +02:00
|
|
|
server's only real role is to decide who gets delivered
|
2021-08-20 21:53:28 +02:00
|
|
|
which submessages. It's a lot like a "subchat" system.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
### Backward compatibility
|
|
|
|
|
|
|
|
Our "submessage" widgets are still evolving, and we want
|
|
|
|
to have a plan for allowing future progress without
|
|
|
|
breaking old messages.
|
|
|
|
|
|
|
|
Widget developers can revise code to improve a
|
|
|
|
widget's visual polish without too much concern
|
2021-08-20 21:53:28 +02:00
|
|
|
for breaking how old messages get widgetized. They will need to
|
2018-08-09 01:08:59 +02:00
|
|
|
be more cautious if they change the actual data
|
|
|
|
structures passed around in the submessage payloads.
|
|
|
|
|
|
|
|
For significant schema changes, it would be worthwhile to add
|
|
|
|
some kind of versioning scheme inside of `SubMessages`, either
|
|
|
|
at the DB level or more at the JSON level within fields.
|
2021-08-20 21:53:28 +02:00
|
|
|
This has yet to be designed. One thing to consider is that
|
2018-08-09 01:08:59 +02:00
|
|
|
most widgets are somewhat ephemeral in nature, so it's not
|
|
|
|
the end of the world if upgrades cause some older messages
|
|
|
|
to be obsolete, as long as the code degrades gracefully.
|
|
|
|
|
2021-07-01 11:49:45 +02:00
|
|
|
Mission-critical widgets should have a deprecation strategy.
|
2018-08-09 01:08:59 +02:00
|
|
|
For example, you could add optional features for one version
|
|
|
|
bump and then only make them mandatory for the next version,
|
2021-08-20 21:53:28 +02:00
|
|
|
as long as you don't radically change the data model. And
|
2018-08-09 01:08:59 +02:00
|
|
|
if you're truly making radical changes, you can always
|
|
|
|
write a Django migration for the `SubMessage` data.
|
|
|
|
|
|
|
|
### Adding widgets
|
|
|
|
|
|
|
|
Right now we don't have a plugin model for the above widgets;
|
|
|
|
they are served up by the core Zulip server implementation.
|
|
|
|
Of course, anybody who wishes to build their own widget
|
|
|
|
has the option of forking the server code and self-hosting,
|
|
|
|
but we want to encourage folks to submit widget
|
2021-08-20 21:53:28 +02:00
|
|
|
code to our codebase in PRs. If we get to a critical mass
|
2018-08-09 01:08:59 +02:00
|
|
|
of contributed widgets, we will want to explore a more
|
|
|
|
dynamic mechanism for "plugging in" code from outside sources,
|
|
|
|
but that is not in our immediate roadmap.
|
|
|
|
|
|
|
|
This is sort of a segue to the next section of this document.
|
|
|
|
Suppose you want to write your own custom bot, and you
|
|
|
|
want to allow users to click buttons to respond to options,
|
|
|
|
but you don't want to have to modify the Zulip server codebase
|
2021-08-20 21:53:28 +02:00
|
|
|
to turn on those features. This is where our "zform"
|
2018-08-09 01:08:59 +02:00
|
|
|
architecture comes to the rescue.
|
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
## zform (trivia quiz bot)
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
This section will describe our "zform" architecture.
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
For context, imagine a naive trivia bot. The trivia bot
|
2018-08-09 01:08:59 +02:00
|
|
|
sends a question with the answers labeled as A, B, C,
|
2021-08-20 21:53:28 +02:00
|
|
|
and D. Folks who want to answer the bot send back an
|
2018-08-09 01:08:59 +02:00
|
|
|
answer have to send an actual Zulip message with something
|
|
|
|
like `@trivia_bot answer A to Q01`, which is kind of
|
2021-08-20 21:53:28 +02:00
|
|
|
tedious to type. Wouldn't it be nice if the bot could
|
2018-08-09 01:08:59 +02:00
|
|
|
serve up some kind of buttons with canned replies, so
|
|
|
|
that the user just hits a button?
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
That is where zforms come in. Zulip's trivia bot sends
|
2018-08-09 01:08:59 +02:00
|
|
|
the Zulip server a JSON representation of a form it
|
|
|
|
wants rendered, and then the client renders a generic
|
|
|
|
"**zform**" with buttons corresponding to `short_name` fields
|
|
|
|
inside a `choices` list inside of the JSON payload.
|
|
|
|
|
|
|
|
Here is what an example payload looks like:
|
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```json
|
2018-08-09 01:08:59 +02:00
|
|
|
{
|
|
|
|
"extra_data": {
|
|
|
|
"type": "choices",
|
|
|
|
"heading": "05: What color is a blueberry?",
|
|
|
|
"choices": [
|
|
|
|
{
|
|
|
|
"type": "multiple_choice",
|
|
|
|
"reply": "answer 05 A",
|
|
|
|
"long_name": "red",
|
|
|
|
"short_name": "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "multiple_choice",
|
|
|
|
"reply": "answer 05 B",
|
|
|
|
"long_name": "blue",
|
|
|
|
"short_name": "B"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "multiple_choice",
|
|
|
|
"reply": "answer 05 C",
|
|
|
|
"long_name": "yellow",
|
|
|
|
"short_name": "C"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "multiple_choice",
|
|
|
|
"reply": "answer 05 D",
|
|
|
|
"long_name": "orange",
|
|
|
|
"short_name": "D"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"widget_type": "zform"
|
|
|
|
}
|
2021-08-20 22:54:08 +02:00
|
|
|
```
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
When users click on the buttons, **generic** click
|
|
|
|
handlers automatically simulate a client reply using
|
|
|
|
a field called `reply` (in `choices`) as the content
|
2021-08-20 21:53:28 +02:00
|
|
|
of the message reply. Then the bot sees the reply
|
2018-08-09 01:08:59 +02:00
|
|
|
and grades the answer using ordinary chat-bot coding.
|
|
|
|
|
2020-03-17 13:57:10 +01:00
|
|
|
The beautiful thing is that any third party developer
|
2018-08-09 01:08:59 +02:00
|
|
|
can enhance bots that are similar to the **trivia_quiz**
|
|
|
|
bot without touching any Zulip code, because **zforms**
|
2019-01-29 16:57:45 +01:00
|
|
|
are completely generic.
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
## Data flow
|
|
|
|
|
|
|
|
We can walk through the steps from the bot generating
|
|
|
|
the **zform** to the client rendering it.
|
|
|
|
|
|
|
|
First,
|
2021-08-31 23:32:37 +02:00
|
|
|
[here](https://github.com/zulip/python-zulip-api/blob/main/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py)
|
2018-08-09 01:08:59 +02:00
|
|
|
is the code that produces the JSON.
|
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```py
|
2018-08-09 01:08:59 +02:00
|
|
|
def format_quiz_for_widget(quiz_id: str, quiz: Dict[str, Any]) -> str:
|
|
|
|
widget_type = 'zform'
|
|
|
|
question = quiz['question']
|
|
|
|
answers = quiz['answers']
|
|
|
|
|
|
|
|
heading = quiz_id + ': ' + question
|
|
|
|
|
|
|
|
def get_choice(letter: str) -> Dict[str, str]:
|
|
|
|
answer = answers[letter]
|
|
|
|
reply = 'answer ' + quiz_id + ' ' + letter
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
type='multiple_choice',
|
|
|
|
short_name=letter,
|
|
|
|
long_name=answer,
|
|
|
|
reply=reply,
|
|
|
|
)
|
|
|
|
|
|
|
|
choices = [get_choice(letter) for letter in 'ABCD']
|
|
|
|
|
|
|
|
extra_data = dict(
|
|
|
|
type='choices',
|
|
|
|
heading=heading,
|
|
|
|
choices=choices,
|
|
|
|
)
|
|
|
|
|
|
|
|
widget_content = dict(
|
|
|
|
widget_type=widget_type,
|
|
|
|
extra_data=extra_data,
|
|
|
|
)
|
|
|
|
payload = json.dumps(widget_content)
|
|
|
|
return payload
|
|
|
|
```
|
|
|
|
|
|
|
|
The above code processes data that is specific to a trivia
|
|
|
|
quiz, but it follows a generic schema.
|
|
|
|
|
|
|
|
The bot sends the JSON payload to the server using the
|
|
|
|
`send_reply` callback.
|
|
|
|
|
|
|
|
The bot framework looks for the optional `widget_content`
|
|
|
|
parameter in `send_reply` and includes that in the
|
|
|
|
message payload it sends to the server.
|
|
|
|
|
|
|
|
The server validates the schema of `widget_content` using
|
|
|
|
`check_widget_content`.
|
|
|
|
|
|
|
|
Then code inside of `zerver/lib/widget.py` builds a single
|
|
|
|
`SubMessage` row to contain the **zform** payload, and the
|
|
|
|
server also sends this payload to all clients who are
|
|
|
|
recipients of the parent message.
|
|
|
|
|
|
|
|
When the message gets to the client, the codepath for **zform**
|
2020-10-31 17:23:08 +01:00
|
|
|
is actually quite similar to what happens with a more
|
2021-08-20 21:53:28 +02:00
|
|
|
customized widget like **poll**. (In fact, **zform** is a
|
2020-10-31 17:23:08 +01:00
|
|
|
sibling of **poll** and **zform** just has a somewhat more
|
2024-02-16 21:28:44 +01:00
|
|
|
generic job to do.) In `web/src/widgetize.ts` you will see
|
2020-10-31 17:23:08 +01:00
|
|
|
where this code converges, with snippets like this:
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```js
|
2018-12-17 04:46:27 +01:00
|
|
|
widgets.poll = poll_widget;
|
2018-08-09 01:08:59 +02:00
|
|
|
widgets.todo = todo_widget;
|
|
|
|
widgets.zform = zform;
|
2021-08-20 22:54:08 +02:00
|
|
|
```
|
2018-08-09 01:08:59 +02:00
|
|
|
|
2023-02-22 23:03:47 +01:00
|
|
|
The code in `web/src/zform.js` renders the form (not
|
2018-08-09 01:08:59 +02:00
|
|
|
shown here) and then sets up a click handler like below:
|
|
|
|
|
2021-08-20 22:54:08 +02:00
|
|
|
```js
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.find('button').on('click', function (e) {
|
2018-08-09 01:08:59 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Grab our index from the markup.
|
|
|
|
var idx = $(e.target).attr('data-idx');
|
|
|
|
|
|
|
|
// Use the index from the markup to dereference our
|
|
|
|
// data structure.
|
|
|
|
var reply_content = data.choices[idx].reply;
|
|
|
|
|
|
|
|
transmit.reply_message({
|
|
|
|
message: opts.message,
|
|
|
|
content: reply_content,
|
|
|
|
});
|
|
|
|
});
|
2021-08-20 22:54:08 +02:00
|
|
|
```
|
2018-08-09 01:08:59 +02:00
|
|
|
|
|
|
|
And then we are basically done!
|