diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..18bc9b4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Multi-stage build setup (https://docs.docker.com/develop/develop-images/multistage-build/) + +# Stage 1 (to create a "build" image, ~850MB) +FROM golang:1.11 AS builder +RUN go version + +COPY . /go/src/github.com/bottalk/bottalk-proxy-plugin +WORKDIR /go/src/github.com/bottalk/bottalk-proxy-plugin +RUN go get -v . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o plugin . + +# Stage 2 (to create a downsized "container executable", ~7MB) + +FROM alpine:3.7 +RUN apk --no-cache add ca-certificates +WORKDIR /root/ +COPY --from=builder /go/src/github.com/bottalk/bottalk-proxy-plugin/plugin . + +ENTRYPOINT ["./plugin"] diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..0c28ad9 --- /dev/null +++ b/proxy.go @@ -0,0 +1,61 @@ +package main + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "log" + "net/http" + + bottalk "github.com/bottalk/go-plugin" +) + +type btRequest struct { + Token string `json:"token"` + UserID string `json:"user"` + Vars map[string]string `json:"vars"` + Input json.RawMessage `json:"input"` +} + +func errorResponse(message string) string { + return "{\"result\": \"fail\",\"message\":\"" + message + "\"}" +} + +func main() { + + plugin := bottalk.NewPlugin() + plugin.Name = "Simple Proxy Plugin" + plugin.Description = "This plugin proxies your alexa/google requests entirely to endpoints" + + plugin.Actions = map[string]bottalk.Action{"call": bottalk.Action{ + Name: "call", + Description: "This action calls remote endpoint", + Endpoint: "/call", + Action: func(r *http.Request) string { + + var BTR btRequest + decoder := json.NewDecoder(r.Body) + + err := decoder.Decode(&BTR) + if err != nil { + return errorResponse(err.Error()) + } + + if len(BTR.Vars["url"]) < 5 { + return errorResponse("Call webhook is incorrect") + } + + res, err := http.Post(BTR.Vars["url"], "application/json", bytes.NewBuffer([]byte(BTR.Input))) + if err != nil { + return errorResponse(err.Error()) + } + output, _ := ioutil.ReadAll(res.Body) + log.Println(string(output)) + + return "{\"result\": \"ok\",\"output\":" + string(output) + "}" + }, + Params: map[string]string{"url": "Endpoint url to call"}, + }} + + plugin.Run(":9080") +}