forked from bpuzon/eastrisk
/
partypants.erl
55 lines (48 loc) · 1.71 KB
/
partypants.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%%% (c) 2010 Erlang Solutions, Ltd
-module(partypants).
-export([start/2]).
start(ChannelPid, _ChannelEnv) ->
agi:answer(ChannelPid),
agi:stream_file(ChannelPid, "partypants", ""),
read_response(ChannelPid),
agi:say_number(ChannelPid, read_accept(ChannelPid), ""),
agi:stream_file(ChannelPid, "have-accepted", ""),
agi:say_number(ChannelPid, read_decline(ChannelPid), ""),
agi:stream_file(ChannelPid, "have-declined", ""),
agi:hangup(ChannelPid),
agi_channel:close(ChannelPid).
read_response(ChannelPid) ->
AcceptCount = read_accept(ChannelPid),
DeclineCount = read_decline(ChannelPid),
case agi:get_option(ChannelPid, "partyaccept", "12", 5000) of
{ok, {$1, _EndPoint}} ->
update_accept(ChannelPid, AcceptCount + 1),
agi:stream_file(ChannelPid, "you", ""),
agi:stream_file(ChannelPid, "have-accepted", "");
{ok, {$2, _EndPoint}} ->
update_decline(ChannelPid, DeclineCount + 1),
agi:stream_file(ChannelPid, "you", ""),
agi:stream_file(ChannelPid, "have-declined", "");
{ok, {0, _EndPoint}} ->
read_response(ChannelPid);
{error, _Error} ->
exit(normal)
end.
update_accept(ChannelPid, Value) ->
agi:database_put(ChannelPid, "partypants", "accept", integer_to_list(Value)).
update_decline(ChannelPid, Value) ->
agi:database_put(ChannelPid, "partypants", "decline", integer_to_list(Value)).
read_accept(ChannelPid) ->
case agi:database_get(ChannelPid, "partypants", "accept") of
{ok, {1, Value}} -> list_to_integer(Value);
{ok, 0} -> 0;
{error, _Error} ->
exit(normal)
end.
read_decline(ChannelPid) ->
case agi:database_get(ChannelPid, "partypants", "decline") of
{ok, {1, Value}} -> list_to_integer(Value);
{ok, 0} -> 0;
{error, _Error} ->
exit(normal)
end.