Skip to content

Commit

Permalink
Add plugin, dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
chrl committed Sep 16, 2018
1 parent 2f331c9 commit ce8b87f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Dockerfile
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"]
61 changes: 61 additions & 0 deletions proxy.go
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")
}

0 comments on commit ce8b87f

Please sign in to comment.