Skip to content
Browse files

start of getting phone sups to work

  • Loading branch information...
1 parent c90e11d commit ac936ec5a8cffe3abb6349a291b5d987e1016313 @hypernumbers committed Apr 7, 2012
Showing with 119 additions and 20 deletions.
  1. +6 −5 src/inbound_phone_srv.erl
  2. +28 −9 src/inbound_phone_sup.erl
  3. +78 −5 src/twilio_ext.erl
  4. +7 −1 src/twilio_web_util.erl
View
11 src/inbound_phone_srv.erl
@@ -12,7 +12,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -33,9 +33,9 @@
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
-start_link() ->
- io:format("Starting inbound_phone_srv"),
- gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
+start_link(Params) ->
+ io:format("Starting inbound_phone_srv for ~p~n", [Params]),
+ gen_server:start_link(?MODULE, [Params], []).
%%%===================================================================
%%% gen_server callbacks
@@ -52,7 +52,8 @@ start_link() ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-init([]) ->
+init([_Params]) ->
+ io:format("In inbound_phone_srv:init~n"),
{ok, #state{}}.
%%--------------------------------------------------------------------
View
37 src/inbound_phone_sup.erl
@@ -10,6 +10,9 @@
-behaviour(supervisor).
+-include("twilio.hrl").
+-include("twilio_web.hrl").
+
%% API
-export([
start_link/0,
@@ -18,20 +21,35 @@
]).
%% Supervisor callbacks
--export([init/1]).
+-export([
+ init/1
+ ]).
-define(SERVER, ?MODULE).
%%%===================================================================
%%% API functions
%%%===================================================================
-spec answer_phone(string()) -> pid() | string().
-answer_phone(CallId) ->
- ChildSpec = gen_child_spec(CallId),
- case supervisor:start_child({local, CallId}, ChildSpec) of
- {ok, Pid} -> Pid;
- {error, {already_started, Pid}} -> Pid;
- Else -> Else
+answer_phone(Params) ->
+ ChildSpec = gen_child_spec(Params),
+ io:format("ChildSpec is ~p~n", [ChildSpec]),
+ case supervisor:start_child(?MODULE, ChildSpec) of
+ {ok, Pid} ->
+ N = random:uniform(1),
+ TwiML_EXT = twilio_ext:get_twiml_ext(N),
+ io:format("Starting state machine with ~p~n", [TwiML_EXT]),
+ io:format("call started...~n"),
+ Pid;
+ {error, {{already_started, Pid}, _}} ->
+ io:format("call already exists (1)~n"),
+ Pid;
+ {error, {already_started, Pid}} ->
+ io:format("call already exists (2)~n"),
+ Pid;
+ Else ->
+ io:format("Something else happend on startup ~p~n", [Else]),
+ Else
end.
%%--------------------------------------------------------------------
@@ -67,5 +85,6 @@ init([]) -> {ok,{{one_for_one,1,30}, []}}.
%%% Internal functions
%%%===================================================================
gen_child_spec(S) ->
- {S, {inbound_phone_srv, start_link, [S]},
- transient, 2000, worker, dynamic}.
+ #twilio{call_sid = CallSID} = S,
+ {CallSID, {inbound_phone_srv, start_link, [S]},
+ permanent, brutal_kill, worker, [inbound_phone_srv]}.
View
83 src/twilio_ext.erl
@@ -8,15 +8,25 @@
-module(twilio_ext).
-export([
- handle/1
- ]).
+ handle/1,
+ get_twiml_ext/1
+ ]).
+
+% debugging exports
+-export([
+ debug/0,
+ debug2/0,
+ log_terms/2
+ ]).
-include("twilio.hrl").
+-include("twilio_web.hrl").
handle(Params) ->
- log_terms(Params, "terms.log"),
- Say = #say{text="yowza"},
- twiml:encode([Say]).
+ inbound_phone_sup:answer_phone(Params),
+ ok.
+
+get_twiml_ext(1) -> [#say{text="yowza"}].
log_terms(Terms, File) ->
Str = lists:flatten(io_lib:format("~p.~n", [Terms])),
@@ -28,3 +38,66 @@ log_terms(Terms, File) ->
_ ->
error
end.
+
+debug() ->
+
+ Params = [{"AccountSid","AC7a076e30da6d49119b335d3a6de43844"},
+ {"ToZip",[]},
+ {"FromState",[]},
+ {"Called","+441315101897"},
+ {"FromCountry","GB"},
+ {"CallerCountry","GB"},
+ {"CalledZip",[]},
+ {"Direction","inbound"},
+ {"FromCity",[]},
+ {"CalledCountry","GB"},
+ {"CallerState",[]},
+ {"CallSid","CAb78d22107b73765976c1d8b3eafcb4d8"},
+ {"CalledState","Edinburgh"},
+ {"From","+447776251669"},
+ {"CallerZip",[]},
+ {"FromZip",[]},
+ {"CallStatus","ringing"},
+ {"ToCity",[]},
+ {"ToState","Edinburgh"},
+ {"To","+441315101897"},
+ {"ToCountry","GB"},
+ {"CallerCity",[]},
+ {"ApiVersion","2010-04-01"},
+ {"Caller","+447776251669"},
+ {"CalledCity",[]}],
+ Records = twilio_web_util:process_proplist(Params),
+ handle(Records).
+
+debug2() ->
+
+ {A, B, C} = now(),
+ CallSID = integer_to_list(A) ++ integer_to_list(B) ++ integer_to_list(C),
+ Params = [{"AccountSid","AC7a076e30da6d49119b335d3a6de43844"},
+ {"ToZip",[]},
+ {"FromState",[]},
+ {"Called","+441315101897"},
+ {"FromCountry","GB"},
+ {"CallerCountry","GB"},
+ {"CalledZip",[]},
+ {"Direction","inbound"},
+ {"FromCity",[]},
+ {"CalledCountry","GB"},
+ {"CallerState",[]},
+ {"CallSid", CallSID},
+ {"CalledState","Edinburgh"},
+ {"From","+447776251669"},
+ {"CallerZip",[]},
+ {"FromZip",[]},
+ {"CallStatus","ringing"},
+ {"ToCity",[]},
+ {"ToState","Edinburgh"},
+ {"To","+441315101897"},
+ {"ToCountry","GB"},
+ {"CallerCity",[]},
+ {"ApiVersion","2010-04-01"},
+ {"Caller","+447776251669"},
+ {"CalledCity",[]}],
+ Records = twilio_web_util:process_proplist(Params),
+ handle(Records),
+ supervisor:which_children(inbound_phone_sup).
View
8 src/twilio_web_util.erl
@@ -9,6 +9,7 @@
-export([
process_body/1,
+ process_proplist/1,
country_code_to_prefix/1,
country_code_to_country/1,
prefix_to_country/1,
@@ -50,12 +51,15 @@ prefix_to_country_code(Prefix) ->
{_Country, CC, Prefix} = lists:keyfind(Prefix, 3, ?CCLOOKUP),
CC.
-process_body(Binary) ->
+process_body(Binary) when is_binary(Binary) ->
List = binary_to_list(Binary),
Elements = string:tokens(List, "&"),
PropList = process(Elements, []),
make_record(PropList).
+process_proplist(Proplist) ->
+ make_record(Proplist).
+
make_record(PropList) ->
make_r(PropList, #twilio{}, #twilio_called{}, #twilio_caller{},
#twilio_from{}, #twilio_to{}, #twilio_duration{}, #twilio_inprogress{}).
@@ -156,6 +160,8 @@ fix_up({Rec, Number, City, Zip, State, [], CC, []}) ->
{Rec, fix_number(Number, NewPrefix), City, Zip, State,
Country, CC, "+" ++ NewPrefix}.
+fix_number(" " ++ Number, CC) ->
+ "0" ++ re:replace(Number, "^" ++ CC, "", [{return, list}]);
fix_number("+" ++ Number, CC) ->
"0" ++ re:replace(Number, "^" ++ CC, "", [{return, list}]).

0 comments on commit ac936ec

Please sign in to comment.
Something went wrong with that request. Please try again.