Coder Social home page Coder Social logo

Comments (18)

Charca avatar Charca commented on May 13, 2024 3

I like this idea, I can see the use for something like this, and there's definitely room to implement it, we could use the data param that is currently not implemented for postback events.

I'm thinking we could establish a pattern for arguments (I like the : as a separator) and send them back to the callback in the data object:

chat.say({
    text: 'Some product here',
    buttons: [
        { type: 'postback', title: 'More info', payload: 'INFO:product_id:user_id:promo_id' }
    ]
});

bot.on('postback:INFO', (payload, chat, data) => {
    console.log(data.args); => [ 'product_id', 'user_id', 'promo_id' ]
});

We could even have named arguments so data.args become an object instead of an array, but I'm not sure what's the limit of characters in the postback string and I don't wanna push it.

In the case of a callback listening for a particular postback + argument combination, we will execute both. So for example if you have a button with postback INFO:123456 and these two listeners:

bot.on('postback:INFO', () => {} );
bot.on('postback:INFO:123456', () => {} );

Both will be called, and the data.captured flag will be set to true.

Thoughts?

from bootbot.

Charca avatar Charca commented on May 13, 2024 3

@caiolopes absolutely, go ahead!

from bootbot.

dlocation avatar dlocation commented on May 13, 2024 3

Hi guys. I think that also worth implement support of regexp parameters of postback events as at bot.hear().
I look forward to a solution of this issue.
P.S Charca, thank you for awesome and lightweight lib. You rock :)

from bootbot.

caiolopes avatar caiolopes commented on May 13, 2024 2

@Charca Yes, exactly! Can I try to implement this and make a PR?

from bootbot.

Charca avatar Charca commented on May 13, 2024 2

I think this would make for a pretty cool feature. Grabbing data passed in the postback's name is a pretty common use case, and providing an easy way to access that data on the callback function would be really useful.

I'll leave this open, if anyone wants to take a crack at it, please go ahead.

from bootbot.

kebou avatar kebou commented on May 13, 2024 1

Hey @caiolopes, my workaround is the following:

  1. stringify the object that I want to pass as payload
  2. subscribe to all postback messages, parse the payload string as JSON and decide the type of the postback

Here is the code with your example:

chat.say({
    text: 'Some product here',
    buttons: [{
        type: 'postback',
        title: 'More info',
        payload: JSON.stringify({
            type: 'INFO',
            product_id: 'PRODUCT_ID'
        })
    }]
});

bot.on('postback', (payload, chat, data) => {
    const pb_payload = JSON.parse(payload.postback.payload);

    if (pb_payload.type == 'INFO') {
        console.log(pb_payload.product_id);
    }
});

from bootbot.

Charca avatar Charca commented on May 13, 2024 1

@caiolopes I agree. So just to make sure we're on the same page, we're talking about something on the lines of:

chat.say({
    text: 'Some product here',
    buttons: [{
        type: 'postback',
        title: 'More info',
        payload: {
            name: 'INFO',
            arguments: [ 'PRODUCT_ID' ]
        }
    }]
});

bot.on('postback:INFO', (payload, chat, data) => {
    console.log(data.arguments); // => [ 'PRODUCT_ID' ]
});

Correct?

from bootbot.

caiolopes avatar caiolopes commented on May 13, 2024

@kebou nice solution! I think this could be implemented in a native way in the bootbot!

I am just wondering if the list of if statements can get really long, don't you think?

from bootbot.

kebou avatar kebou commented on May 13, 2024

yep, you're right, it would be great to have a similar built in feature, however the code won't be shorter, because subscribing for every specific event or checking the type of the payload with if statements requires nearly the same length of code, but it would be without doubt more consistent.

from bootbot.

caiolopes avatar caiolopes commented on May 13, 2024

@Charca I liked your suggestion but I think it is easier and transparent for someone using the project pass an object instead of having to follow some unusual pattern.

I don't think the limit of characters is gonna be a problem since it is not meant to pass too much data. For example, in my case, I used only for passing ids so I could search in the database for information, etc.

In that case, I really enjoyed @kebou suggestion, but instead of doing the JSON parse yourself and a lot of ifs statements, just pass an object and the correct postback callback is gonna be called!

It would require one property in this object for identifying the the callback, and the rest could be anything set by the user and passed trough the data object as you mentioned.

from bootbot.

kebou avatar kebou commented on May 13, 2024

Hey guys, I really like the idea, it will be great!

@caiolopes let me share with you this code snippet (what I found earlier), maybe it can help you during the JSON validation/parsing, because separate a string from a stringified object is harder than it seems.

const tryParseJSON = (jsonString) => {
    try {
        var o = JSON.parse(jsonString);

        // Handle non-exception-throwing cases:
        // Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
        // but... JSON.parse(null) returns null, and typeof null === "object", 
        // so we must check for that, too. Thankfully, null is falsey, so this suffices:
        if (o && typeof o === "object") {
        	return o;
        }
    }
    catch (e) { }

    return false;
}

Good luck!

from bootbot.

rohit-takhar avatar rohit-takhar commented on May 13, 2024

Hi everyone,
As @deonisnov mentioned, I also need support of regexp parameters of postback events as at bot.hear().. Can somebody explain a way out?

Thanks,
Rohit

from bootbot.

Charca avatar Charca commented on May 13, 2024

PRs are welcome. If no one is able to take this one I'll grab it after I'm done with the currently open PRs (hopefully tonight or tomorrow).

from bootbot.

mmayla avatar mmayla commented on May 13, 2024

@Charca @kebou
Does anyone know if facebook has maximum allowed length for the payload?

from bootbot.

mmayla avatar mmayla commented on May 13, 2024

I just tested it out the maximum length of the payload is 1000.
and facebook return an error with code 100

{ 
  message: '(#100) Length of param [buttons][1][payload] must be less than or equal to 1000',
  type: 'OAuthException',
  code: 100,
  fbtrace_id: 'D2BEe5nhiIE' 
}

from bootbot.

mraaroncruz avatar mraaroncruz commented on May 13, 2024

@Charca is this still a relevant issue (the postback part)? I will move the regex thread here to a new issue.

from bootbot.

WIEM-S avatar WIEM-S commented on May 13, 2024

Hi , I have a question : how my bot knows if a user is active ??
thanks

from bootbot.

Charca avatar Charca commented on May 13, 2024

@WIEM-S What do you mean by active? If you want to know if the user is currently using the Messenger app, I don't think there's a way to know that for sure.

from bootbot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.