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

Use Listener pattern for incomming webhooks #946

Closed
eximius313 opened this issue Dec 15, 2018 · 1 comment
Closed

Use Listener pattern for incomming webhooks #946

eximius313 opened this issue Dec 15, 2018 · 1 comment
Assignees
Milestone

Comments

@eximius313
Copy link

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?

@nbartels
Copy link
Contributor

I'll have a look at this idea ...

@nbartels nbartels self-assigned this Jun 5, 2020
@nbartels nbartels added this to the 3.7.0 milestone Jun 5, 2020
nbartels added a commit that referenced this issue Jun 9, 2020
@nbartels nbartels closed this as completed Jun 9, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants