Skip to content

Commit

Permalink
Support hangouts chat bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemeng Qin committed Oct 10, 2018
1 parent 47cfa18 commit 8a8bdcb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
34 changes: 34 additions & 0 deletions chat_bot/helpers.go
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
}
49 changes: 49 additions & 0 deletions cmd/chat_bot/lambda/main.go
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
}


0 comments on commit 8a8bdcb

Please sign in to comment.