Skip to content

Commit 14e4487

Browse files
committed
Add Session for Request
1 parent b6480a0 commit 14e4487

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

internal/handlers/handlers.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package handlers
33

44
import (
55
"encoding/json"
6+
"fmt"
67
"net/http"
8+
"time"
79

810
"go.uber.org/zap"
911

@@ -29,6 +31,20 @@ func Webhook(w http.ResponseWriter, r *http.Request) {
2931
return
3032
}
3133

34+
text := "There are no new messages for you."
35+
if req.Session.New {
36+
tz, err := time.LoadLocation(req.Timezone)
37+
if err != nil {
38+
logger.Log.Debug("cannot parse timezone")
39+
w.WriteHeader(http.StatusBadRequest)
40+
return
41+
}
42+
43+
now := time.Now().In(tz)
44+
hour, minute, _ := now.Clock()
45+
text = fmt.Sprintf("The exact time %d hours, %d minutes. %s", hour, minute, text)
46+
}
47+
3248
if req.Request.Type != models.TypeSimpleUtterance {
3349
logger.Log.Debug("unsupported request type", zap.String("type", req.Request.Type))
3450
w.WriteHeader(http.StatusUnprocessableEntity)
@@ -37,7 +53,7 @@ func Webhook(w http.ResponseWriter, r *http.Request) {
3753

3854
resp := models.Response{
3955
Response: models.ResponsePayload{
40-
Text: "Sorry, I can't do anything yet",
56+
Text: text,
4157
},
4258
Version: "1.0",
4359
}

internal/handlers/handlers_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ func TestWebhook(t *testing.T) {
1414
srv := httptest.NewServer(handler)
1515
defer srv.Close()
1616

17-
successBody := `{
18-
"response": {
19-
"text": "Sorry, I can't do anything yet"
20-
},
21-
"version": "1.0"
22-
}`
23-
2417
testCases := []struct {
2518
name string
2619
method string
@@ -62,9 +55,9 @@ func TestWebhook(t *testing.T) {
6255
{
6356
name: "method_post_success",
6457
method: http.MethodPost,
65-
body: `{"request": {"type": "SimpleUtterance", "command": "sudo do something"}, "version": "1.0"}`,
58+
body: `{"request": {"type": "SimpleUtterance", "command": "sudo do something"}, "session": {"new": true}, "version": "1.0"}`,
6659
expectedCode: http.StatusOK,
67-
expectedBody: successBody,
60+
expectedBody: `The exact time .* hours, .* minutes. There are no new messages for you.`,
6861
},
6962
}
7063

@@ -84,7 +77,7 @@ func TestWebhook(t *testing.T) {
8477

8578
assert.Equal(t, tc.expectedCode, resp.StatusCode(), "Response code didn't match expected")
8679
if tc.expectedBody != "" {
87-
assert.JSONEq(t, tc.expectedBody, string(resp.Body()))
80+
assert.Regexp(t, tc.expectedBody, string(resp.Body()))
8881
}
8982
})
9083
}

internal/models/models.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ const (
77

88
// https://yandex.ru/dev/dialogs/alice/doc/request.html
99
type Request struct {
10-
Request SimpleUtterance `json:"request"`
11-
Version string `json:"version"`
10+
Timezone string `json:"timezone"`
11+
Request SimpleUtterance `json:"request"`
12+
Session Session `json:"session"`
13+
Version string `json:"version"`
14+
}
15+
16+
// Session
17+
type Session struct {
18+
New bool `json:"new"`
1219
}
1320

1421
// Command from Request

internal/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestGzipCompression(t *testing.T) {
3939
}`
4040
successBody := `{
4141
"response": {
42-
"text": "Sorry, I can't do anything yet"
42+
"text": "There are no new messages for you."
4343
},
4444
"version": "1.0"
4545
}`

0 commit comments

Comments
 (0)