コード例 #1
0
ファイル: main.go プロジェクト: mikeflynn/echo-tunnel
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)
}
コード例 #2
0
ファイル: main.go プロジェクト: mikeflynn/echo-tunnel
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)
	}
}