Top

Asterisk 1.4: Numbers Guessing Game

December 3, 2006

Asterisk Numbers Guessing Game Howto

1. First, we have to grab the sound files

- guessit.tgz

2. Now, we copy these to our asterisk sounds directory

# mv guessit.tgz /var/lib/asterisk/sounds/

3. Now we must untar the sound files into the guessit directory

# cd /var/lib/asterisk/sounds# tar xzvf guessit_sounds.tgz

4. You should have files like the following

#cd guessit

# ls
goodbye.gsm input-number.gsm input_number.gsm intro.gsm less.gsm more.gsm playagain.gsm suityourself.gsm thatsnotanumber.gsm win.gsm

5. Now we have to add the game extension to asterisk's extensions.conf, or other files if you are using includes. Here it is in one simple block.

; go to the game extension
exten => 8006,1,Wait(1)
exten => 8006,n,Goto(guessgame,s,1)

[timehang]
; if they take too long say it's invalid

exten => t,1,Playback(goodbye)
exten => t,2,Hangup
exten => i,1,Playback(invalid)

[guessgame]
include => timehang
exten => s,1,Playback(guessit/intro)
exten => s,2,Set(GUESS="")
exten => s,3,Set(GUESS=$[${EPOCH} % 9])
exten => s,4,Read(NUMBER,guessit/input_number,1)
exten => s,5,Set(TIMEOUT(digit)=3)
exten => s,6,Set(TIMEOUT(response)=5)
exten => s,7,GotoIf($["${NUMBER}" = "${GUESS}"]?19:8)
exten => s,8,GotoIf($["${NUMBER}" > "${GUESS}"]?13:9)
exten => s,9,GotoIf($["${NUMBER}" < "${GUESS}"]?15:10)
exten => s,10,GotoIf($["${NUMBER}" = "*"]?17:17)
exten => s,11,GotoIf($["${NUMBER}" = "#"]?17:17)
exten => s,12,Goto(s,4)
exten => s,13,PlayBack(guessit/less)
exten => s,14,Goto(s,4)
exten => s,15,PlayBack(guessit/more)
exten => s,16,Goto(s,4)
exten => s,17,PlayBack(guessit/thatsnotanumber)
exten => s,18,Goto(s,4)
exten => s,19,PlayBack(guessit/win)
exten => s,20,Wait(.5)
exten => s,21,Background(guessit/playagain)
exten => 1,1,Goto(guessgame,s,1)
exten => 2,1,Playback(goodbye)
exten => 2,2,Hangup

6. That's it. Reload Asterisk, and dial 8006 and you should be able to start the game!

7. Have fun!

Comments

4 Responses to “Asterisk 1.4: Numbers Guessing Game”

  1. Someone on December 4th, 2006 5:52 pm

    At step 6, no need to restart, a reload will do
    You know, this is not Windows ;-)

  2. Matt Gibson on December 4th, 2006 8:12 pm

    Hi Someone,

    Thanks for the note. I meant reload, not restart. The howto has been updated to reflect the proper wording :)

    Good catch!

    Thanks,
    voipphreak

  3. Steve Murphy on December 5th, 2006 3:57 pm

    Hello–

    I suppose this is grossly obsessive-compulsive, but I took a few minutes and translated the above extensions.conf into AEL.

    —- cut here ———file=guessit.ael——————————
    macro guessgame()
    {
    while (1)
    {
    Playback(guessit/intro);
    set(GUESS=”");
    GUESS=${EPOCH}%9;
    Set(TIMEOUT(digit)=3);
    Set(TIMEOUT(response)=5);
    while (1)
    {
    Read(NUMBER,guessit/input_number,1);
    Verbose(Got ${NUMBER} from Read);
    if( “${NUMBER}” = “*” || “${NUMBER}” = “#” || “${NUMBER}” = “”)
    {
    Playback(guessit/thatsnotanumber);
    }
    else if (“${NUMBER}” = “${GUESS}”)
    {
    Playback(guessit/win);
    break; // the only way out of this loop!
    }
    else if (“${NUMBER}” > “${GUESS}”)
    {
    Playback(guessit/less);
    }
    else if (“${NUMBER}” < "${GUESS}")
    {
    Playback(guessit/more);
    }
    else /* what other stuff can the user enter than a number, #, nothing, or * ? */
    {
    Playback(guessit/thatsnotanumber);
    }
    }
    /* You get here after a successful guess */
    Wait(.5);
    Read(AGAIN,guessit/playagain,1);
    if ("${AGAIN}" != "1")
    break;
    }
    Playback(guessit/goodbye);
    return;

    catch t
    {
    playback(guessit/goodbye);
    return;
    }
    catch i
    {
    playblack(invalid);
    }
    }
    ---------------------------- cut here ----------------------

    Put the above into the file, guessit.ael in /etc/asterisk.

    Then in your extensions.ael, outside a context, slip in this include:

    #include "guessit.ael"

    and in some context enter in the macro call from an appropriate extension:

    779 => {
    &guessgame();
    }

    In the above, dialing 779 will trigger the game.

    All these instructions apply to 1.4 and trunk versions of Asterisk. For those running 1.2, I suggest making a 1.2 patch, and installing AEL2. It most likely will not work on the original AEL code that comes with 1.2.

    I wrote the code as an example, it has no goto’s. It implements the game as nested loops. I will also post it on the voip-info asterisk wiki in the AEL examples page.

    There are some slight variations from the original code…

    murf

  4. flewid on December 5th, 2006 4:02 pm

    Awesome. Thanks!

Bottom