Permalink
Browse files

WHISTLE-1441: convert eavesdrop req to originate req

  • Loading branch information...
James Aimonetti
James Aimonetti committed Dec 4, 2012
1 parent ec1e88c commit 609b9408578f5ebf29ed8b755bda6aa87bdba764
Showing with 116 additions and 1 deletion.
  1. +116 −1 whistle_apps/apps/spyvsspy/src/spyvsspy_handlers.erl
@@ -14,4 +14,119 @@
-include("spyvsspy.hrl").
handle_eavesdrop_req(JObj, _Props) ->
true = wapi_resource:eavesdrop_req_v(JObj).
true = wapi_resource:eavesdrop_req_v(JObj),
AcctId = wh_json:get_value(<<"Account-ID">>, JObj),
case get_endpoints(AcctId
,wh_json:get_value(<<"Endpoint-ID">>, JObj)
)
of
{ok, EPs} -> send_eavesdrop(JObj, EPs, AcctId);
{error, E} -> respond_error(JObj, E)
end.
get_endpoints(AcctId, EndpointId) ->
cf_endpoint:build(EndpointId, new_call(AcctId)).
new_call(AcctId) ->
whapps_call:from_json(wh_json:from_list(
[{<<"Account-ID">>, AcctId}
,{<<"Account-DB">>, wh_util:format_account_id(AcctId, encoded)}
])).
get_group_and_call_id(JObj) ->
case wh_json:get_value(<<"Eavesdrop-Group-ID">>, JObj) of
undefined -> {undefined, wh_json:get_value(<<"Eavesdrop-Call-ID">>, JObj)};
GroupId -> {GroupId, wh_json:get_value(<<"Eavesdrop-Call-ID">>, JObj, <<"all">>)}
end.
send_eavesdrop(JObj, EPs, AcctId) ->
{GroupId, CallId} = get_group_and_call_id(JObj),
CCVs = props:filter_undefined([{<<"Account-ID">>, AcctId}]),
Timeout = wh_json:get_integer_value(<<"Endpoint-Timeout">>, JObj, 20),
{CallerIdName, CallerIdNumber} = find_caller_id(JObj),
Prop = wh_json:set_values(
props:filter_undefined(
[{<<"Msg-ID">>, wh_util:rand_hex_binary(6)}
,{<<"Custom-Channel-Vars">>, wh_json:from_list(CCVs)}
,{<<"Timeout">>, Timeout}
,{<<"Endpoints">>, [wh_json:set_values([{<<"Endpoint-Timeout">>, Timeout}
,{<<"Outgoing-Caller-ID-Name">>, CallerIdName}
,{<<"Outgoing-Caller-ID-Number">>, CallerIdNumber}
]
,EP)
|| EP <- EPs
]}
,{<<"Export-Custom-Channel-Vars">>, [<<"Account-ID">>
,<<"Retain-CID">>
,<<"Authorizing-ID">>
,<<"Authorizing-Type">>
]}
,{<<"Account-ID">>, AcctId}
,{<<"Resource-Type">>, <<"originate">>}
,{<<"Application-Name">>, <<"eavesdrop">>}
,{<<"Eavesdrop-Call-ID">>, CallId}
,{<<"Eavesdrop-Group-ID">>, GroupId}
,{<<"Eavesdrop-Mode">>, wh_json:get_value(<<"Eavesdrop-Mode">>, JObj)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
]
)
,wh_json:delete_key(<<"Event-Name">>, JObj)),
lager:debug("sending eavesdrop request for ~s:~s", [CallerIdName, CallerIdNumber]),
case whapps_util:amqp_pool_request(Prop
,fun wapi_resource:publish_originate_req/1
,fun wapi_dialplan:originate_ready_v/1
,2000
) of
{ok, OrigJObj} ->
lager:debug("originate is ready to execute"),
send_originate_execute(OrigJObj, wh_json:get_value(<<"Server-ID">>, OrigJObj)),
respond_success(JObj, OrigJObj);
{error, E} ->
lager:debug("error originating: ~p", [E]),
respond_error(JObj, E)
end.
respond_success(JObj, OrigJObj) ->
ServerId = wh_json:get_value(<<"Server-ID">>, JObj),
EavesdropCallId = wh_json:get_value(<<"Call-ID">>, OrigJObj),
respond(ServerId, [{<<"Status">>, <<"started">>}
,{<<"Eavesdropper-Call-ID">>, EavesdropCallId}
,{<<"Msg-ID">>, wh_json:get_value(<<"Msg-ID">>, JObj)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
]).
respond_error(JObj, E) ->
ServerId = wh_json:get_value(<<"Server-ID">>, JObj),
respond(ServerId, [{<<"Status">>, <<"error">>}
,{<<"Error-Msg">>, E}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
]).
respond(ServerId, Props) ->
lager:debug("Respond to ~s: ~p", [ServerId, Props]),
wapi_resource:publish_eavesdrop_resp(ServerId, Props).
send_originate_execute(JObj, Q) ->
Prop = [{<<"Call-ID">>, wh_json:get_value(<<"Call-ID">>, JObj)}
,{<<"Msg-ID">>, wh_json:get_value(<<"Msg-ID">>, JObj)}
| wh_api:default_headers(Q, ?APP_NAME, ?APP_VERSION)
],
wapi_dialplan:publish_originate_execute(wh_json:get_value(<<"Server-ID">>, JObj), Prop).
find_caller_id(JObj) ->
find_caller_id(JObj, [{<<"Outgoing-Caller-ID-Name">>, <<"Outgoing-Caller-ID-Number">>}
,{<<"Caller-ID-Name">>, <<"Caller-ID-Number">>}
]).
find_caller_id(_JObj, []) ->
{<<"SpyVsSpy">>, <<"01010101">>};
find_caller_id(JObj, [{KName, KNum}|Ks]) ->
case wh_json:get_value(KName, JObj) of
undefined -> find_caller_id(JObj, Ks);
Name -> {Name, wh_json:get_value(KNum, JObj)}
end.

0 comments on commit 609b940

Please sign in to comment.