-
Notifications
You must be signed in to change notification settings - Fork 10
/
icharge.c
119 lines (91 loc) · 3.81 KB
/
icharge.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <esl.h>
#define ERROR_PROMPT "say:输入错误,请重新输入"
int check_account_password(const char *account, const char *password)
{
return (!strcmp(account, "1111")) && (!strcmp(password, "1111"));
}
void process_event(esl_handle_t *handle, esl_event_t *event)
{
const char *uuid = esl_event_get_header(event, "Caller-Unique-ID");
uuid = strdup(uuid);
if (!uuid) abort();
switch (event->event_id) {
case ESL_EVENT_CHANNEL_PARK:
{
const char *service;
service = esl_event_get_header(event, "variable_service");
esl_log(ESL_LOG_INFO, "Service: %s\n", service);
if (!service || (service && strcmp(service, "icharge"))) break;
esl_log(ESL_LOG_INFO, "New Call %s\n", uuid);
esl_execute(handle, "answer", NULL, uuid);
esl_execute(handle, "set", "tts_engine=tts_commandline", uuid);
esl_execute(handle, "set", "tts_voice=Ting-Ting", uuid);
esl_execute(handle, "speak", "您好,欢迎使用空中充值服务", uuid);
again:
esl_execute(handle, "set", "charge_state=WAIT_ACCOUNT", uuid);
esl_execute(handle, "play_and_get_digits",
"4 5 3 5000 # 'say:请输入您的账号,以井号结束' "
ERROR_PROMPT " charge_account ^\\d{4}$", uuid);
esl_execute(handle, "set", "charge_state=WAIT_PASSWORD", uuid);
esl_execute(handle, "play_and_get_digits",
"4 5 3 5000 # 'say:请输入您的密码,以井号结束' "
ERROR_PROMPT " charge_password ^\\d{4}$", uuid);
break;
}
case ESL_EVENT_CHANNEL_EXECUTE_COMPLETE:
{
const char *application;
const char *charge_state;
application = esl_event_get_header(event, "Application");
charge_state = esl_event_get_header(event, "variable_charge_state");
if (!strcmp(application, "play_and_get_digits") &&
!strcmp(charge_state, "WAIT_PASSWORD")) {
const char *account = esl_event_get_header(event, "variable_charge_account");
const char *password = esl_event_get_header(event, "variable_charge_password");
if (account && password && check_account_password(account, password)) {
esl_log(ESL_LOG_INFO, "Account: %s Balance: 100\n", account);
esl_execute(handle, "speak", "您的余额是100元", uuid);
esl_execute(handle, "speak", "再见", uuid);
esl_execute(handle, "hangup", NULL, uuid);
} else {
esl_execute(handle, "speak", "账号密码错误", uuid);
goto again;
}
}
break;
}
case ESL_EVENT_CHANNEL_HANGUP_COMPLETE:
esl_log(ESL_LOG_INFO, "Hangup %s\n", uuid);
break;
default:
break;
}
free((void *)uuid);
}
int main(void)
{
esl_handle_t handle = {{0}};
esl_status_t status;
const char *uuid;
esl_global_set_default_logger(ESL_LOG_LEVEL_INFO);
status = esl_connect(&handle, "127.0.0.1", 8022, NULL, "ClueCon");
if (status != ESL_SUCCESS) {
esl_log(ESL_LOG_INFO, "Connect Error: %d\n", status);
exit(1);
}
esl_log(ESL_LOG_INFO, "Connected to FreeSWITCH\n");
esl_events(&handle, ESL_EVENT_TYPE_PLAIN,
"CHANNEL_PARK CHANNEL_EXECUTE_COMPLETE CHANNEL_HANGUP_COMPLETE");
esl_log(ESL_LOG_INFO, "%s\n", handle.last_sr_reply);
handle.event_lock = 1;
while((status = esl_recv_event(&handle, 1, NULL)) == ESL_SUCCESS) {
if (handle.last_ievent) {
process_event(&handle, handle.last_ievent);
}
}
end:
esl_disconnect(&handle);
return 0;
}