-
Notifications
You must be signed in to change notification settings - Fork 25
/
handler.go
68 lines (57 loc) · 2.4 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package botsfw
import (
"context"
"github.com/dal-go/dalgo/dal"
"net/http"
)
type HttpRouter interface {
Handle(method string, path string, handle http.HandlerFunc)
}
// WebhookHandler handles requests from a specific bot API
// This is implemented by different botsfw packages, e.g. https://github.com/bots-go-framework/bots-fw-telegram
// TODO: Simplify interface by decomposing it into smaller interfaces? Probably next method could/should be decoupled: CreateBotCoreStores()
type WebhookHandler interface {
// RegisterHttpHandlers registers HTTP handlers for bot API
RegisterHttpHandlers(driver WebhookDriver, botHost BotHost, router HttpRouter, pathPrefix string)
// HandleWebhookRequest handles incoming webhook request
HandleWebhookRequest(w http.ResponseWriter, r *http.Request)
// GetBotContextAndInputs returns bot context and inputs for current request
// It returns multiple inputs as some platforms (like Facebook Messenger)
// may send multiple message in one request
GetBotContextAndInputs(c context.Context, r *http.Request) (botContext *BotContext, entriesWithInputs []EntryInputs, err error)
// CreateBotCoreStores TODO: should be deprecated after migration to dalgo
//CreateBotCoreStores(appContext BotAppContext, r *http.Request) botsfwdal.DataAccess
// CreateWebhookContext creates WebhookContext for current webhook request
CreateWebhookContext(args CreateWebhookContextArgs) (WebhookContext, error)
GetResponder(w http.ResponseWriter, whc WebhookContext) WebhookResponder
HandleUnmatched(whc WebhookContext) (m MessageFromBot)
//ProcessInput(input webhookInput, entry *WebhookEntry)
}
type CreateWebhookContextArgs struct {
HttpRequest *http.Request // TODO: Can we get rid of it? Needed for botHost.GetHTTPClient()
AppContext BotAppContext
BotContext BotContext
WebhookInput WebhookInput
Tx dal.ReadwriteTransaction
//BotCoreStores botsfwdal.DataAccess
GaMeasurement GaQueuer
}
func NewCreateWebhookContextArgs(
httpRequest *http.Request,
appContext BotAppContext,
botContext BotContext,
webhookInput WebhookInput,
tx dal.ReadwriteTransaction,
//botCoreStores botsfwdal.DataAccess,
gaMeasurement GaQueuer,
) CreateWebhookContextArgs {
return CreateWebhookContextArgs{
HttpRequest: httpRequest,
AppContext: appContext,
BotContext: botContext,
WebhookInput: webhookInput,
Tx: tx,
//BotCoreStores: botCoreStores,
GaMeasurement: gaMeasurement,
}
}