ARI: Wait for DTMF

I’m using a C#/NET library to implement my ARI conference app.

I’m implementing authentication prior to adding a conference user into a conference.
If a callerId is not found in DB, then I need to ask for the PIN number.
I can’t seem to find how to fire the event only when I need to.

Eg:

if (!addUser)
{
       PlayFile("enter-conf-pin-number", c.Id);
       PlayFile("sound:then-press-pound", c.Id);
       // ask for pin 
       askForPin = true; 
}

The PIN construct gets called inside the OnChannelDtmfReceivedEvent method:

private void c_OnChannelDtmfReceivedEvent(object sender, ChannelDtmfReceivedEvent e) //here
{
      ConferenceUser confUser = ConferenceUsers.SingleOrDefault(x => x.Channel.Id == e.Channel.Id);
      if (confUser == null) return;

       if (askForPin) {
           confUser.KeyPressPinNumber(e.Digit);
    }          
}

Here’s my KeyPressPinNumber construct;

public void KeyPressPinNumber(string digit)
        {
            // User pressed a key
            switch (digit)
            {
                case "1":
                    PinNumber += "1";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "2":
                    PinNumber += "2";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "3":
                    PinNumber += "3";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "4":
                    PinNumber += "4";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "5":
                    PinNumber += "5";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "6":
                    PinNumber += "6";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "7":
                    PinNumber += "7";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    // ?
                    break;
                case "8":
                    PinNumber += "8";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    // ?
                    break;
                case "9":
                    PinNumber += "9";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    // ?
                    break;
                case "0":
                    PinNumber += "0";
                    _client.Channels.Play(Channel.Id, "sound:beep", "en", 0, 0, Guid.NewGuid().ToString());
                    break;
                case "#":

                    if (PinNumber.Length > 3) {

                        //check PIN number against set BD conferences PIN
                        IEnumerable<Conf.Models.Conference> conf = conferenceRepository.GetConferenceByPinNumber((int.Parse(PinNumber)));

                        //if PIN Number exists
                        if(conf.Count() > 0)
                        {
                            //add caller directly into conference
                            //play sound: Entering confernce number..."
                            _client.Channels.Play(Channel.Id, "sound:pin-number-accepted", "en", 0, 0, Guid.NewGuid().ToString());
                           // _client.Channels.Play(Channel.Id, "sound:entering-conf-number", "en", 0, 0, Guid.NewGuid().ToString());

                        }
                        //if Pin Number does NOT exust
                        else
                        {
                            //play sound: "That is not a valid conference number. Please try again."
                            _client.Channels.Play(Channel.Id, "sound:conf-invalid", "en", 0, 0, Guid.NewGuid().ToString());
                            //reset Pin NUmber
                            PinNumber = "";
                            break;
                        }
                    }
                    else
                    {
                        //play sound: "That is not a valid conference number. Please try again."
                        _client.Channels.Play(Channel.Id, "sound:cconf-invalid", "en", 0, 0, Guid.NewGuid().ToString());
                        //reset Pin NUmber
                        PinNumber = "";
                        break;
                    }

                    break;
            }
        }

But this does not seem to work. How can I do this?

You’re going to need to be specific about what language and toolkit you’re using. There’s quite a few.

I’ve accidentally posted before I was done writing. Please see now.

Hi Dan,

Are you using AsterNET.ARI? (it looks like it from the code sample you’ve shown).

You need to store each DTMF in a string, then when that string is of the correct length, accept it as the PIN (or use a terminator like ‘#’).

e.g.

if(askForPin && digitsSoFar.length < 4){

}else{
// you have your full PIN now!
}

Yes AsterNet.Ari. I’ve added my PIN number construct. So your saying to check the boolean “askFoPin” inside the KeyPressPinNumber function on the pound (#) key press? and pass askForPin as a parameter?

It’s totally up to you, this is more a programming question than anything to do with either ARI or Asterisk.

Basically, you’re only going to be sent each digit when DTMF event is fired, so you need to build up a string to handle that input from ARI. You have to decide when the user has completed entering the PIN number!

The best way to do that is build up a string and then check to see if the string is the right length, or maybe check to see if they press a ‘#’, in which case you know they’ve finished.

You can get help relating to AsterNET.ARI here: https://github.com/skrusty/AsterNET.ARI

Again, your question is less about Asterisk/ARI and more about programming concepts.

Ben