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

Ability to serve multiple pages from the same webhook #10

Open
alsuren opened this issue May 10, 2017 · 0 comments
Open

Ability to serve multiple pages from the same webhook #10

alsuren opened this issue May 10, 2017 · 0 comments

Comments

@alsuren
Copy link

alsuren commented May 10, 2017

I want to create a bot that multiple people could add to their pages. I like your idea of returning a channel of Callbacks, but it might need pulling apart once I add multiple pages.

Sorry if this feels a bit stream-of-consciousness: I'm mostly just trying to organise my thoughts (this is my first Go project, but I've written messenger bots in other languages).

I think that it might be enough to copy Entry.PageID into the Callback struct, and then make the Callback channel consumer maintain a mapping from PageID to BotAPI. This feels ugly though.

Alternatively we could remove BotAPI.Token, and replace it with a mapping from PageID to Page (might want renaming) which would look something like:

type Page struct {
	PageID    int64
        Token     string
        channel   chan<- Callback
}

A bunch of methods would move from being on BotAPI to being on Page.

We could then create a function like:

func (bot *BotAPI) AddPage(pageID int64, token string) (*Page, <-chan Callback)

which adds a Page to the BotAPI and then returns it along with channel of Callbacks.

Any Entry that doesn't belong to a Page could then be sent to a callback function:

func HandleUnknownPage(pageID int64, entry Entry) bool 

which would return true if it called AddPage(), to make the BotAPI retry the Entry.

SetWebhook() could then lose the <-chan Callback part of its return value.

In a world where you want to add a page to a live load-balanced website, you would need to:

  • Write the PageID and Token to a Database somewhere.
  • Tell Facebook to subscribe to the webhooks for that page.
  • When webhooks start coming in, make your HandleUnknownPage() callback pick the token out of your Database and call AddPage().

Of course, if you're load-balancing your webhook requests onto random servers then they won't be showing up in a stream anymore, so the <-chan Callback interface stops making sense, and you might as well just use callback functions 😞 . Also, if your HandleUnknownPage() callback is expensive then you might end up in trouble if you accidentally subscribe to webhooks without writing the Token to the DB.

Is this an approach that's worth pursuing, or should I just look for a callback-function-based messenger lib that's already capable of handling multiple pages?

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

1 participant