Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Module
- -module(open_rcc_server).
- %Behaviour
- -behaviour(gen_server).
- %Start Function
- -export([start_link/1]).
- %Gen_Server API
- -export([
- init/1,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- terminate/2,
- code_change/3
- ]).
- %% mochi
- -export([start/1,
- stop/0]).
- %OpenACD
- -include_lib("OpenACD/include/log.hrl").
- -include_lib("OpenACD/include/call.hrl").
- -include_lib("OpenACD/include/agent.hrl").
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Gen_Server Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- -record(state, {} ). %Empty for now.
- start_link(Port) ->
- {foo, '[email protected]'} ! {api, log, "info [open_rcc_server:start_link/0] Starting open_rcc_server..."},
- gen_server:start_link({local, open_rcc_server}, open_rcc_server, [Port], []).
- init([Port]) ->
- start(Port),
- NewState = #state{},
- {ok, NewState}.
- handle_call(_Msg, _From, State) ->
- {reply, ok, State}.
- handle_cast(_MSG, State) ->
- {noreply, State}.
- handle_info(_Msg, State) ->
- NewState = State,
- {noreply, NewState}.
- terminate(_,_) ->
- ok.
- code_change(_, _, State) ->
- {ok, State}.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mochi-Web Stuff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- start(Port) ->
- mochiweb_http:start([{port, Port},
- {loop, fun(Req) ->
- Path = Req:get(path),
- Resource = case string:str(Path, "?") of
- 0 -> Path;
- N -> string:substr(Path, 1, length(Path) - (N + 1))
- end,
- handle_request(Resource, Req)
- end}]).
- stop() ->
- mochiweb_http:stop().
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% REST API %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- handle_request("/agents", Req) ->
- Data = cpx:get_agents(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/clients", Req) ->
- Data = call_queue_config:get_clients(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/queues", Req) ->
- Data = call_queue_config:get_queues(),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/ringtest/" ++ Agent, Req) ->
- AgentPid = cpx:get_agent(Agent),
- AgentRec = agent:dump_state(AgentPid),
- Callrec = #call{id = "unused", source = self(), callerid= {"Echo test", "0000000"}},
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p~p~p", [AgentPid, AgentRec, Callrec])))}),
- Data = freeswitch_media_manager:ring_agent_echo(AgentPid, AgentRec, Callrec, 60000),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- %Data = freeswitch_dialer:start(Node, Number, Exten, Skills, Client, Vars),
- %freeswitch_media_manager:make_outbound_call("DCF", cpx:get_agent("742"), agent:dump_state(cpx:get_agent("742")#agent.login)
- handle_request("/init_outbound/" ++ Rest, Req) ->
- Apid = cpx:get_agent(Rest),
- AgentRec = agent:dump_state(cpx:get_agent(Rest)), % TODO - avoid
- Reply = case AgentRec#agent.state of
- Agentstate when Agentstate =:= released; Agentstate =:= idle ->
- try list_to_existing_atom("freeswitch") of
- freeswitch ->
- case whereis(freeswitch_media_manager) of
- P when is_pid(P) ->
- case freeswitch_media_manager:make_outbound_call("DCF", Apid, AgentRec#agent.login) of
- {ok, Pid} ->
- Call = gen_media:get_call(Pid),
- agent:set_state(Apid, precall, Call),
- {200, [], mochijson2:encode({struct, [{success, true}]}), Call};
- {error, Reason} ->
- {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, list_to_binary(io_lib:format("Initializing outbound call failed (~p)", [Reason]))}, {<<"errcode">>, <<"UNKNOWN_ERROR">>}]})}
- end;
- _ ->
- {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"freeswitch is not available">>}, {<<"errcode">>, <<"MEDIA_NOEXISTS">>}]})}
- end;
- % TODO - more outbound types go here :)
- _ ->
- {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"Unknown call type">>}, {<<"errcode">>, <<"MEDIA_NOEXISTS">>}]})}
- catch
- _:_ ->
- {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"Unknown call type">>}, {<<"errcode">>, <<"MEDIA_NOEXISTS">>}]})}
- end;
- _ ->
- {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"Agent must be released or idle">>}, {<<"errcode">>, <<"INVALID_STATE_CHANGE">>}]})}
- end,
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Reply])))});
- handle_request("/dial" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- Agent = proplists:get_value("agent", QueryStringData, "undefined"),
- Number = proplists:get_value("number", QueryStringData, "undefined"),
- {true, Pid} = agent_manager:query_agent(Agent),
- ?NOTICE("1) ~p~p", [Agent, Number]),
- AgentRec = agent:dump_state(Pid),
- ?NOTICE("2) ~p", [AgentRec]),
- try
- Reply =
- case AgentRec#agent.state of
- precall ->
- #agent{statedata = Call} = AgentRec,
- ?NOTICE("3) ~p", [Call]),
- case Call#call.direction of
- outbound ->
- case gen_media:call(Call#call.source, {dial, Number}) of
- ok ->
- {reply, {200, [], mochijson2:encode({struct, [{success, true}]})}};
- {error, Error} ->
- ?NOTICE("Outbound call error ~p", [Error]),
- {reply, {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, list_to_binary(lists:flatten(io_lib:format("~p, Check your phone configuration", [Error])))}, {<<"errcode">>, <<"UNKNOWN_ERROR">>}]})}}
- end;
- _ ->
- {reply, {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"This is not an outbound call">>}, {<<"errcode">>, <<"INVALID_STATE_CHANGE">>}]})}}
- end;
- _ ->
- {reply, {200, [], mochijson2:encode({struct, [{success, false}, {<<"message">>, <<"Agent is not in pre-call">>}, {<<"errcode">>, <<"INVALID_STATE_CHANGE">>}]})}}
- end,
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Reply])))})
- catch
- What:Why ->
- cpxlog:error(now(), ?MODULE, ?LINE, self(), "//////////////////////////////////////////////////////////////////////////////\n
- //////////////////////////////////////////////////////////////////////////////\n
- What:~p\n
- Why:~p\n
- //////////////////////////////////////////////////////////////////////////////\n
- //////////////////////////////////////////////////////////////////////////////\n", [What, Why])
- end;
- handle_request("/spy" ++ _Rest, Req) ->
- QueryStringData = Req:parse_qs(),
- Spy = proplists:get_value("spy", QueryStringData, "undefined"),
- Agent = proplists:get_value("agent", QueryStringData, "undefined"),
- SpyPid = case agent_manager:query_agent(Spy) of
- {true, Spid} -> Spid;
- false -> self()
- end,
- Response = case agent_manager:query_agent(Agent) of
- {true, Apid} ->
- case agent_channel:get_media(Apid) of
- CallRec when is_record(CallRec, call) ->
- case gen_media:spy(CallRec#call.source, SpyPid, agent:dump_state(cpx:get_agent(Apid))) of
- ok ->
- {struct, [{success, true}]};
- invalid ->
- {struct, [{success, false}, {<<"message">>, <<"invalid action">>}, {<<"errcode">>, <<"MEDIA_ACTION_UNSUPPORTED">>}]};
- {error, Err} ->
- {struct, [{success, false}, {<<"message">>, list_to_binary(io_lib:format("Error: ~p", [Err]))}, {<<"errcode">>, <<"UNKNOWN_ERROR">>}]}
- end;
- _ ->
- {struct, [{success, false}, {<<"message">>, <<"agent not oncall">>}, {<<"errcode">>, <<"INVALID_STATE or NYI">>}]}
- end;
- false ->
- {struct, [{success, false}, {<<"message">>, <<"no such agent">>}, {<<"errcode">>, <<"AGENT_NOEXISTS">>}]}
- end,
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Response])))});
- handle_request("/hangup", Req) ->
- Data = agent_tcp_client:hangup(self()),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Data])))});
- handle_request("/transfer", Req) ->
- QueryStringData = Req:parse_qs(),
- FromAgent = proplists:get_value("from", QueryStringData, "undefined"),
- ToAgent = proplists:get_value("to", QueryStringData, "undefined"),
- {true, From} = agent_manager:query_agent(FromAgent),
- {true, To} = agent_manager:query_agent(ToAgent),
- Response = agent_channel:agent_transfer(From, To),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(lists:flatten(io_lib:format("~p", [Response])))});
- handle_request(Path, Req) ->
- Get = Req:parse_qs(),
- Post = Req:parse_post(),
- User_agent = Req:get_header_value("user-agent"),
- %erlang:display({get, Get}),
- %erlang:display({post, Post}),
- %erlang:display({user_agent, User_agent}),
- %erlang:display({path, Path}),
- Req:respond({200, [{"Content-Type", "text/html"}], list_to_binary(Path)}).
Advertisement
Add Comment
Please, Sign In to add comment