Skip to content

Instantly share code, notes, and snippets.

@waj
Created June 9, 2012 15:51
Show Gist options
  • Save waj/2901542 to your computer and use it in GitHub Desktop.
Save waj/2901542 to your computer and use it in GitHub Desktop.
Spiking with Erlang and Asterisk
-module(main).
-behaviour(gen_event).
-export([
start/0,
init/1,
code_change/3,
handle_call/2,
handle_event/2,
handle_info/2,
terminate/2
]).
-export([agi_answer/2]).
-include("agi.hrl").
start() ->
% application:start(sasl),
application:start(eastrisk),
agi_events:add_agi_handler(?MODULE, nil).
init(_) ->
{ok, nil}.
handle_event({new_channel, Pid, Env}, State) ->
spawn(?MODULE, agi_answer, [Pid, Env]),
{ok, State};
handle_event(_Event, State) ->
{ok, State}.
code_change(_, _, _) ->
{error}.
handle_call(_, _) ->
{error}.
handle_info(_, _) ->
{error}.
terminate(_, _) ->
{error}.
agi_answer(Pid, _Env) ->
agi:answer(Pid),
loop_on_digits(Pid),
agi:hangup(Pid),
agi_channel:close(Pid),
ok.
loop_on_digits(Pid) ->
Digit = agi:wait_for_digit(Pid, 3000),
io:format("Digit: ~p~n", [Digit]),
case Digit of
{ok, 52} -> ok;
{ok, 0} -> ok;
{ok, _} ->
loop_on_digits(Pid)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment