Comments (18)
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.
@caiolopes absolutely, go ahead!
from bootbot.
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.
@Charca Yes, exactly! Can I try to implement this and make a PR?
from bootbot.
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.
Hey @caiolopes, my workaround is the following:
- stringify the object that I want to pass as payload
- 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.
@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.
@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.
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.
@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.
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.
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.
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.
@Charca @kebou
Does anyone know if facebook has maximum allowed length for the payload?
from bootbot.
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.
@Charca is this still a relevant issue (the postback part)? I will move the regex thread here to a new issue.
from bootbot.
Hi , I have a question : how my bot knows if a user is active ??
thanks
from bootbot.
@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)
- How can i delete or update message using your lib. HOT 1
- The reusable attachment ID
- how do i get location from the user via telegram
- Https server ? HOT 2
- YouTube Thumbnail
- Request to support handover protocol HOT 2
- Bot answers only on one messge from user HOT 1
- Error: listen EADDRINUSE :::3000 HOT 2
- Question: Facebook Groups HOT 1
- How to disable request signature verification?
- [Feature request] Account linking
- Release latest update. HOT 1
- Conversation context gets lost when postback listener is set
- Integration with express HOT 1
- i18n and sessions HOT 1
- chat.say() not sending in order HOT 1
- Allow for quick replies to be sent with template
- Error sending message: getaddrinfo EAI_AGAIN graph.facebook.com
- Send img from local storage
- Facebook webhook not approving URL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bootbot.