先说明一下,这个是文字版的不支持语音,官方有语音版的。
最近比较忙,但是做公司的东西总感觉没意思,就想抽空搞搞自己的东东。于是就有了这个小i机器人,这种权当是思维娱乐了,谁叫美剧冬歇了呢,不过最近一些美剧已经开始更新啦。哈哈
可以通过扫描下面的二维码或者直接添加微信号xiaoibot进行关注哈~
关于源码:
其实很简单,我是抓包分析小i机器人wap版然后用正则表达式处理了一下,原理和之前我博客微信一样,博客微信的源码已经公布了, Kaedeen博客公众微信源码。
源码已经被红色石头公布了,我也不藏着掖着了,大家可以帮我改改,有好意见要告诉我哦~
<div style="background:#fdfdfd;color:black;"><u>HTML+PHP语言</u>: </div>
<div class="source" style="font-family: '[object HTMLOptionElement]', Consolas, 'Lucida Console', 'Courier New'; color: rgb(246, 243, 232); background-color: rgb(36, 36, 36);"><span style="color: rgb(229, 120, 109);"><?php</span>
/
作者:Kaedeen
来源:http://www.kaedeen.com/
日期:2013.1.7
*
/
//define your token
define("TOKEN", "kaede");
$wxObj = new weixinCallbackApi();
//$wxObj->valid(); //第一次验证时使用
$wxObj->responseMsg();
class weixinCallbackApi
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "";
$picnews = "";
//新关注我的用户
if($keyword=='Hello2BizUser')
{
$contentStr=$this->welcome($toUsername);
}
$contentStr = $this->get_wap_xiaoi($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something…";
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
//调用小i机器人wap版
public function get_wap_xiaoi($key){
$post_data =
array(
'requestContent='.$key,
);
$post_data = implode('&',$post_data);
$url='http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();
$preg = '/</span>(.*)</p>/iUs';
preg_match_all($preg,$result,$match);
$response_msg=$match[0][0];
$preg = "/</?[^>]+>/i";
$response_msg=preg_replace($preg,'',$response_msg);
if("hello,how are you"==$response_msg||"how do you do"==$response_msg)
{
$response_msg="小i机器人欢迎您,作者主页地址:www.kaedeen.com。小i机器人不断学习中,欢迎各种调戏…/:,@-D";//欢迎语
}
$response_msg=trim($response_msg);
return $response_msg;
}
//问候语
public function welcome($toUsername) {
if($toUsername=="gh_48776be7ef17"){//微信原始id
return "小i机器人欢迎您,作者主页地址:www.kaedeen.com。小i机器人不断学习中,欢迎各种调戏…/:,@-D";//欢迎语
}
}
}
//sae打日志封装函数
function sae_log($msg)
{
sae_set_display_errors(false);//关闭信息输出
if (is_array($msg))
{
$msg = implode(",", $msg);
}
sae_debug("[kaedeen_begin]".$msg."[kaedeen_begin]");//记录日志
sae_set_display_errors(true);//记录日志后再打开信息输出,否则会阻止正常的错误信息的显示
}
?>