Skip to content

Commit

Permalink
Re-architecture WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
col committed Jun 5, 2018
1 parent 689adac commit 44ecde9
Show file tree
Hide file tree
Showing 17 changed files with 739 additions and 153 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

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

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build:
dep ensure
GOOS=linux go build -o bin/telegram telegram/main.go
GOOS=linux go build -o bin/telegram_lambda cmd/telegram/lambda/main.go
GOOS=linux go build -o bin/telegram_http cmd/telegram/http/main.go
78 changes: 78 additions & 0 deletions Plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# WhosInBot Plan


## Package whosinbot
WhosInBot
DataStore DataStore
HandleCommand(command Command) (Response, Error)

## Package whosinbot/domain
Command struct
name string
params string[]

Response struct
message string

RollCall struct
ChatID string
Title string

RollCallResponse struct
ChatID int64
UserID int64
Response string
Reasons string


DataStore interface
SetRollCall(RollCall)
DeleteRollCall(RollCall)
SetResponse(RollCallResponse)
DeleteResponse(RollCallResponse)
## Package whosinbot/dynamodb
DynamoDataStore
SetRollCall(RollCall)
DeleteRollCall(RollCall)
SetResponse(RollCallResponse)
DeleteResponse(RollCallResponse)
NewDynamoDataStore(DynamoConfig)

## Package whoinbot/helpers
Helpers

## Package telegram
NewTelegram(TelegramConfig)
Telegram
BotApi
ParseUpdateRequest(string) (Command, error)
SendMessage(Response) (error)

## Package cmd/telegram_lambda

Main
LoadDynamoConfig
LoadTelegramConfig

## Package cmd/telegram_http

Main
LoadDynamoConfig
LoadTelegramConfig
LoadHttpConfig

##Package whosinbot/config
TelegramConfig
HttpConfig
DynamoConfig


## Commands
- start_roll_call (title)
- in (reason)
- out (reason)
- set_title (title)
- set_in_for (name)
- end_roll_call
- ssh


4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ curl -XPOST https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getWebhookInfo

Install AWS CLI tools

```
```
brew install awscli
aws configure --profile col.w.harris
```
Expand All @@ -40,7 +40,7 @@ make build
```
serverless deploy
```

## Links

- https://github.com/go-telegram-bot-api/telegram-bot-api
Expand Down
20 changes: 20 additions & 0 deletions cmd/telegram/http/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/col/whosinbot/dynamodb"
"net/http"
"fmt"
"github.com/col/whosinbot/whosinbot"
whttp "github.com/col/whosinbot/http"
)

func main() {
dataStore := &dynamodb.DynamoDataStore{}
bot := &whosinbot.WhosInBot{ DataStore: dataStore }

http.Handle("/webhook", &whttp.WebhookHandler{WhosInBot: bot})
port := 8080
serverConfig := fmt.Sprintf(":%d", port)
http.ListenAndServe(serverConfig, nil)
}

40 changes: 40 additions & 0 deletions cmd/telegram/lambda/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"github.com/col/whosinbot/dynamodb"
"context"
"github.com/col/whosinbot/telegram"
"github.com/col/whosinbot/whosinbot"
)

func main() {
lambda.Start(Handler)
}

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

// Parse Command
command, err := telegram.ParseUpdate([]byte(request.Body))
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, err
}

// Process Command
dataStore := &dynamodb.DynamoDataStore{}
bot := whosinbot.WhosInBot{ DataStore: dataStore }
response, err := bot.HandleCommand(command)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, err
}

// Send Response
api := telegram.NewTelegram(request.PathParameters["token"])
err = api.SendResponse(response)
if err != nil {
return events.APIGatewayProxyResponse{StatusCode: 400}, err
}

return events.APIGatewayProxyResponse{StatusCode: 200}, nil
}
81 changes: 0 additions & 81 deletions core/message_handler.go

This file was deleted.

58 changes: 58 additions & 0 deletions domain/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package domain

import "strings"

type Command struct {
ChatID int64
Name string
Params []string
From User
}

func (c Command) ParamsString() string {
return strings.Join(c.Params, " ")
}

type User struct {
UserID int64
Username string
}

func EmptyCommand() Command {
return Command{
ChatID: 0,
Name: "",
Params: nil,
From: User{},
}
}

type Response struct {
ChatID int64
Text string
}

type DataStore interface {
StartRollCall(rollCall RollCall) error
EndRollCall(rollCall RollCall) error

SetResponse(rollCallResponse RollCallResponse) error

GetRollCall(chatID int64) (*RollCall, error)
}

type RollCall struct {
ChatID int64
Title string
In []RollCallResponse
Out []RollCallResponse
Maybe []RollCallResponse
}

type RollCallResponse struct {
ChatID int64
UserID int64
Name string
Response string
Reason string
}
Loading

0 comments on commit 44ecde9

Please sign in to comment.