We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Currently If I want to read and handle incomming webhook, I must do this:
JsonMapper mapper = new DefaultJsonMapper(); WebhookObject webhook = mapper.toJavaObject(jsonCallback, WebhookObject.class); webhook.getEntryList().forEach(entry -> { entry.getMessaging().forEach(m -> { Optional.ofNullable(m.getOptin()) .map(OptinItem::getRef) //is it an OptIn? .map(Optional::of) .orElseGet(() -> Optional.ofNullable(m.getReferral()) //is it a Referral? .filter(byField(ReferralItem::getSource, "MESSENGER_CODE"::equals)) .map(ReferralItem::getRef) ) .ifPresent(referral -> { //handle referral }); }); });
which is ugly as hell.
But when you think about it..... incomming webhook is just a set of predefined Events https://developers.facebook.com/docs/messenger-platform/webhook#setup
So why not to use Observer (listener) design pattern?
You could provide an Interface:
public interface WebhookEventListener { void echo(final MessagingItem item); void optin(final MessagingItem item); void referral(final MessagingItem item); void payment(final MessagingItem item); void postback(final MessagingItem item); //... void unknown(final MessagingItem item); }
and Adapter to ease the implementation:
public abstract class AbstractWebhookEventListener implements WebhookEventListener { @Override public void echo(final MessagingItem item) { } @Override public void optin(final MessagingItem item) { } @Override public void referral(final MessagingItem item) { } @Override public void payment(final MessagingItem item) { } @Override public void postback(final MessagingItem item) { } @Override public void unknown(final MessagingItem item) { } }
and then process Webhooks like this:
JsonMapper mapper = new DefaultJsonMapper(); final WebhookEventListener optInListener = new AbstractWebhookEventListener() { @Override public void optin(final MessagingItem item) { handle(item.getOptin().getRef()); } @Override public void referral(final MessagingItem item) { handle(item.getReferral().getRef()); } }; WebhookObject webhook = Webhook.registerListener(optInListener); webhook.process(jsonCallback);
What do you think?
The text was updated successfully, but these errors were encountered:
I'll have a look at this idea ...
Sorry, something went wrong.
Issue #946 - webhook listeners added
9aff6c9
nbartels
No branches or pull requests
Currently If I want to read and handle incomming webhook, I must do this:
which is ugly as hell.
But when you think about it..... incomming webhook is just a set of predefined Events https://developers.facebook.com/docs/messenger-platform/webhook#setup
So why not to use Observer (listener) design pattern?
You could provide an Interface:
and Adapter to ease the implementation:
and then process Webhooks like this:
What do you think?
The text was updated successfully, but these errors were encountered: