Exemple #1
0
func (node MenuNode) Execute(ivrChannel *IVRChannel) (string, error) {

	if ivrChannel.ChannelState == IVRChannel_State_Hangup {
		return "", errors.New("channel state is invalid : hangup")
	}

	ivrChannel.ActiveNode = node.NodeName
	// Clear dtmf channel value.
	for len(ivrChannel.Dtmf) > 0 {
		<-ivrChannel.Dtmf
	}

	executePrompt(node.Prompts.Prompt, ivrChannel)

	/*
		if len(node.Prompts.Prompt) > 0 {
			for _, promptName := range node.Prompts.Prompt {
				// Find prompt from ivrPromptMap by promptName
				if prompt, ok := ivrPromptMap[promptName]; ok {
					executePrompt(prompt, ivrChannel)
					<-ivrChannel.PlaybackDone
				} else {
					l4g.Warn("Prompt not find for promptName=%s", promptName)
				}
			}
		}
	*/

	ivrChannel.Esocket.StartDTMF()
	defer ivrChannel.Esocket.StopDTMF()

	// Wait dtmf input.
	timeout := eventsocket.CheckTimeout(node.Timeout)
	select {
	case <-timeout:
		l4g.Warn("Timeout,no dtmf.")
		ivrChannel.NoInputTimes = ivrChannel.NoInputTimes + 1
		return node.NoInput, nil
	case dtmf := <-ivrChannel.Dtmf:

		for _, choice := range node.Choices.Choice {
			if dtmf == choice.DTMF {
				return choice.NextNode, nil
			}
		}

		l4g.Warn("No match for dtmf=%s", dtmf)
		ivrChannel.NoMatchTimes = ivrChannel.NoMatchTimes + 1
		return node.NoMatch, nil
	case <-ivrChannel.ChannelHangup:
		l4g.Trace("Channel hangup.")
		return "", errors.New("Channel hangup.")
	}

}
Exemple #2
0
func (node PromptCollectNode) Execute(ivrChannel *IVRChannel) (string, error) {

	if ivrChannel.ChannelState == IVRChannel_State_Hangup {
		return "", errors.New("channel state is invalid : hangup")
	}

	ivrChannel.ActiveNode = node.NodeName
	// Clear dtmf channel value.
	for len(ivrChannel.Dtmf) > 0 {
		<-ivrChannel.Dtmf
	}

	executePrompt(node.Prompts.Prompt, ivrChannel)

	/*
		if len(node.Prompts.Prompt) > 0 {
			for _, promptName := range node.Prompts.Prompt {
				// Find prompt from ivrPromptMap by promptName
				if prompt, ok := ivrPromptMap[promptName]; ok {
					executePrompt(prompt, ivrChannel)
					<-ivrChannel.PlaybackDone
				} else {
					l4g.Warn("Prompt not find for promptName=%s", promptName)
				}
			}
		}
	*/

	ivrChannel.Esocket.StartDTMF()
	defer ivrChannel.Esocket.StopDTMF()

	// Wait for dtmf input.
	if grammar, ok := ivrGrammarMap[node.Grammars.Grammar[0]]; ok {

		dtmfValue := ""
		maxDtmfLen := grammar.MaxLen

		done := false

		for !done {
			timeout := eventsocket.CheckTimeout(grammar.Timeout)
			select {
			case <-timeout:
				l4g.Warn("Timeout,no dtmf.")
				done = true
			case dtmf := <-ivrChannel.Dtmf:
				if dtmf == grammar.Terminator {
					done = true
				} else {
					dtmfValue = dtmfValue + dtmf
					if len(dtmfValue) >= maxDtmfLen {
						done = true
					}
				}
			case <-ivrChannel.ChannelHangup:
				l4g.Trace("Channel hangup.")
				return "", errors.New("Channel hangup.")
			}
		}

		l4g.Debug("Now dtmf vlaue=%s", dtmfValue)
		if len(dtmfValue) == 0 {
			// Timeout Noinput error.
			ivrChannel.NoInputTimes = ivrChannel.NoInputTimes + 1
			return node.NoInput, nil
		} else {
			dtmfRex := regexp.MustCompile(grammar.Express)
			if dtmfRex.MatchString(dtmfValue) {
				ivrChannel.DtmfValue = dtmfValue
				l4g.Trace("Collect dtmfValue=%s,nextNode=%s", ivrChannel.DtmfValue, node.NextNode)
				return node.NextNode, nil
			} else {
				// No match. NoMath error.
				ivrChannel.NoMatchTimes = ivrChannel.NoMatchTimes + 1
				return node.NoMatch, nil
			}
		}

	} else {
		l4g.Warn("Grammar not find for %s at node %s", node.Grammars.Grammar[0], node.NodeName)
		return "", errors.New("Grammar not find")
	}

}