reasonableframework/route/api.twilio.php

59 lines
1.5 KiB
PHP
Raw Normal View History

2019-04-15 01:53:19 +00:00
<?php
/**
2019-04-15 06:20:15 +00:00
* @file api.twilio.php
2019-04-15 01:53:19 +00:00
* @date 2019-04-15
* @author Go Namhyeon <gnh1201@gmail.com>
2019-10-07 09:55:51 +00:00
* @brief Twilio API controller (or domestic API)
*/
2019-04-15 01:53:41 +00:00
2019-10-07 09:55:51 +00:00
loadHelper("twilio.api"); // for voice, or international
loadHelper("lguplus.api"); // for domestic
loadHelper("string.utils");
2019-04-15 01:53:41 +00:00
2019-04-15 04:30:13 +00:00
$action = get_requested_value("action", array("_JSON", "_ALL"));
2019-04-16 08:09:43 +00:00
$message = get_requested_value("message", array("_JSON", "_ALL"));
2019-04-15 04:30:13 +00:00
$to = get_requested_value("to", array("_JSON", "_ALL"));
2019-04-15 01:58:16 +00:00
2019-10-07 09:55:51 +00:00
$country = get_requested_value("country", array("_JSON", "_ALL"));
$is_domestic = array_key_equals("lguplus_country", $config, $country);
if(!$is_domestic) {
$to = sprintf("+%s%s", $country, $to);
2019-10-07 10:09:24 +00:00
} else {
$to = sprintf("%s%s", (substr($to, 0, 1) == "0" ? "" : "0"), $to);
2019-10-07 09:55:51 +00:00
}
2019-04-16 08:09:43 +00:00
$response = false;
2019-10-07 10:01:11 +00:00
// temporary filter (example)
2019-10-07 09:55:51 +00:00
$terms = get_tokenized_text($message);
2019-10-07 10:10:03 +00:00
if(in_array("fuck", $terms) || in_array("bitch", $terms) || in_array("hell", $terms)) {
2019-10-07 10:01:11 +00:00
$action = "denied";
2019-10-07 09:55:51 +00:00
}
2019-04-15 01:58:16 +00:00
switch($action) {
2019-04-16 08:09:43 +00:00
case "text":
2019-10-07 09:55:51 +00:00
if(!$is_domestic) {
$response = twilio_send_message($message, $to);
} else {
$response = lguplus_send_message($message, $to);
}
2019-04-15 01:58:16 +00:00
break;
case "voice":
2019-04-16 08:09:43 +00:00
$response = twilio_send_voice($message, $to);
break;
2019-10-07 09:55:51 +00:00
2019-10-07 10:01:11 +00:00
case "denied":
$response = array("error" => "action is denied");
break;
2019-10-07 09:55:51 +00:00
default:
$response = array("error" => "action is required");
break;
2019-04-15 01:58:16 +00:00
}
2019-10-07 09:55:51 +00:00
write_common_log(sprintf("message: %s, to: %s", $message, $to), "api.twilio");
2019-04-15 01:58:16 +00:00
header("Content-Type: application/json");
echo json_encode($response);