abhinavdahiya / go-messenger-bot

Golang bindings for the Messenger Bot API
https://godoc.org/github.com/abhinavdahiya/go-messenger-bot
MIT License
52 stars 10 forks source link

Ability to serve multiple pages from the same webhook #10

Open alsuren opened 7 years ago

alsuren commented 7 years ago

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:

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?