-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |