Skip to content

Commit

Permalink
Change chat_id from number to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemeng Qin committed Oct 17, 2018
1 parent 8a8bdcb commit 8228a50
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 113 deletions.
12 changes: 6 additions & 6 deletions domain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "strings"
import "time"

type Command struct {
ChatID int64
ChatID string
Name string
Params []string
From User
Expand Down Expand Up @@ -35,15 +35,15 @@ type User struct {

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

type Response struct {
ChatID int64
ChatID string
Text string
}

Expand All @@ -56,12 +56,12 @@ type DataStore interface {

SetResponse(rollCallResponse RollCallResponse) error

GetRollCall(chatID int64) (*RollCall, error)
GetRollCall(chatID string) (*RollCall, error)
LoadRollCallResponses(rollCall *RollCall) error
}

type RollCall struct {
ChatID int64
ChatID string
Title string
Quiet bool
In []RollCallResponse
Expand All @@ -81,7 +81,7 @@ func (r *RollCall) AddResponse(response RollCallResponse) {
}

type RollCallResponse struct {
ChatID int64
ChatID string
UserID string
Name string
Status string
Expand Down
19 changes: 9 additions & 10 deletions dynamodb/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"os"
"sort"
"strconv"
"time"
)

Expand Down Expand Up @@ -65,9 +64,9 @@ func (d DynamoDataStore) StartRollCall(rollCall domain.RollCall) error {
return nil
}

func (d DynamoDataStore) GetRollCall(chatID int64) (*domain.RollCall, error) {
func (d DynamoDataStore) GetRollCall(chatID string) (*domain.RollCall, error) {
// DEBUG
log.Printf("GetRollCall: %+d\n", chatID)
log.Printf("GetRollCall: %s\n", chatID)

input := &dynamodb.GetItemInput{
TableName: aws.String(os.Getenv("ROLLCALL_TABLE")),
Expand Down Expand Up @@ -113,9 +112,9 @@ func (d DynamoDataStore) LoadRollCallResponses(rollCall *domain.RollCall) error
return nil
}

func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallResponse, error) {
func (d DynamoDataStore) getRollCallResponses(chatID string) ([]domain.RollCallResponse, error) {
// DEBUG
log.Printf("getRollCallResponses ChatID: %+d\n", chatID)
log.Printf("getRollCallResponses ChatID: %s\n", chatID)

keyConditionExpression := "chat_id = :chat_id"
selectString := dynamodb.SelectAllAttributes
Expand All @@ -124,7 +123,7 @@ func (d DynamoDataStore) getRollCallResponses(chatID int64) ([]domain.RollCallRe
Select: &selectString,
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":chat_id": {
N: aws.String(strconv.Itoa(int(chatID))),
S: aws.String(chatID),
},
},
KeyConditionExpression: &keyConditionExpression,
Expand Down Expand Up @@ -309,18 +308,18 @@ func (d DynamoDataStore) SetQuiet(rollCall domain.RollCall, quiet bool) error {
return nil
}

func getRollCallKey(chatID int64) map[string]*dynamodb.AttributeValue {
func getRollCallKey(chatID string) map[string]*dynamodb.AttributeValue {
return map[string]*dynamodb.AttributeValue{
"chat_id": {
N: aws.String(strconv.Itoa(int(chatID))),
S: aws.String(chatID),
},
}
}

func getResponseKey(chatID int64, userID string) map[string]*dynamodb.AttributeValue {
func getResponseKey(chatID string, userID string) map[string]*dynamodb.AttributeValue {
return map[string]*dynamodb.AttributeValue{
"chat_id": {
N: aws.String(strconv.Itoa(int(chatID))),
S: aws.String(chatID),
},
"user_id": {
S: aws.String(userID),
Expand Down
4 changes: 2 additions & 2 deletions dynamodb/data_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func setUp() {
func TestStartRollCall(t *testing.T) {
setUp()
rollCall := domain.RollCall{
ChatID: 123,
ChatID: "123",
Title: "Dinner",
Quiet: false,
}
Expand All @@ -86,7 +86,7 @@ func TestStartRollCall(t *testing.T) {
func TestStartRollCallSetsTitleToSpaceWhenEmpty(t *testing.T) {
setUp()
rollCall := domain.RollCall{
ChatID: 123,
ChatID: "123",
}
dataSource.StartRollCall(rollCall)
assert.True(t, mockDynamoService.UpdateItemCalled)
Expand Down
4 changes: 2 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ resources:
AttributeDefinitions:
-
AttributeName: chat_id
AttributeType: N
AttributeType: S
KeySchema:
-
AttributeName: chat_id
Expand All @@ -90,7 +90,7 @@ resources:
AttributeDefinitions:
-
AttributeName: chat_id
AttributeType: N
AttributeType: S
-
AttributeName: user_id
AttributeType: S
Expand Down
2 changes: 1 addition & 1 deletion telegram/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ParseUpdate(requestBody []byte) (domain.Command, error) {
}
// TODO: split this into a mapping function and write some tests around it
command := domain.Command{
ChatID: update.Message.Chat.ID,
ChatID: strconv.Itoa(int(update.Message.Chat.ID)),
Name: update.Message.Command(),
Params: strings.Fields(update.Message.CommandArguments()),
From: domain.User{
Expand Down
8 changes: 7 additions & 1 deletion telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"whosinbot/domain"
"gopkg.in/telegram-bot-api.v4"
"os"
"strconv"
)

type Telegram struct {
Expand All @@ -31,7 +32,12 @@ func (t *Telegram) SendResponse(response *domain.Response) error {
return err
}

_, err = bot.Send(tgbotapi.NewMessage(response.ChatID, response.Text))
messageChatId, err := strconv.ParseInt(response.ChatID, 10, 64)
if err != nil {
return err
}

_, err = bot.Send(tgbotapi.NewMessage(messageChatId, response.Text))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8228a50

Please sign in to comment.