Cristian Livadaru's blog

... think again ...

Getting Started With Adhearsion

If you want to give adhearsion a try, here is a quick introduction in how to set it up and do your first tests.
This little howto was made on a Mac but it should be usable for linux without any problems, except for the Asterisk paths.
So, let’s get started. First we need asterisk for Mac OS, we can get this here: after installing asterisk, let’s get the adhearsion gem

1
2
3
$ sudo gem install adhearsion
Successfully installed adhearsion-0.8.4
1 gem installed

after adhearsion was installed, you should install some softphone for testing. I recommend using blink on Mac OS.

Now let’s create a first project to play around.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ ahn create test
      create  
      create  components/simon_game
      create  components/disabled/stomp_gateway
      create  components/disabled/sandbox
      create  components/ami_remote
      create  components/disabled/restful_rpc/spec
      create  config
      create  .ahnrc
      create  components/simon_game/simon_game.rb
      create  components/ami_remote/ami_remote.rb
      create  components/disabled/stomp_gateway/stomp_gateway.rb
      create  components/disabled/stomp_gateway/stomp_gateway.yml
      create  components/disabled/stomp_gateway/README.markdown
      create  components/disabled/restful_rpc/restful_rpc.rb
      create  components/disabled/restful_rpc/restful_rpc.yml
      create  components/disabled/restful_rpc/README.markdown
      create  components/disabled/restful_rpc/example-client.rb
      create  components/disabled/restful_rpc/spec/restful_rpc_spec.rb
      create  components/disabled/sandbox/sandbox.yml
      create  components/disabled/sandbox/sandbox.rb
      create  config/startup.rb
      create  dialplan.rb
      create  events.rb
      create  README
      create  Rakefile

Now we need to set up asterisk extensions.conf for our first adhearsion project. So let’s open /Library/Asterisk/conf/extensions.conf and search for the [default] section and add exten => _X.,1,AGI(agi://127.0.0.1) right at the beginning.
It should now look like this:

1
2
3
4
5
6
[default]
;
; By default we include the demo.  In a production system, you
; probably don't want to have the demo there.
;
exten => _X.,1,AGI(agi://127.0.0.1)

Why the default? For the purpose of testing, this is the fastest way. You can of course use something different, but keep in mind that this MUST match what you will have in your adhearsion dialplan.rb ! But more on that later.

Now open dialplan.rb from your freshly created adhearsion project in your favorite editor.
Remove the lines from the example

1
2
3
adhearsion {
  simon_game
}

And replace them with our first example:

1
2
3
default {
  ahn_log " Extenstion #{extension} was dialed"
}

Note that the default here must match the section of your extensions.conf, so if you used [my_cool_ivr] then your dialplan.rb must look like this:

1
2
3
my_cool_ivr {
  ahn_log " Extenstion #{extension} was dialed"
}

So now start asterisk

$ asterisk -cvvvvv

Start your ahn project

1
2
3
$ ahn start test
 INFO ahn: Adhearsion initialized!
[Sat Jul 24 20:33:55 2010] Adhearsion::VoIP::Asterisk::AGI::Server::RubyServer 0.0.0.0:4573 start

And start Blink and dial 100@127.0.0.1, nothing exciting will happen, but if you check your session where you started adhearsion you will see

1
2
[Sat Jul 24 20:34:45 2010] Adhearsion::VoIP::Asterisk::AGI::Server::RubyServer 0.0.0.0:4573 client:64178 localhost connect
 INFO ahn:  Extenstion 100 was dialed

So with very little effort you now have connected Asterisk to ruby, pretty cool. Well, let’s do something more exciting now.
Open up dialplan.rb and add some code in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
default {
  ahn_log "Extenstion #{extension} was dialed"
  case extension
    when 100
      play "agent-pass"
      login=input 4, :timeout => 1.minute
      ahn_log "got login #{login}"
      if login=="1234"
        play "agent-loginok"
      else
        play "agent-incorrect"
      end
    else
      play "ss-noservice"
  end
}

So if you dial 100@127.0.0.1 you will be asked to enter a pin, with the line login=input 4, :timeout => 1.minute you are telling asterisk that you want 4 digits, and there should be a timeout if nothing happens in one minute.

I did intend to show off some conference features … but it turns out the binary build of asterisk for mac os is missing app_meetme so until I figure that one out I’ll just leave this as it is.