Skip to content

Commit

Permalink
refactor adapters to commonchat
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Jan 29, 2018
1 parent 63c46cc commit 8c0a168
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions aws-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws lambda update-function-code --function-name='Chathooks' --zip-file='fileb://main.zip' --publish --region='us-east-1'
40 changes: 34 additions & 6 deletions examples/proxy_send/proxy_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ import (
"github.com/grokify/gotilla/net/httputilmore"
"github.com/grokify/gotilla/strings/stringsutil"
"github.com/valyala/fasthttp"

"github.com/joho/godotenv"
)

const (
WebhookUrlEnvGlip = "GLIP_WEBHOOK"
WebhookUrlEnvSlack = "SLACK_WEBHOOK"
EnvWebhookUrlGlip = "GLIP_WEBHOOK"
EnvWebhookUrlSlack = "SLACK_WEBHOOK"
EnvChathooksReqInputType = "CHATHOOKS_REQ_INPUT_TYPE"
EnvChathooksReqOutputType = "CHATHOOKS_REQ_OUTPUT_TYPE"
EnvChathooksReqToken = "CHATHOOKS_REQ_TOKEN"
EnvChathooksReqURL = "CHATHOOKS_REQ_URL"
)

type ExampleWebhookSender struct {
Expand Down Expand Up @@ -74,6 +80,7 @@ func (s *ExampleWebhookSender) SendExampleForFilepath(filepath string, inputType
Token: s.RequestParams.Token,
URL: s.RequestParams.URL,
}

fullUrl := BuildURLQueryString(s.BaseUrl, qry)
fmt.Printf("FULL_URL: %v\n", fullUrl)

Expand Down Expand Up @@ -104,7 +111,6 @@ func main() {
tokenP := flag.String("token", "token", "You token")

flag.Parse()
inputTypes := strings.ToLower(strings.TrimSpace(*inputTypeP))

qry := models.RequestParams{
InputType: *inputTypeP,
Expand All @@ -114,19 +120,41 @@ func main() {
}
fmtutil.PrintJSON(qry)

if len(os.Getenv("ENV_PATH")) > 0 {
err := godotenv.Load(os.Getenv("ENV_PATH"))
if err != nil {
panic(err)
}

if len(os.Getenv(EnvChathooksReqInputType)) > 0 {
qry.InputType = os.Getenv(EnvChathooksReqInputType)
}
if len(os.Getenv(EnvChathooksReqOutputType)) > 0 {
qry.OutputType = os.Getenv(EnvChathooksReqOutputType)
}
if len(os.Getenv(EnvChathooksReqToken)) > 0 {
qry.Token = os.Getenv(EnvChathooksReqToken)
}
if len(os.Getenv(EnvChathooksReqURL)) > 0 {
qry.URL = os.Getenv(EnvChathooksReqURL)
}
}

fmtutil.PrintJSON(qry)

sender := ExampleWebhookSender{
DocHandlersDir: config.DocsHandlersDir(),
BaseUrl: "http://localhost:8080/hook",
RequestParams: qry,
}
if len(sender.RequestParams.URL) == 0 {
sender.RequestParams.URL = os.Getenv(WebhookUrlEnvGlip)
sender.RequestParams.URL = os.Getenv(EnvWebhookUrlGlip)
}

examples := stringsutil.SliceTrimSpace(strings.Split(inputTypes, ","))
examples := stringsutil.SplitCondenseSpace(qry.InputType, ",")

for _, ex := range examples {
err := sender.SendExamplesForInputType(ex)
err := sender.SendExamplesForInputType(strings.ToLower(ex))
if err != nil {
panic(err)
}
Expand Down
4 changes: 4 additions & 0 deletions examples/proxy_send/sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CHATHOOKS_REQ_INPUT_TYPE=aha
CHATHOOKS_REQ_OUTPUT_TYPE=glip
CHATHOOKS_REQ_TOKEN=deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
CHATHOOKS_REQ_URL=https://hooks.glip.com/webhook/11112222-3333-4444-5555-666677778888
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/buaazp/fasthttprouter"
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
//"github.com/grokify/commonchat"
ccglip "github.com/grokify/commonchat/glip"
ccslack "github.com/grokify/commonchat/slack"
"github.com/grokify/gotilla/fmt/fmtutil"
fhu "github.com/grokify/gotilla/net/fasthttputil"
nhu "github.com/grokify/gotilla/net/nethttputil"
Expand Down Expand Up @@ -127,12 +130,12 @@ func getConfig() ServiceInfo {

fmtutil.PrintJSON(cfgData)
adapterSet := adapters.NewAdapterSet()
glipAdapter, err := adapters.NewGlipAdapter("")
glipAdapter, err := ccglip.NewGlipAdapter("")
if err != nil {
log.Fatal(err)
}
adapterSet.Adapters["glip"] = glipAdapter
slackAdapter, err := adapters.NewSlackAdapter("")
slackAdapter, err := ccslack.NewSlackAdapter("")
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 4 additions & 2 deletions src/adapters/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var (
)

type AdapterSet struct {
Adapters map[string]Adapter
Adapters map[string]cc.Adapter
}

func NewAdapterSet() AdapterSet {
return AdapterSet{Adapters: map[string]Adapter{}}
return AdapterSet{Adapters: map[string]cc.Adapter{}}
}

func (set *AdapterSet) SendWebhooks(hookData models.HookData) []models.ErrorInfo {
Expand Down Expand Up @@ -51,6 +51,7 @@ func (set *AdapterSet) procResponse(errs []models.ErrorInfo, req *fasthttp.Reque
return errs
}

/*
type Adapter interface {
SendWebhook(url string, message cc.Message, finalMsg interface{}) (*fasthttp.Request, *fasthttp.Response, error)
SendMessage(message cc.Message, finalMsg interface{}) (*fasthttp.Request, *fasthttp.Response, error)
Expand All @@ -63,3 +64,4 @@ func IntegrationActivitySuffix(displayName string) string {
}
return ""
}
*/
2 changes: 2 additions & 0 deletions src/adapters/glip.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package adapters

/*
import (
"encoding/json"
Expand Down Expand Up @@ -60,3 +61,4 @@ func (adapter *GlipAdapter) WebhookUID(ctx *fasthttp.RequestCtx) (string, error)
webhookUID := adapter.WebhookURLOrUID
return webhookUID, nil
}
*/
2 changes: 2 additions & 0 deletions src/adapters/slack.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package adapters

/*
import (
"fmt"
Expand Down Expand Up @@ -35,3 +36,4 @@ func (adapter *SlackAdapter) WebhookUID(ctx *fasthttp.RequestCtx) (string, error
webhookUID := fmt.Sprintf("%s", ctx.UserValue("webhookuid"))
return webhookUID, nil
}
*/
2 changes: 2 additions & 0 deletions src/adapters/slack_client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package adapters

/*
import (
"encoding/json"
"net/http"
Expand Down Expand Up @@ -83,3 +84,4 @@ func (client *SlackWebhookClient) PostWebhookFast(url string, message slack.Mess
func (client *SlackWebhookClient) PostWebhookGUIDFast(urlOrUid string, message slack.Message) (*fasthttp.Request, *fasthttp.Response, error) {
return client.PostWebhookFast(client.BuildWebhookURL(urlOrUid), message)
}
*/
2 changes: 2 additions & 0 deletions src/handlers/base_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/aws/aws-lambda-go/events"
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
//"github.com/grokify/commonchat"
"github.com/grokify/chathooks/src/adapters"
"github.com/grokify/chathooks/src/config"
"github.com/grokify/chathooks/src/models"
//"github.com/grokify/commonchat"
cc "github.com/grokify/commonchat"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
Expand Down
13 changes: 13 additions & 0 deletions src/handlers/handler_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
//"strings"

//"github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt"
//"github.com/grokify/commonchat"
"github.com/grokify/chathooks/src/adapters"
"github.com/grokify/chathooks/src/config"
//cc "github.com/grokify/commonchat"
//"github.com/grokify/gotilla/strings/stringsutil"
"github.com/valyala/fasthttp"
)
Expand All @@ -19,6 +21,10 @@ const (
QueryParamOutputURL = "url"
)

var (
ShowDisplayName = false
)

// HomeHandler is a fasthttp handler for handling the webhoo proxy homepage.
func HomeHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "%s", []byte("Chathooks\nSource: https://github.com/grokify/chathooks"))
Expand All @@ -29,6 +35,13 @@ type Configuration struct {
AdapterSet adapters.AdapterSet
}

func IntegrationActivitySuffix(displayName string) string {
if !ShowDisplayName || len(displayName) < 1 {
return ""
}
return ""
}

/*
type HookRequestData struct {
InputType string
Expand Down
9 changes: 4 additions & 5 deletions src/handlers/userlike/handler_userlike_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

"github.com/grokify/chathooks/src/adapters"
"github.com/grokify/chathooks/src/config"
"github.com/grokify/chathooks/src/handlers"
"github.com/grokify/chathooks/src/models"
Expand Down Expand Up @@ -90,7 +89,7 @@ func NormalizeOfflineMessage(cfg config.Configuration, src UserlikeOfflineMessag
ccMsg.IconURL = iconURL.String()
}

ccMsg.Activity = fmt.Sprintf("Offline message received%v", adapters.IntegrationActivitySuffix(DisplayName))
ccMsg.Activity = fmt.Sprintf("Offline message received%v", handlers.IntegrationActivitySuffix(DisplayName))

attachment := cc.NewAttachment()

Expand Down Expand Up @@ -119,7 +118,7 @@ func NormalizeChatMeta(cfg config.Configuration, src UserlikeChatMetaStartOutMes
}

ccMsg.Activity = fmt.Sprintf("%s%s",
GlipActivityForChat(src.Event, src.FeedbackMessage), adapters.IntegrationActivitySuffix(DisplayName))
GlipActivityForChat(src.Event, src.FeedbackMessage), handlers.IntegrationActivitySuffix(DisplayName))

attachment := cc.NewAttachment()

Expand Down Expand Up @@ -187,7 +186,7 @@ func NormalizeChatWidget(cfg config.Configuration, src UserlikeChatWidgetOutMess
ccMsg.IconURL = iconURL.String()
}

ccMsg.Activity = fmt.Sprintf("Chat widget configuration updated%s", adapters.IntegrationActivitySuffix(DisplayName))
ccMsg.Activity = fmt.Sprintf("Chat widget configuration updated%s", handlers.IntegrationActivitySuffix(DisplayName))

titleParts := []string{}
if len(src.StatusUrl) > 0 {
Expand Down Expand Up @@ -231,7 +230,7 @@ func NormalizeOperator(cfg config.Configuration, src UserlikeOperatorOutMessage)
}

ccMsg.Activity = fmt.Sprintf("Operator is %s%s",
src.Event, adapters.IntegrationActivitySuffix(DisplayName))
src.Event, handlers.IntegrationActivitySuffix(DisplayName))

attachment := cc.NewAttachment()

Expand Down

0 comments on commit 8c0a168

Please sign in to comment.