Skip to content

Instantly share code, notes, and snippets.

@jsgoecke
Last active August 1, 2022 02:25
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jsgoecke/872d31ae29a70bc5db44e2cea4fe64f9 to your computer and use it in GitHub Desktop.
Save jsgoecke/872d31ae29a70bc5db44e2cea4fe64f9 to your computer and use it in GitHub Desktop.
forward
backward
reverse
into
out
out of
{
"intents": [
{
"intent": "SummonTeslaVehicle",
"slots": [
{
"name": "Action",
"type": "DIRECTION"
}
]
}
}
SummonTeslaVehicle pull the car {Action}
SummonTeslaVehicle pull the tesla {Action}
SummonTeslaVehicle to pull the car {Action}
SummonTeslaVehicle drive the car {Action}
SummonTeslaVehicle drive the vehicle {Action}
SummonTeslaVehicle drive the vehicle in {Action}
SummonTeslaVehicle pull the car {Action} the garage
SummonTeslaVehicle please pull the car {Action} the garage
SummonTeslaVehicle to pull the car {Action} the garage
SummonTeslaVehicle to please pull the car {Action} the garage
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/apex/go-apex"
"github.com/jsgoecke/tesla"
)
// Struct for the Intent Schema
type AlexaSkillEvent struct {
Session struct {
Sessionid string `json:"sessionId"`
Application struct {
Applicationid string `json:"applicationId"`
} `json:"application"`
Attributes interface{} `json:"attributes"`
User struct {
Userid string `json:"userId"`
Accesstoken interface{} `json:"accessToken"`
} `json:"user"`
New bool `json:"new"`
} `json:"session"`
Request struct {
Type string `json:"type"`
Requestid string `json:"requestId"`
Timestamp string `json:"timestamp"`
Intent struct {
Name string `json:"name"`
Slots struct {
Action struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"Action"`
Name struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"Name"`
} `json:"slots"`
} `json:"intent"`
Reason interface{} `json:"reason"`
} `json:"request"`
}
// Struct for the resonse to Alexa
type AlexaResponse struct {
Version string `json:"version,omitempty"`
Response struct {
Outputspeech struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"outputSpeech"`
Reprompt interface{} `json:"reprompt,omitempty"`
Shouldendsession bool `json:"shouldEndSession"`
} `json:"response"`
Sessionattributes struct {
} `json:"sessionAttributes,omitempty"`
}
var (
teslaClient *tesla.Client
vehicles tesla.Vehicles
)
func init() {
fmt.Fprintf(os.Stderr, "Starting up @->%s!\n", time.Now().String())
var err error
teslaClient, err = tesla.NewClient(&tesla.Auth{
ClientID: os.Getenv("TESLA_CLIENT_ID"),
ClientSecret: os.Getenv("TESLA_CLIENT_SECRET"),
Email: os.Getenv("TESLA_USERNAME"),
Password: os.Getenv("TESLA_PASSWORD"),
})
if err != nil {
panic(err)
}
vehicles, err = teslaClient.Vehicles()
if err != nil {
panic(err)
}
}
func main() {
apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
fmt.Fprintf(os.Stderr, "Event->%s\n", string(event))
skillEvent := &AlexaSkillEvent{}
if err := json.Unmarshal(event, skillEvent); err != nil {
return nil, err
}
response := processRequest(skillEvent)
body, err := json.Marshal(response)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "Response->%s\n", string(body))
return response, nil
})
}
func processRequest(event *AlexaSkillEvent) AlexaResponse {
text := ""
switch event.Request.Intent.Name {
case "SummonTeslaVehicle":
action := event.Request.Intent.Slots.Action.Value
switch action {
case "forward":
vehicles[0].AutoparkForward()
text = "I will now put your Tesla in drive. Keep an eye on it!"
case "backward", "reverse":
vehicles[0].AutoparkReverse()
text = "I will now put your Tesla in reverse. Keep an eye on it!"
case "into":
vehicles[0].TriggerHomelink()
time.Sleep(3 * time.Second)
vehicles[0].AutoparkReverse()
text = "I will now pull your Tesla into the garage. Keep an eye on it!"
case "out", "out of":
vehicles[0].TriggerHomelink()
time.Sleep(3 * time.Second)
vehicles[0].AutoparkForward()
text = "I will now pull your Tesla out of the garage. Keep an eye on it!"
}
default:
text = "I have no idea what you just said, could you speak more clearly"
}
fmt.Fprintf(os.Stderr, "Text->%s\n", text)
return generateAlexaResponse(text)
}
func generateAlexaResponse(text string) AlexaResponse {
var response AlexaResponse
response.Version = "1.0"
response.Response.Outputspeech.Type = "PlainText"
response.Response.Shouldendsession = true
response.Response.Outputspeech.Text = text
return response
}
@kennethchapman99
Copy link

I just wrote an Alexa app for Tesla too -- using Node.js (thanks 10000% to Tim Dorr who did all the real work of course) -- does charge status, gives location and time to my house, shift_state, and more...

Was thinking that the new Account Linking option in Alexa kit would be useful to allow distribution to the community -- did you look into that already to know if its feasible? Assuming yes given oAuth support in v6.0 API

@ddaugher
Copy link

ddaugher commented Sep 3, 2016

it appears I am able to successfully connect to the tesla... but I continue to get 'The response is invalid' as response... any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment