reasonableframework/helper/twilio.api.php

98 lines
3.0 KiB
PHP
Raw Normal View History

2019-04-08 06:57:38 +00:00
<?php
/**
* @file twilio.api.php
* @date 2019-04-08
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Twilio REST API interface module
* @documentation https://www.twilio.com/docs/sms/send-messages
*/
2019-04-15 06:18:52 +00:00
if(!is_fn("twilio_get_config")) {
2019-05-20 08:19:05 +00:00
function twilio_get_config() {
$config = get_config();
2019-04-16 07:16:11 +00:00
2019-05-20 08:19:05 +00:00
return array(
"sid" => get_value_in_array("twilio_sid", $config, ""),
"token" => get_value_in_array("twilio_token", $config, ""),
"from" => get_value_in_array("twilio_from", $config, ""),
2019-09-11 02:59:53 +00:00
"char_limit" => get_value_in_array("twilio_char_limit", $config, 160)
2019-05-20 08:19:05 +00:00
);
}
2019-04-15 06:18:52 +00:00
}
if(!is_fn("twilio_get_message_blocks")) {
2019-09-11 02:59:53 +00:00
function twilio_parse_messages($message) {
$strings = array();
$cnf = twilio_get_config();
if(loadHelper("string.utils")) {
$strings = get_splitted_strings($message, $cnf['char_limit']);
} else {
$strings[] = substr($messages, 0, $cnf['char_limit']);
}
return $strings;
}
}
if(!is_fn("twilio_send_message")) {
2019-05-20 08:19:05 +00:00
function twilio_send_message($message, $to) {
$response = false;
2019-04-10 11:28:40 +00:00
2019-05-20 08:19:05 +00:00
$cnf = twilio_get_config();
2019-09-11 02:59:53 +00:00
$messages = twilio_parse_messages($message);
2019-04-15 07:05:25 +00:00
2019-05-20 08:19:05 +00:00
if(loadHelper("webpagetool")) {
2019-10-25 01:55:08 +00:00
$bind = array(
"sid" => $cnf['sid']
);
$request_url = get_web_binded_url("https://api.twilio.com/2010-04-01/Accounts/:sid/Messages.json", $bind);
2019-09-11 02:59:53 +00:00
foreach($messages as $_message) {
$response = get_web_json($request_url, "post.cmd", array(
"headers" => array(
"Content-Type" => "application/x-www-form-urlencoded",
"Authentication" => array("Basic", $cnf['sid'], $cnf['token']),
),
"data" => array(
"Body" => $_message,
"From" => $cnf['from'],
"To" => $to,
)
));
}
2019-05-20 08:19:05 +00:00
}
2019-04-16 07:16:11 +00:00
2019-05-20 08:19:05 +00:00
return $response;
}
2019-04-10 11:28:40 +00:00
}
if(!is_fn("twilio_send_voice")) {
2019-05-20 08:19:05 +00:00
function twilio_send_voice($message="", $to) {
$response = false;
2019-04-15 07:05:25 +00:00
2019-05-20 08:19:05 +00:00
$cnf = twilio_get_config();
2019-09-09 07:21:57 +00:00
$url = "http://catswords.re.kr/ep/storage/data/voice.xml";
2019-04-10 11:28:40 +00:00
2019-05-20 08:19:05 +00:00
if(loadHelper("webpagetool")) {
2019-10-25 01:55:08 +00:00
$bind = array(
"sid" => $cnf['sid']
);
$request_url = sprintf("https://api.twilio.com/2010-04-01/Accounts/:sid/Calls.json", $bind);
2019-05-20 08:19:05 +00:00
$response = get_web_page($request_url, "post.cmd", array(
"headers" => array(
"Content-Type" => "application/x-www-form-urlencoded",
"Authentication" => array("Basic", $cnf['sid'], $cnf['token']),
),
"data" => array(
"Url" => $url,
"From" => $cnf['from'],
"To" => $to,
),
));
}
2019-04-16 07:16:11 +00:00
2019-05-20 08:19:05 +00:00
return $response;
}
2019-04-10 11:28:40 +00:00
}