Created
August 21, 2011 01:38
-
-
Save seven1240/1159964 to your computer and use it in GitHub Desktop.
erlang sample ivr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ivr). | |
-author("Seven Du"). | |
-export([start/0, start/1]). | |
-define(FS_NODE, 'freeswitch@localhost'). | |
-define(WELCOME_SOUND, "tone_stream://%(100,1000,800);loops=1"). | |
-define(INPUT_NUMBER_SOUND, "tone_stream://%(100,1000,800);loops=2"). | |
-define(LOG(Fmt, Args), io:format("~b " ++ Fmt, [?LINE | Args])). | |
start() -> | |
register(ivr, self()), | |
loop(). | |
start(Ref) -> | |
NewPid = spawn(fun() -> serve() end), | |
{Ref, NewPid}. | |
loop() -> | |
receive | |
{get_pid, UUID, Ref, FSPid} -> | |
NewPid = spawn(fun() -> serve() end), | |
FSPid ! {Ref, NewPid}, | |
?LOG("Main process ~p spawned new child process ~p~n", [self(), NewPid]), | |
loop(); | |
_X -> | |
loop() | |
end. | |
serve() -> | |
receive | |
{call, {event, [UUID | Event]} } -> | |
?LOG("New call ~p~n", [UUID]), | |
send_msg(UUID, set, "hangup_after_bridge=true"), | |
send_lock_msg(UUID, playback, ?WELCOME_SOUND), | |
send_msg(UUID, read, "1 4 " ?INPUT_NUMBER_SOUND " erl_dst_number 5000 #"), | |
serve(); | |
{call_event, {event, [UUID | Event]} } -> | |
Name = proplists:get_value(<<"Event-Name">>, Event), | |
App = proplists:get_value(<<"Application">>, Event), | |
?LOG("Event: ~p, App: ~p~n", [Name, App]), | |
case Name of | |
<<"CHANNEL_EXECUTE_COMPLETE">> when App =:= <<"read">> -> | |
send_msg(UUID, hangup, ""), | |
case proplists:get_value(<<"variable_erl_dst_number">>, Event) of | |
undefined -> | |
send_msg(UUID, read, "1 4 " ?INPUT_NUMBER_SOUND " erl_dst_number 5000 #"); | |
Dest -> | |
send_msg(UUID, bridge, "user/" ++ binary_to_list(Dest)) | |
end; | |
_ -> ok | |
end, | |
serve(); | |
call_hangup -> | |
?LOG("Call hangup~n", []); | |
_X -> | |
?LOG("ignoring message ~p~n", [_X]), | |
serve() | |
end. | |
send_msg(UUID, App, Args) -> | |
Headers = [{"call-command", "execute"}, | |
{"execute-app-name", atom_to_list(App)}, {"execute-app-arg", Args}], | |
send_msg(UUID, Headers). | |
send_lock_msg(UUID, App, Args) -> | |
Headers = [{"call-command", "execute"}, {"event-lock", "true"}, | |
{"execute-app-name", atom_to_list(App)}, {"execute-app-arg", Args}], | |
send_msg(UUID, Headers). | |
send_msg(UUID, Headers) -> {sendmsg, ?FS_NODE} ! {sendmsg, UUID, Headers}. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment