Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Message collector to object "wizard" #1794

Open
AmiterCodes opened this issue Jun 17, 2021 · 6 comments
Open

Suggestion: Message collector to object "wizard" #1794

AmiterCodes opened this issue Jun 17, 2021 · 6 comments

Comments

@AmiterCodes
Copy link

basically a feature that should probably in a utils file that abstracts dialog with a user and automatic command handling,
it would look like this:

User: "#startPoll"

Bot: "What drink is your favorite?
options are: Coca Cola, Water, Pepsi
please reply with your requested option"
User (replying): "Coca Cola"

Bot: "I see you like Coca Cola, Please pick your favorite artist from the options below
options: Eminem, Cardi, Kanye
default: 'Eminem', use , to skip"
User (replying): ","

Bot: "Please reply with your email"
User (replying): "something@something"

Bot: "that email is not valid
Please reply with your email"
User (replying): "something@something.com"

Bot: "I've seen enough, thanks for your cooperation"

at the end of the conversation it will return a map object that looks like this:
{
"favDrink": "Coca Cola",
"favArtist": "Eminem",
"Email": "something@something.com"
}

it will be used like the following:

enum Artist {
    Eminem,
    Cardi,
    Kanye
}

const dialogTemplate = {
    privateOnly: true, // if only allowed in private messaging
    identifier: "#startPoll", // can also accept regex
    finishedResponse: "I've seen enough, thank you for your cooperation"
    properties: {
        favDrink: {
            messageType: "text",
            prompt: "What drink is your favorite?",
            options: [ // string options
                "Coca Cola", "Water", "Pepsi"
            ],
            /**
                             * Or use a function to check if the reply is valid
                             * @param m Message
                             */
            check: (m: Message) => {
                return m.body === "yes"
            },
            /**
             * Or check via regex
             */
            regex: "/.*/g",
            /**
             * An error message to send if the message doesnt pass any of the above checks
             */
            errorMessage: "The answer must be valid",
            /**
             * Order, 0 = first
             */
            order: 0
        },
        favArtist: {
            messageType: "text",
            prompt: (lastInput, props) => ``I see you like ${lastInput}, Please pick your favorite artist from the options below`,// can be either a string or a callback that takes
            // 2 arguments, one of them is the invalid input and the other is that props that have been read so far and returns a string
            options: [ "Eminem", "Cardi", "Kanye"],
            optional: true, // if optional then  you can provide a default value, automatically required.
            default: "Eminem",
            errorMessage: (lastInput, props) => `${lastInput} isn't a valid artist, but your favorite drink is ${props.favDrink}`,
            onDone: (current, props) => console.log(`The user chose ${current} as their favorite artist just now`), // optional callback after a part of the wizard has been done
            process: (current) => Artist[current] //  processes a value after it has been read for it's final object shape
        },
        email: {
            messageType: "text",
            prompt: "Please reply with your email",
            regex: /^\S+@\S+\.\S+$/g,
            errorMessage: "that's an invalid email",
        }
    }
};

// default values for config here, but you can pass in a config object to register
const dialogConfig = {
    optionsText: "options: ", //can be used to prefix 
    defaultText: "default: ", // 
    textPrefix: "" // can be used to prefix all bot messages
}

const dialog = dialogUtil.register(dialogTemplate, dialogConfig);

dialog.subscribe(resposne => {
    console.log(response.sender) // contact of sender
    console.log(resposne.props.favArtist) // props is the object with all the response property values
    console.log(Response.props.favArtist === Artist.Kanye)
})
@smashah
Copy link
Member

smashah commented Jul 15, 2021

@AmiterCodes this would be greatly enhanced by the latest version's ability to send buttons.

#98 (comment)

@smashah
Copy link
Member

smashah commented Nov 1, 2021

I wonder if there's a simple way we can implement the dialogtemplate to create decision trees, implement buttons/list message and grab media while we're at it. I checked out survey.js but it's designed to be more visual and there's a lot of limitations here that don't exist there.

So far I can see the following types that can be implemented:

  • boolean: 2 button message
  • single select: ListMessage
  • Text
  • Media
  • Contact?
  • Location
  • Multi select is possible with list message but it's blocked by WA currently (might be possible with MD)

@smashah
Copy link
Member

smashah commented Nov 1, 2021

r.e validation:

There can be multiple layers of validation, and even multiple regex validations so maybe best to make that an array of validators

@andersondeoliveiramachado
Copy link
Contributor

andersondeoliveiramachado commented Nov 2, 2021

r.e validation:

There can be multiple layers of validation, and even multiple regex validations so maybe best to make that an array of validators

https://github.com/TomFrost/jexl

https://www.npmjs.com/package/object-quiz
https://www.npmjs.com/package/quiz-text

@smashah
Copy link
Member

smashah commented Jul 5, 2022

Some progress on this:

#2779

@smashah
Copy link
Member

smashah commented Jul 5, 2022

I think to get started on this (albeit a year late) it's best to limit the scope to whatever can be responded to via buttons (i.e not taking in media, location, contacts, etc.) as that would require maintaining state of the quiz between restarts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants