Forms
A form is a sheet of fields a person fills in and sends once: a poll’s question and answers, a giveaway’s prize, the reason for a report. Your wisp asks with one in answer to a run or a press, the person’s app opens it at once, and what they send reaches the code for the form’s action.
using Woosh.Wisp;
public sealed class Polls(Ballots ballots){ [Command("poll", "Ask the channel something")] public Task New(CommandRun run) => run.AskAsync(NewPoll());
[Form("new-poll")] public async Task Post(Press sent) { var answers = sent.Values.Text("answers")! .Split('\n') .Select(a => a.Trim()) .Where(a => a.Length > 0) .ToArray(); var different = answers.Distinct().Count() == answers.Length; if (answers.Length is < 2 or > 10 || !different) { var error = "2 to 10 answers, each a different one."; await sent.UpdateAsync(NewPoll().WithError("answers", error)); return; } var question = sent.Values.Text("question")!; var many = sent.Values.Choice("pick") == "many"; var ends = sent.Values.Time("ends"); var poll = ballots.Open(question, answers, many, ends); // A line in the channel: the sheet closes. await sent.AnswerAsync($"{sent.User} asks: {question}", poll); }
private static Form NewPoll() { var now = DateTimeOffset.UtcNow; return new Form("new-poll", "New poll") .Text("Everyone sees the answers as they come in.") .Text("question", "Question", required: true, max: 300) .Text("answers", "Answers", "One per line, 2 to 10", required: true, lines: 6, max: 900) .Choice("pick", "People pick", [("one", "One answer"), ("many", "As many as they like")], prefill: "one") .Time("ends", "Ends", required: true, prefill: now.AddDays(1), min: now, max: now.AddDays(7)) .Submit("Post poll"); }}import { form, type Form, type Wisp } from "@woosh-no/wisp";
const day = 24 * 60 * 60 * 1000;
function newPoll(): Form { const now = Date.now(); return form({ action: "new-poll", title: "New poll", text: "Everyone sees the answers as they come in.", fields: [ { kind: "text", name: "question", label: "Question", required: true, max: 300 }, { kind: "text", name: "answers", label: "Answers", description: "One per line, 2 to 10", required: true, lines: 6, max: 900 }, { kind: "choice", name: "pick", label: "People pick", prefill: "one", choices: [{ value: "one", label: "One answer" }, { value: "many", label: "As many as they like" }] }, { kind: "time", name: "ends", label: "Ends", required: true, prefill: new Date(now + day), min: new Date(now), max: new Date(now + 7 * day) }, ], submit: "Post poll", });}
export function polls(wisp: Wisp, ballots: Ballots) { wisp.command("poll", "Ask the channel something", (run) => run.ask(newPoll()));
wisp.onForm("new-poll", async (sent) => { const answers = sent.values.text("answers")! .split("\n") .map((answer) => answer.trim()) .filter((answer) => answer.length > 0); const different = new Set(answers).size === answers.length; if (answers.length < 2 || answers.length > 10 || !different) { const error = "2 to 10 answers, each a different one."; await sent.update({ ...newPoll(), errors: { answers: error } }); return; } const question = sent.values.text("question")!; const many = sent.values.choice("pick") === "many"; const ends = sent.values.time("ends"); const poll = ballots.open(question, answers, many, ends); // A line in the channel: the sheet closes. await sent.answer(`${sent.user} asks: ${question}`, poll); });}Fields
Section titled “Fields”Fields show in the order they are given, up to 10:
| Field | Holds | Its answer |
|---|---|---|
Text |
lines, min, max (characters) |
Values.Text |
Number |
min, max |
Values.Number |
Integer |
min, max, whole numbers only |
Values.Integer |
Member, Channel, Role |
Values.Member, .Channel, .Role |
|
Choice |
one of up to 25 | Values.Choice |
Choices |
several of up to 25, as tick boxes | Values.Choices |
Toggle |
on or off; required means it must be on (“I read the rules”) |
Values.Toggle, always there |
Time |
min, max: a day and a time, picked to the minute |
Values.Time |
Each takes a description, required, and a prefill filled in as the sheet opens, so settings
show as they stand. A field left empty is null, or an empty list for Choices.
kind |
Holds | Its answer |
|---|---|---|
text |
lines, min, max (characters) |
values.text |
number |
min, max, and whole: true for whole numbers only |
values.number |
member, channel, role |
values.member, .channel, .role |
|
choice |
choices, up to 25; with pick above 1, several as tick boxes |
values.choice, or values.choices |
toggle |
on or off; required means it must be on (“I read the rules”) |
values.toggle, always there |
time |
min, max as Dates: a day and a time, picked to the minute |
values.time, a Date |
Each has a name, a label, and may take a description, required, and a prefill filled in as
the sheet opens, so settings show as they stand. A field left empty reads as undefined, or an
empty list for values.choices.
Checking what was sent
Section titled “Checking what was sent”Woosh checks every answer against its field before your wisp hears it: required ones are there, each is within its limits, a member is in the server. What is left to you is your own rules.
Answer with the same form and WithError under a field, or WithNotice for the whole form by its
send button, and the sheet stays open with everything the person typed.
| Answer | Shows |
|---|---|
sent.UpdateAsync(form) |
the sheet stays open and moves on: errors or a notice from your rules, or a next step |
sent.UpdateAsync(text), (card), (text, card) |
the sheet closes, and its row in the channel becomes that |
sent.AnswerAsync(...), sent.AnswerPrivatelyAsync(...) |
a line or a private answer, as for any press; the sheet closes |
Answer with the same form and errors under its fields, or a notice for the whole form by its
send button ({ ...newPoll(), notice: "Polls are off here." }), and the sheet stays open with
everything the person typed.
| Answer | Shows |
|---|---|
sent.update(form) |
the sheet stays open and moves on: errors or a notice from your rules, or a next step |
sent.update(text), (card), (text, card) |
the sheet closes, and its row in the channel becomes that |
sent.answer(...), sent.answerPrivately(...) |
a line or a private answer, as for any press; the sheet closes |
A form is sent once. Moved on with a new form, it can be sent again; answered any other way, it is done and a second sending is refused, so a poll is never posted twice. If your wisp does not answer within 15 seconds, the sheet stays open with everything typed and says so, and the person can send again.
What else to know
Section titled “What else to know”- A form is always private: only the person who asked sees it. Their other apps show a row to open it again if they close the sheet by mistake.
- A form never has hidden fields or password fields, and Woosh’s own line at its foot tells the person never to share a password.
- A form’s value carries up to 100 bytes of your own, which come back with the sending as its value
(
form.Value(...)andsent.Value; in TypeScript,valueandsent.value). Woosh keeps it beside the form, and it never reaches the person. - A form and its answers are kept nowhere. Keep what you need.
- A form that breaks a rule, such as a
minabove itsmax, throws where you built it: anArgumentExceptionin C#, aWispRuleErrorin TypeScript.form.Fits()(in TypeScript,fits(form)) says whether one built from your own data is within 4,000 characters and 8 KB.
A form’s sending reaches the code for its own action, and a button the code for its own, so a button and a form may share a name and each goes to its own:
The [Form] method for its action, and the [Press] method for the button’s. Without attributes:
wisp.OnForm("new-poll", sent => ...).
wisp.onForm("new-poll", ...) for the form, and wisp.onPress("new-poll", ...) for the button.
Writing your own library? Every rule of a form, as cases to test against, is in Forms, raw.