github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/alexa/request.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package alexa 20 21 import ( 22 "fmt" 23 "time" 24 25 "github.com/pkg/errors" 26 27 "github.com/e154/smart-home/common/apperr" 28 ) 29 30 // Request represents all fields sent from the Server service to the skillserver. 31 // Convenience methods are provided to pull commonly used properties out of the request. 32 type Request struct { 33 Version string `json:"version"` 34 Session Session `json:"session"` 35 Context Context `json:"context"` 36 Request ReqBody `json:"request"` 37 } 38 39 // VerifyTimestamp will parse the timestamp in the Request and verify that it is in the correct 40 // format and is not too old. True will be returned if the timestamp is valid; false otherwise. 41 func (r *Request) VerifyTimestamp() bool { 42 reqTimestamp, _ := time.Parse("2006-01-02T15:04:05Z", r.Request.Timestamp) 43 return time.Since(reqTimestamp) < time.Duration(150)*time.Second 44 } 45 46 // VerifyAppID check that the incoming application ID matches the application ID provided 47 // when running the server. This is a step required for skill certification. 48 func (r *Request) VerifyAppID(myAppID string) bool { 49 if r.Session.Application.ApplicationID == myAppID || 50 r.Context.System.Application.ApplicationID == myAppID { 51 return true 52 } 53 54 return false 55 } 56 57 // GetSessionID is a convenience method for getting the session ID out of an Request. 58 func (r *Request) GetSessionID() string { 59 return r.Session.SessionID 60 } 61 62 // GetUserID is a convenience method for getting the user identifier out of an Request. 63 func (r *Request) GetUserID() string { 64 return r.Session.User.UserID 65 } 66 67 // GetRequestType is a convenience method for getting the request type out of an Request. 68 func (r *Request) GetRequestType() string { 69 return r.Request.Type 70 } 71 72 // GetIntentName is a convenience method for getting the intent name out of an Request. 73 func (r *Request) GetIntentName() string { 74 if r.GetRequestType() == "IntentRequest" { 75 return r.Request.Intent.Name 76 } 77 78 return r.GetRequestType() 79 } 80 81 // GetSlotValue is a convenience method for getting the value of the specified slot out of an Request 82 // as a string. An error is returned if a slot with that value is not found in the request. 83 func (r *Request) GetSlotValue(slotName string) (string, error) { 84 slot, err := r.GetSlot(slotName) 85 86 if err != nil { 87 return "", err 88 } 89 90 return slot.Value, nil 91 } 92 93 // GetSlot will return an Slot from the Request with the given name. 94 func (r *Request) GetSlot(slotName string) (Slot, error) { 95 if _, ok := r.Request.Intent.Slots[slotName]; ok { 96 return r.Request.Intent.Slots[slotName], nil 97 } 98 99 return Slot{}, errors.Wrap(apperr.ErrNotFound, fmt.Sprintf("name \"%s\"", slotName)) 100 } 101 102 // AllSlots will return a map of all the slots in the Request mapped by their name. 103 func (r *Request) AllSlots() map[string]Slot { 104 return r.Request.Intent.Slots 105 } 106 107 // Locale returns the locale specified in the request. 108 func (r *Request) Locale() string { 109 return r.Request.Locale 110 }