Skip to content

Commit

Permalink
Parse http request body to domain command
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemeng Qin committed Oct 31, 2018
1 parent 13ff618 commit 472c5f1
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 40 deletions.
188 changes: 162 additions & 26 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 37 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
Expand All @@ -16,10 +16,43 @@
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/aws/aws-lambda-go"
version = "^1.0.1"
version = "1.6.0"

[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.15.56"

[[constraint]]
branch = "master"
name = "github.com/fnproject/fdk-go"

[[constraint]]
name = "github.com/onsi/ginkgo"
version = "1.6.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.0.0"

[[constraint]]
branch = "master"
name = "google.golang.org/api"

[[constraint]]
name = "gopkg.in/telegram-bot-api.v4"
version = "4.6.4"

[prune]
go-tests = true
unused-packages = true
4 changes: 2 additions & 2 deletions cmd/hangout/lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/aws/aws-lambda-go/lambda"
"google.golang.org/api/chat/v1"
"log"
"whosinbot/hangout_bot"
"whosinbot/hangout"
"whosinbot/dynamodb"
"whosinbot/whosinbot"
)
Expand All @@ -18,7 +18,7 @@ func main() {
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Request Body: " + request.Body)

command, err := hangout_bot.ParseDeprecatedEvent([]byte(request.Body))
command, err := hangout.ParseDeprecatedEvent([]byte(request.Body))
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, err
}
Expand Down
13 changes: 13 additions & 0 deletions hangout/hangout_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hangout_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestHangout(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Hangout Suite")
}
19 changes: 11 additions & 8 deletions hangout_bot/helpers.go → hangout/helpers.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package hangout_bot
package hangout

import (
"encoding/json"
"google.golang.org/api/chat/v1"
"log"
"strings"
"whosinbot/domain"
"errors"
)

func ParseDeprecatedEvent(requestBody []byte) (domain.Command, error) {
deprecatedEvent := chat.DeprecatedEvent{}
err := json.Unmarshal(requestBody, &deprecatedEvent)

log.Printf("deprecatedEvent: " + deprecatedEvent.Message.Text)

if err != nil {
return domain.EmptyCommand(), err
}

log.Printf("deprecatedEvent: " + deprecatedEvent.Message.Text)

// TODO: split this into a mapping function and write some tests around it
threadName := deprecatedEvent.Message.Thread.Name
argumentText := deprecatedEvent.Message.ArgumentText
tokens := strings.Split(argumentText, " ")
arguments := strings.Fields(deprecatedEvent.Message.ArgumentText)
if len(arguments) <= 0 {
return domain.EmptyCommand(), errors.New("no argument provided")
}
command := domain.Command{
ChatID: threadName,
Name: tokens[0],
Params: []string{tokens[1]},
Name: arguments[0],
Params: arguments[1:],
From: domain.User{
UserID: deprecatedEvent.Message.Sender.Name,
Name: deprecatedEvent.Message.Sender.DisplayName,
Expand Down
Loading

0 comments on commit 472c5f1

Please sign in to comment.