Skip to content

Instantly share code, notes, and snippets.

@resir014
Last active August 20, 2018 09:48
Show Gist options
  • Save resir014/fb42e6936bf6c4f47c7b23060bd0f92f to your computer and use it in GitHub Desktop.
Save resir014/fb42e6936bf6c4f47c7b23060bd0f92f to your computer and use it in GitHub Desktop.
<?php
// Header AI ====================================================================================================================
error_reporting(1);
header("Content-type: text/html; charset=utf-8");
ini_set("max_execution_time", 30);
date_default_timezone_set("Asia/Jakarta");
$module = basename(__FILE__,".php");
define("_MODULE" , $module);
define("_AI_TOKEN" , "YmxjaGVlaW9paGZuamhsamxnMTU0Q29lVTJyMm5PVTA3OTg4OWxRZGJ3Nk5XbU1Z");
define("_ROOT", realpath(dirname(__FILE__)));
$err_log = "err_".$module.".log";
ini_set("error_log", $err_log);
ini_set("default_charset", "UTF-8");
ini_set("mbstring.language", "neutral");
if ( version_compare(PHP_VERSION, "5.6.0", "<") )
{
mb_internal_encoding("UTF-8");
ini_set("mbstring.internal_encoding", "UTF-8");
ini_set("mbstring.http_input", "auto");
ini_set("mbstring.http_output", "UTF-8");
ini_set("iconv.internal_encoding", "UTF-8");
ini_set("iconv.output_encoding", "UTF-8");
}
// Handle Input ====================================================================================================================
$in_data = file_get_contents("php://input");
$arr_in_data = json_decode($in_data, true);
// Parse Data ====================================================================================================================
$ref_id = ( isset($arr_in_data["ref_id"]) ) ? $arr_in_data["ref_id"] : "" ;
$eva_ref_id = ( isset($arr_in_data["eva_ref_id"]) ) ? $arr_in_data["eva_ref_id"] : "" ;
$sender_data = ( isset($arr_in_data["sender_data"]) ) ? $arr_in_data["sender_data"] : "" ;
$platform_id = ( isset($sender_data["platform_id"]) ) ? $sender_data["platform_id"] : "" ;
$platform_name = ( isset($sender_data["platform_name"]) ) ? $sender_data["platform_name"] : "" ;
$sender_id = ( isset($sender_data["sender_id"]) ) ? $sender_data["sender_id"] : "" ;
$sender_uid = ( isset($sender_data["sender_uid"]) ) ? $sender_data["sender_uid"] : "" ;
$sender_nick = ( isset($sender_data["sender_nick"]) ) ? $sender_data["sender_nick"] : "" ;
$sender_name = ( isset($sender_data["sender_name"]) ) ? $sender_data["sender_name"] : "" ;
$sender_chat_id = ( isset($sender_data["sender_chat_id"]) ) ? $sender_data["sender_chat_id"] : "" ;
$sender_room_type = ( isset($sender_data["sender_room_type"]) ) ? $sender_data["sender_room_type"] : "" ;
$sender_room_title = ( isset($sender_data["sender_room_title"]) ) ? $sender_data["sender_room_title"] : "" ;
$sender_is_eva_owner = ( isset($sender_data["sender_is_eva_owner"]) ) ? $sender_data["sender_is_eva_owner"] : false ;
$sender_eva_uid = ( isset($sender_data["sender_eva_uid"]) ) ? $sender_data["sender_eva_uid"] : "" ;
$message_data = ( isset($arr_in_data["message_data"]) ) ? $arr_in_data["message_data"] : "" ;
$msg_original = ( isset($message_data["msg_original"]) ) ? $message_data["msg_original"] : "" ;
$msg_type = ( isset($message_data["msg_type"]) ) ? $message_data["msg_type"] : "" ;
$msg_dtsent = ( isset($message_data["msg_dtsent"]) ) ? $message_data["msg_dtsent"] : "" ;
$msg_clear = ( isset($message_data["msg_clear"]) ) ? $message_data["msg_clear"] : "" ;
$msg_last_n = ( isset($message_data["msg_last_n"]) ) ? $message_data["msg_last_n"] : array() ;
$msg_type_data = ( isset($message_data["msg_type_data"]) ) ? $message_data["msg_type_data"] : false;
$msg_file_data = ( isset($message_data["msg_file_data"]) ) ? $message_data["msg_file_data"] : false ;
$arr_words_lc = $nlp_data = "";
$msg_breakdown = ( isset($message_data["msg_breakdown"]) ) ? $message_data["msg_breakdown"] : false ;
if( $msg_breakdown ) {
$keyword = ( isset($msg_breakdown["keyword"]) ) ? $msg_breakdown["keyword"] : "" ;
$msg_clear = ( isset($msg_breakdown["msg_clear"]) ) ? $msg_breakdown["msg_clear"] : "" ;
$msg_clear_no_keywords = ( isset($msg_breakdown["msg_clear_no_keywords"]) ) ? $msg_breakdown["msg_clear_no_keywords"] : "" ;
$nlp_data = ( isset($msg_breakdown["nlp_data"]) ) ? $msg_breakdown["nlp_data"] : false ;
if( $nlp_data ) {
$arr_words_lc = ( isset($nlp_data["arr_words_lc"]) ) ? $nlp_data["arr_words_lc"] : "" ;
}
}
$meta_data = ( isset($arr_in_data["meta_data"]) ) ? $arr_in_data["meta_data"] : "" ;
$eva_bot_src_id = ( isset($meta_data["eva_bot_src_id"]) ) ? $meta_data["eva_bot_src_id"] : "" ;
$eva_session_status = ( isset($meta_data["eva_session_status"]) ) ? $meta_data["eva_session_status"] : "" ;
$eva_session_id = ( isset($meta_data["eva_session_id"]) ) ? $meta_data["eva_session_id"] : "" ;
$eva_name = ( isset($meta_data["eva_name"]) ) ? $meta_data["eva_name"] : "" ;
$eva_nick = ( isset($meta_data["eva_nick"]) ) ? $meta_data["eva_nick"] : "" ;
$eva_language_code = ( isset($meta_data["eva_language_code"]) ) ? $meta_data["eva_language_code"] : "" ;
$eva_add_options = ( isset($message_data["eva_add_options"]) ) ? $message_data["eva_add_options"] : array();
$session = ( isset($arr_in_data["eva_session"]) ) ? $arr_in_data["eva_session"] : false ;
// Output function ============================================================================================================
function create_uid()
{
$ref = microtime(true);
$sec = $ref | 0;
return sprintf("%d%'08d", $sec, ($ref - $sec) * 100000000);
}
function curl( $url, $method = 'get', $data = '', $header = array(), $keep = true )
{
global $ch, $curl_cookie_file;
$res = array();
if ( false == $ch )
{
$ch = curl_init();
}
$_header = array(
'Accept-Language: en-US,en;q=0.7,fr;q=0.3',
'Accept-Encoding: gzip, deflate',
);
$header += $_header;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ( 'post' == $method AND '' != $data )
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ( strpos($url, 'https://') !== false )
{
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if ( $curl_cookie_file )
{
curl_setopt($ch, CURLOPT_COOKIEJAR, $curl_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $curl_cookie_file);
}
$ce = curl_exec($ch);
if ( false === $ce )
{
$res = array(
'eno' => curl_errno($ch),
'emsg' => curl_error($ch),
);
}
else
{
$res = array(
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'body' => $ce,
);
}
if ( false != $keep )
{
curl_close($ch);
$ch = false;
}
return $res;
}
function ouput_message($arr_push) {
$ref_id = create_uid();
$eva_name = ( isset($arr_push["eva_name"]) ) ? $arr_push["eva_name"] : "";
$platform_id = ( isset($arr_push["platform_id"]) ) ? $arr_push["platform_id"] : "";
$sender_chat_id = ( isset($arr_push["sender_chat_id"]) ) ? $arr_push["sender_chat_id"] : "";
$text = ( isset($arr_push["text"]) ) ? $arr_push["text"] : "";
$url_src = ( isset($arr_push["url_src"]) ) ? $arr_push["url_src"] : "";
$content_type = ( isset($arr_push["content_type"]) ) ? $arr_push["content_type"] : "";
$keep_session = ( isset($arr_push["keep_session"]) ) ? $arr_push["keep_session"] : "";
$date_send = gmdate("Y-m-d H:i:s");
$result_arr_push = array(
"ref_id" => $ref_id,
"eva_ref_id" => 0,
"eva_name" => $eva_name,
"dest_platform_id" => $platform_id,
"dest_chat_id" => $sender_chat_id,
"content_type" => $content_type,
"content_data" => array("text" => $text, "url_src" => $url_src),
"keep_session" => $keep_session,
"eva_session" => "",
"dt_sent" => $date_send,
);
$output_data = json_encode($result_arr_push);
return $output_data;
}
function push_message($arr_push) {
$api_url = "https://api.eva.id/ais/"._AI_TOKEN."/send_msg";
$ref_id = create_uid();
$eva_name = ( isset($arr_push["eva_name"]) ) ? $arr_push["eva_name"] : "";
$platform_id = ( isset($arr_push["platform_id"]) ) ? $arr_push["platform_id"] : "";
$sender_chat_id = ( isset($arr_push["sender_chat_id"]) ) ? $arr_push["sender_chat_id"] : "";
$text = ( isset($arr_push["text"]) ) ? $arr_push["text"] : "";
$url_src = ( isset($arr_push["url_src"]) ) ? $arr_push["url_src"] : "";
$content_type = ( isset($arr_push["content_type"]) ) ? $arr_push["content_type"] : "";
$keep_session = ( isset($arr_push["keep_session"]) ) ? $arr_push["keep_session"] : "";
$date_send = gmdate("Y-m-d H:i:s");
$result_arr_push = array(
"ref_id" => $ref_id,
"eva_ref_id" => 0,
"eva_name" => $eva_name,
"dest_platform_id" => $platform_id,
"dest_chat_id" => $sender_chat_id,
"content_type" => $content_type,
"content_data" => array("text" => $text, "url_src" => $url_src),
"keep_session" => $keep_session,
"eva_session" => "",
"dt_sent" => $date_send,
);
$output_data = json_encode($result_arr_push);
$push_data = curl( $api_url, "post", $output_data );
}
// Output Mode Message ===========================================================================================================
if($msg_original){
$ouputMode = 0;
$push["eva_name"] = $eva_name;
$push["platform_id"] = $platform_id;
$push["sender_chat_id"] = $sender_chat_id;
$push["content_type"] = 1;
$push["keep_session"] = false;
$push["text"] = "Your message is: ".$msg_original;
if($ouputMode == 0){
$push["text"] = $push["text"]." (Mode: output)";
echo ouput_message($push);
}
else{
$push["text"] = $push["text"]." (Mode: push)";
push_message($push);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment