func jeopardyAnswer(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) { msg := "" echoResp := alexa.NewEchoResponse() if session.CurrentQuestion.Answer == "" { echoResp.OutputSpeech("You need to get a question before answering!").EndSession(false) return echoResp, session } answer, err := echoReq.GetSlotValue("Answer") log.Println(answer) if err != nil { echoResp.OutputSpeech("We need an answer!").EndSession(false) return echoResp, session } if strings.ToLower(answer) == strings.ToLower(session.CurrentQuestion.Answer) { msg += getRandom(JeopardyRightAnswer) session.Dollars = session.Dollars + session.CurrentQuestion.Value } else { msg += getRandom(JeopardyWrongAnswer) + " The correct answer was " + session.CurrentQuestion.Answer + ". " } session.NumQuestions++ session.CurrentQuestion = JeopardyQuestion{} msg += "You're at " + strconv.Itoa(session.Dollars) + " after " + strconv.Itoa(session.NumQuestions) + " questions. Please pick another category." echoResp.OutputSpeech(msg).Card("Answer", session.CurrentQuestion.Answer).EndSession(false) return echoResp, session }
func jeopardyStart(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) { catNames := []string{} for k, _ := range JeopardyCategories { catNames = append(catNames, k) } msg := "" if echoReq.GetIntentName() == "StartJeopardy" { msg = JeopardyGreetings[rand.Intn(len(JeopardyGreetings))] + " Please pick one of the following categories: " } else { msg = "The categories are " } msg += strings.Join(catNames, ", ") echoResp := alexa.NewEchoResponse().OutputSpeech(msg).EndSession(false) return echoResp, session }
func jeopardyCategory(echoReq *alexa.EchoRequest, session *JeopardySession) (*alexa.EchoResponse, *JeopardySession) { msg := "" echoResp := alexa.NewEchoResponse() // Declare the category category, err := echoReq.GetSlotValue("Category") _, catExists := JeopardyCategories[category] if err != nil || !catExists { catNames := []string{} for k, _ := range JeopardyCategories { catNames = append(catNames, k) } category = getRandom(catNames) msg = msg + getRandom(JeopardyCatSelect) + category + ". " } else { category = strings.ToLower(category) } clue, err := getJServiceClue(JeopardyCategories[category]) if err != nil { clue, err = getJServiceClue(JeopardyCategories[category]) if err != nil { echoResp := alexa.NewEchoResponse().OutputSpeech("I'm sorry, but I can't seem to get a question right now.").EndSession(true) return echoResp, session } } msg += "From " + category + " for " + strconv.Itoa(clue.Value) + ". " + clue.Question + ". I need your answer in the form of a question." session.CurrentQuestion.Category = category session.CurrentQuestion.Answer = sanitize.HTML(clue.Answer) session.CurrentQuestion.Question = clue.Question session.CurrentQuestion.Value = clue.Value log.Println(session.CurrentQuestion.Question) log.Println(session.CurrentQuestion.Answer) echoResp.OutputSpeech(msg).Card("Question", msg).Reprompt("Times up. I need your answer in the form of a question.").EndSession(false) return echoResp, session }
func EchoLaunchHandler(req *alexa.EchoRequest, res *alexa.EchoResponse) { mongo, db := MongoConnect() GetSession(db, req.GetSessionID()) defer mongo.Close() user, err := UserIndex.getUserByAmazonID(req.GetUserID()) if err != nil { user = UserIndex.addUser(req.GetUserID()) res.OutputSpeech(fmt.Sprintf("Welcome new user. Your ID is %d. You need some clients to connect to us first!", user.ID)).EndSession(true) return } res.OutputSpeech("Which computer do you want to connect to?").EndSession(false) }
func EchoIntentHandler(req *alexa.EchoRequest, res *alexa.EchoResponse) { mongo, db := MongoConnect() defer mongo.Close() session := GetSession(db, req.GetSessionID()) user, err := UserIndex.getUserByAmazonID(req.GetUserID()) if err != nil { res.OutputSpeech("Sorry, we don't have an account setup for this user.").EndSession(true) return } switch req.GetIntentName() { case "SelectBox": target, _ := req.GetSlotValue("target") if target == "" { res.OutputSpeech("I didn't get that. Can you tell me the computer you want again?").EndSession(false) return } if !user.clientExists(target) { res.OutputSpeech("The computer you requested isn't online.").EndSession(true) return } session.Target = target session.Update(db) runCommand(session, user, res) case "RunCommand": cmd, err := req.GetSlotValue("cmd") if err != nil { res.OutputSpeech("I'm sorry, but what should I tell " + session.Target + "to do?").EndSession(false) return } session.Cmd = cmd payload, _ := req.GetSlotValue("payload") session.Payload = payload session.Update(db) runCommand(session, user, res) case "ListCommand": names := []string{} for _, client := range user.Clients { if client.isActive() { names = append(names, client.Name) } } if len(names) == 0 { res.OutputSpeech("You have no available computes.").EndSession(true) } else if len(names) < 2 { res.OutputSpeech("You have one available computer. It's name is " + names[0]).EndSession(true) } else { res.OutputSpeech("You have %d available computers. They are: " + strings.Join(names, ", ")).EndSession(true) } case "IdCommand": res.OutputSpeechSSML(fmt.Sprintf("<speak>Your user ID is <say-as interpret-as=\"digits\">%d</say-as></speak>", user.ID)).EndSession(true) default: res.OutputSpeech("I'm sorry, I didn't understand your request.").EndSession(false) } }