-
Notifications
You must be signed in to change notification settings - Fork 4
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
Gemeng Qin
committed
Oct 10, 2018
1 parent
47cfa18
commit 8a8bdcb
Showing
2 changed files
with
83 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,34 @@ | ||
package chat_bot | ||
|
||
import ( | ||
"encoding/json" | ||
"google.golang.org/api/chat/v1" | ||
"log" | ||
"strings" | ||
"whosinbot/domain" | ||
) | ||
|
||
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 | ||
} | ||
// 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, " ") | ||
command := domain.Command{ | ||
ChatID: threadName, | ||
Name: tokens[0], | ||
Params: []string{tokens[1]}, | ||
From: domain.User{ | ||
UserID: deprecatedEvent.Message.Sender.Name, | ||
Name: deprecatedEvent.Message.Sender.DisplayName, | ||
}, | ||
} | ||
return command, err | ||
} |
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,49 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"google.golang.org/api/chat/v1" | ||
"log" | ||
"whosinbot/chat_bot" | ||
"whosinbot/dynamodb" | ||
"whosinbot/whosinbot" | ||
) | ||
|
||
func main() { | ||
lambda.Start(Handler) | ||
} | ||
|
||
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | ||
log.Printf("Request Body: " + request.Body) | ||
|
||
command, err := chat_bot.ParseDeprecatedEvent([]byte(request.Body)) | ||
if err != nil { | ||
return events.APIGatewayProxyResponse{StatusCode: 400}, err | ||
} | ||
|
||
// Process Command | ||
dataStore := dynamodb.NewDynamoDataStore() | ||
bot := whosinbot.WhosInBot{DataStore: dataStore} | ||
response, err := bot.HandleCommand(command) | ||
if err != nil { | ||
return events.APIGatewayProxyResponse{StatusCode: 400}, err | ||
} | ||
|
||
// Send Response | ||
if response == nil { | ||
return events.APIGatewayProxyResponse{StatusCode: 400}, nil | ||
} | ||
|
||
message := chat.Message{ | ||
Text: response.Text, | ||
} | ||
messageJOSN, _ := message.MarshalJSON() | ||
|
||
resBody := string(messageJOSN) | ||
log.Printf("response body: %s\n", resBody) | ||
return events.APIGatewayProxyResponse{StatusCode: 200, Body: resBody}, nil | ||
} | ||
|
||
|