Call Us Today! 877.742.2583




Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Update outbound example (WIP)

...

Here's a simple example using outbound mode. First, add  

Using a registered process

Step 1. Add entry in dialplan to call Erlang node

Add an entry in your dialplan to redirect an inbound call to the erlang dialplan application. You can do so by creating the file freeswitch/conf/Erlang application, by creating a new XML file for example:

Code Block
languagexml
title<conf_dir>/dialplan/default/123456_erlang.xml

...

code <include>
linenumbers
true
<include>
  <extension name="to_erlang">
    <condition field="destination_number" expression="^123456$"> 
      <action application="erlang" data="myhandler mynode@myserver"/>
    </condition>
  </extension>
</include>

This will send calls for extension 123456 to destination number 123456 to a registered process called myhandler on the erlang node mynode@myserver. Replace myserver with your hostname, using either short or long form depending on your setting in the mod_erlang_event config file. Next, write some erlang code to handle the call. Save this (possibly remote) Erlang node mynode@myserver.

Tip

The example refers to a node that is set up using shortname, but one can change this in the  <conf_dir>/autoload_configs/erlang_event.conf.xml configuration file (see Configuration section above).

Step 2. Write some Erlang 

Next, write some Erlang code to handle the call, save it in the file myhandler.erl:

Code Block
languageerl
titlemyhandler.erl
linenumberstrue
-module(myhandler).

-export([start/0,run/0,launch/1]).

start()->
  %% start our handler process running
  Pid = spawn(?MODULE,run,[]),
  %% register it with the same name as the module - myhandler
  register(?MODULE,Pid).

run()->
  %% wait for messages from FreeSWITCH
  receive
    {call,Data}->
      %% a new call is starting, find the UUID
      %% _Rest is a list of all the channel variable in the form {"<name>","<value"}
      {event, [UUID | _Rest]} = Data,
      error_logger:info_msg("myhandler ~p: new call received, UUID is ~p~n",[self(), UUID]),
      run();
    {call_event,Data} ->
      %% we've got an event for a call we've been notified of already
      {event, [UUID | Rest]} = Data,
      %% find out the name of the event
      Name = proplists:get_value("Event-Name", Rest),
      error_logger:info_msg("myhandler ~p: UUID ~p, event ~p~n",[self(), UUID,Name]),
      run();
    {get_pid, UUID, Ref, Pid} ->
      %% request from FreeSWITCH for an outbound process to handle call at 'UUID'
      NewPid = spawn(?MODULE, run, []),
      error_logger:info_msg("myhandler ~p: request to spawn new handler process, returning ~p~n", [self(), NewPid]),
      Pid ! {Ref, NewPid},
      run()
  end.

launch(Ref) ->
  %% rpc call to a function to return a new outbound call pid
  NewPid = spawn(?MODULE, run, []),
  error_logger:info_msg("myhandler ~p: launch request, returning ~p~n", [self(), NewPid]),
  {Ref, NewPid}.

...

Step 3. Start an Erlang node

Run the above module as a distributed Erlang node:

Code Block
$ erlc myhandler.erl
$ erl -sname mynode -setcookie ClueCon
Erlang (BEAM) emulator version 5.5.5 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.5  (abort with ^G)
(mynode@myserver)1> myhandler:start().
true

Now dial extension 123456, and see what happens at the erlang Erlang console:

Code Block
 (mynode@myserver)2>
=INFO REPORT==== 23-Jan-2009::11:59:38 ===
myhandler: new call received, UUID is "4f77b818-e945-11dd-a442-9fe384e7e5a2"

=INFO REPORT==== 23-Jan-2009::11:59:39 ===
myhandler: UUID "4f77b818-e945-11dd-a442-9fe384e7e5a2", event "CHANNEL_PROGRESS"

=INFO REPORT==== 23-Jan-2009::11:59:39 ===
myhandler: UUID "4f77b818-e945-11dd-a442-9fe384e7e5a2", event "CHANNEL_PROGRESS_MEDIA"

=INFO REPORT==== 23-Jan-2009::11:59:39 ===
myhandler: UUID "4f77b818-e945-11dd-a442-9fe384e7e5a2", event "CHANNEL_PARK"

We've

...

received

...

all

...

the

...

events

...

up

...

to

...

CHANNEL_PARK

...

 (see Event List for more). Now hang up the call, since we didn't

...

add

...

any

...

code

...

to

...

answer

...

it

...

or

...

do

...

anything

...

more

...

interesting:

Code Block
=INFO REPORT==== 23-Jan-2009::11:59:43 ===
myhandler: UUID "4f77b818-e945-11dd-a442-9fe384e7e5a2", event "CHANNEL_HANGUP"

To You can answer the call, execute applications and , hang up, etc. just as you would in the dialplan, you can send messages to freeswitch an XML Dialplan. You can also send commands to FreeSWITCH, just as with inbound mode.

Using a dynamic process

If you wanted to send events to a dynamic process instead of a registered one, you can instead do:

Code Block
languagexml
title<conf_dir>/dialplan/default/123456_erlang.xml
<include>
  <!-- ... --> 
    <action application="erlang" data="myhandler:launch mynode@myserver"/>
  <!-- ... -->
</include>

This will make an RPC call to myhandler:launch with a single argument, a unique reference. The function is expected to return a tuple of the form {Ref, NewPid}. NewPid should be a spawned process or a newly launched gen_server or something, Ref is the original reference passed in.

 <action
Code Block
languagexml
title<conf_dir>/dialplan/default/123456_erlang.xml
<include>
  <!-- ... --> 
    <action application="erlang" data="myhandler:! mynode@server"/>
  <!-- ... -->
</include>

If the string after the : is a '!', then mod_erlang_event knows you want to send a new process request to the registered process 'myhandler' which will return a pid that all events for that call will be sent to. The message the registered process receives is of the form:

...