1. 程式人生 > >百度大腦和圖靈機器人制作一個簡單的自動聊天機器人【PHP程式碼】

百度大腦和圖靈機器人制作一個簡單的自動聊天機器人【PHP程式碼】

可以和機器人聊天 文字傳送訊息 網頁進行語音回覆

這裡主要用到了兩個介面

一個是百度大腦  用來實現語音合成

一個是圖靈機器人用來實現自動聊天

以下是程式碼分兩個版本 版本一是沒有語音合成 版本二是加上語音合成功能

php版本最低5.5

版本一:

<?php
$api='http://www.tuling123.com/openapi/api';
$cont=isset($_POST['con'])?$_POST['con']:'';
$p=isset($_POST['p'])?$_POST['p']:'';
if($p!=1){
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div style="width:600px; padding:10px; height:800px; border:1px solid #ccc; margin:15px auto;">
    <!-- <form action="jqr.php" method="post"> -->
    <div id="odiv" style="height:760px;">
';

}
function send_post($url, $post_data) {
  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 // 超時時間(單位:s)
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);
  return $result;
}
if($p){
 $post_data = array(
  'key' => 'afdf361909844a6395108f8406a15b02',//請自己設定key
  'info'=>$cont,
  'userid'=>'456789',//用於辨識使用者 測試階段也可以隨意寫
);
$conts=send_post($api, $post_data);
$conts=explode('text":"',$conts)[1];
$conts=explode('"',$conts)[0];

echo '<li style="float:right;width:auto"><img src="">'.$cont.'</li>'.'<li style="float:left; width:100%;">'.$conts.'</li>';
}

if(!$p){
    echo "</div><div style='width:600px; margin:5px auto; ''><input id='b01' type='text' style='height:30px; width:540px;'' name='con'><button id='but' style='height:30px;''>提交</button></div>
<!-- </form> -->
</div>";
}

if(!$p){
echo '<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
</script>
<script>
    $("button").click(function(){
    txt=$("#b01").val();
    $.post("jqr.php",{con:txt,p:1},function(result){
    	var htmla=$("#odiv").html() + result;
   		var odiv =  document.getElementById("odiv");
   		odiv.innerHTML = htmla+"<br>";

  	})
})
</script>';

}

?>



版本二:

<?php
$api='http://www.tuling123.com/openapi/api';
$cont=isset($_POST['con'])?$_POST['con']:'';
$p=isset($_POST['p'])?$_POST['p']:'';
if($p!=1){
    echo '<!DOCTYPE html>
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type=text/css>
*{list-style:none}
</style>
</head>
<div style="width:600px; padding:10px; height:800px; border:1px solid #ccc; margin:15px auto;">
    <!-- <form action="jqr.php" method="post"> -->
    <div id="odiv" style="height:760px;">
';

}
function send_post($url, $post_data) {
  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 // 超時時間(單位:s)
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);
  return $result;
}
if($p){
 $post_data = array(
  'key' => 'afdf361909844a6395108f8406a15b02',
  'info'=>$cont,
  'userid'=>'456789',
);
$conts=send_post($api, $post_data);
$conts=explode('text":"',$conts)[1];
$conts=explode('"',$conts)[0];

echo '<li style="float:right;width:auto">

'.$cont.'</li>'.'<li style="float:left; width:100%;" style="width:40px; height:20px line-height:20px;"><audio controls style="width:40px; height:10px;" >
<source src="http://hope.tk2a.com/baidu.php?con='.$conts.'" type="audio/ogg">
</audio>'.$conts.'</li>';
}

if(!$p){
    echo "</div><div style='width:600px; margin:5px auto; style='width:40px; height:10px;' ''><input id='b01' type='text' style='height:30px; width:540px;'' name='con'><button id='but' style='height:30px;''>提交</button></div>
<!-- </form> -->
</div>";
}

if(!$p){
echo '<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
</script>
<script>
    $("button").click(function(){
    txt=$("#b01").val();
    $.post("ltt.php",{con:txt,p:1},function(result){
    	var htmla=$("#odiv").html() + result;
   		var odiv =  document.getElementById("odiv");
   		odiv.innerHTML = htmla+"<br>";

  	})
})
</script>';

}

?>