Example #1
0
func EchoJeopardy(w http.ResponseWriter, r *http.Request) {
	echoReq := context.Get(r, "echoRequest").(*alexa.EchoRequest)

	// Start up Mongo!
	mongodb, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}

	col := mongodb.DB("echo").C("jeopardy")
	defer mongodb.Close()

	log.Println(echoReq.GetRequestType())
	log.Println(echoReq.GetSessionID())

	if echoReq.GetRequestType() == "LaunchRequest" {
		session := getJeopardySession(col, echoReq.GetSessionID())

		echoResp, session := jeopardyStart(echoReq, session)

		json, _ := echoResp.String()
		w.Header().Set("Content-Type", "application/json;charset=UTF-8")
		w.Write(json)
	} else if echoReq.GetRequestType() == "IntentRequest" {
		session := getJeopardySession(col, echoReq.GetSessionID())

		log.Println(echoReq.GetIntentName())

		var echoResp *alexa.EchoResponse

		switch echoReq.GetIntentName() {
		case "StartJeopardy":
			echoResp, session = jeopardyStart(echoReq, session)
		case "PickCategory":
			if session.CurrentQuestion.Category == "" {
				echoResp, session = jeopardyCategory(echoReq, session)
			} else {
				echoResp, session = jeopardyAnswer(echoReq, session)
			}

			session.Update(col)
		case "AnswerQuestion":
			echoResp, session = jeopardyAnswer(echoReq, session)
			session.Update(col)
		case "QuitGame":
			echoResp = alexa.NewEchoResponse().OutputSpeech("Ok. You ended with " + strconv.Itoa(session.Dollars) + " after " + strconv.Itoa(session.NumQuestions) + " questions.").EndSession(true)
		default:
			echoResp = alexa.NewEchoResponse().OutputSpeech("I'm sorry, I didn't get that. Can you say that again?").EndSession(false)
		}

		json, _ := echoResp.String()
		w.Header().Set("Content-Type", "application/json;charset=UTF-8")
		w.Write(json)
	} else if echoReq.GetRequestType() == "SessionEndedRequest" {
		//session.Delete(col)
	}
}
Example #2
0
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)
}
Example #3
0
func runCommand(session *TunnelSession, user *User, res *alexa.EchoResponse) {
	if session.Target == "" {
		res.OutputSpeech("Which computer do you want to run this on?").EndSession(false)
		return
	}

	if session.Cmd == "" {
		res.OutputSpeech("What should I tell " + session.Target + "to do?").EndSession(false)
		return
	}

	client, err := user.getClient(session.Target)
	if err != nil {
		res.OutputSpeech("The computer you requested isn't online.").EndSession(true)
		return
	}

	client.Connection.send <- []byte(session.Cmd + " " + session.Payload)
	res.OutputSpeech("Done!").EndSession(true)
}
Example #4
0
func EchoIntentHandler(echoReq *alexa.EchoRequest, echoResp *alexa.EchoResponse) {
	echoResp.OutputSpeech("Hello world from my new Echo test app!").Card("Hello World", "This is a test card.")
}
Example #5
0
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)
	}
}