Skip to content

Commit

Permalink
new middleware fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lyricat committed Apr 9, 2023
1 parent accc9c3 commit adad734
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
MiddlewareBotasticSearch = "botastic-search"
MiddlewareDuckduckgoSearch = "duckduckgo-search"
MiddlewareIntentRecognition = "intent-recognition"
MiddlewareFetch = "fetch"
)

const MiddlewareProcessCodeUnknown = -1
Expand Down
2 changes: 2 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ var (
ErrInvalidModel = errors.New("invalid model")

ErrTokenExceedLimit = errors.New("token exceed limit")

ErrBadMiddlewareOptions = errors.New("bad middleware options")
)
45 changes: 40 additions & 5 deletions service/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package middleware
import (
"context"
"fmt"
"io"
"net/http"
"strings"

"github.com/pandodao/botastic/core"
Expand All @@ -16,11 +18,14 @@ func New(
indexz core.IndexService,
) *service {
middlewareMap := make(map[string]*core.Middleware)
middlewareMap["botastic-search"] = &core.Middleware{
Name: "botastic-search",
}
middlewareMap["duckduckgo-search"] = &core.Middleware{
Name: "duckduckgo-search",
for _, v := range []string{
core.MiddlewareBotasticSearch,
core.MiddlewareDuckduckgoSearch,
core.MiddlewareFetch,
} {
middlewareMap[v] = &core.Middleware{
Name: v,
}
}
return &service{
cfg: cfg,
Expand Down Expand Up @@ -144,6 +149,34 @@ func (s *service) ProcessDuckduckgoSearch(ctx context.Context, m *core.Middlewar
return ret, nil
}

func (s *service) ProcessFetch(ctx context.Context, m *core.Middleware, input string) (*core.MiddlewareProcessResult, error) {
// query an URL and return the result
val, ok := m.Options["url"]
if !ok {
return nil, core.ErrBadMiddlewareOptions
}
url := string(val.(string))
req, _ := http.NewRequest("GET", url, nil)
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

ret := &core.MiddlewareProcessResult{
Name: m.Name,
Code: core.MiddlewareProcessCodeOK,
Result: string(body),
}

return ret, nil
}

func (s *service) Process(ctx context.Context, m *core.Middleware, input string) (*core.MiddlewareProcessResult, error) {
switch m.Name {
case core.MiddlewareBotasticSearch:
Expand All @@ -152,6 +185,8 @@ func (s *service) Process(ctx context.Context, m *core.Middleware, input string)
return s.ProcessDuckduckgoSearch(ctx, m, input)
case core.MiddlewareIntentRecognition:
return s.ProcessIntentRecognition(ctx, m, input)
case core.MiddlewareFetch:
return s.ProcessFetch(ctx, m, input)
}
return nil, nil
}

0 comments on commit adad734

Please sign in to comment.