Created
April 17, 2013 12:20
-
-
Save gracece/5403806 to your computer and use it in GitHub Desktop.
a simple weixin robot using php and mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//define your token | |
define("TOKEN", "sysucs"); | |
$wechatObj = new wechatCallbackapiTest(); | |
//$wechatObj->valid(); //第一次验证token时候才使用 | |
$wechatObj->responseMsg(); | |
class wechatCallbackapiTest | |
{ | |
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"; | |
$sysucs =new sysucs(); | |
if(preg_match('#通知|作业#i',$keyword)) | |
{ | |
$contentStr=$sysucs->reply($keyword); | |
} | |
else if(preg_match('#subscribe|Hello2BizUser#i',$keyword)) | |
$contentStr ="欢迎关注计科一班微信!"; | |
else | |
$contentStr = $sysucs->talk($keyword); | |
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); | |
echo $resultStr; | |
}else{ | |
echo "空消息.."; | |
} | |
}else { | |
echo "欢迎使用计科一班微信公共帐号 (sysucs)!"; | |
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; | |
} | |
} | |
} | |
class sysucs | |
{ | |
public function reply($keyword){ | |
error_reporting(E_ALL); | |
$dbc = new mysqli('localhost','user','password','sysucs'); | |
mysqli_query($dbc,"SET NAMES utf8"); | |
date_default_timezone_set('PRC'); | |
$resultStr =" "; | |
if(preg_match('#通知#i',$keyword)) | |
{ | |
$query = "SELECT * FROM info where 'content' not regexp '本周作业' ORDER BY date DESC;"; | |
$result =mysqli_query($dbc,$query); | |
$num_results = $result -> num_rows; | |
if($num_results >3) | |
$num_results =3; | |
for($i=0;$i < $num_results;$i++) | |
{ | |
$row = mysqli_fetch_array($result); | |
$resultStr .= "【".($i+1)."】".date("Y-m-d H:i ",$row['date'])."\n"; | |
$resultStr .=$row['content']."\n"; | |
} | |
$resultStr .= "更多通知请访问 http://m.sysucs.org/more.php"; | |
} | |
else if(preg_match('#作业#i',$keyword)) | |
{ | |
$query = "SELECT * FROM info where content like '%本周作业%' ORDER BY date DESC;"; | |
$result =mysqli_query($dbc,$query); | |
$num_results = $result -> num_rows; | |
if($num_results >1) | |
$num_results =1; | |
for($i=0;$i < $num_results;$i++) | |
{ | |
$row = mysqli_fetch_array($result); | |
echo "<p>".date("Y-m-d H:i ",$row['date'])."</p>"; | |
echo $row['content']; | |
$resultStr .= "【".($i+1)."】".$row['content']."\n"; | |
} | |
} | |
$resultStr=html_entity_decode($resultStr); | |
return strip_tags($resultStr); | |
} | |
function talk($keyword) | |
{ | |
$dbc = new mysqli('localhost','user','password','sysucs'); | |
mysqli_query($dbc,"SET NAMES utf8"); | |
date_default_timezone_set('PRC'); | |
if($keyword =="帮助") | |
{ | |
return "回复“通知”查看最近通知,回复“作业”查看本周作业。\n 要教我回答,请英文下划线开头,接着问题,接着#,接着回答,例如“_计科一班#威武”。\n 其他功能还在完善中,欢迎提建议!http://m.sysucs.org"; | |
} | |
if (substr($keyword, 0,1) == '_') | |
{ | |
$pos = strpos($keyword, '#'); | |
if ($pos > -1) | |
{ | |
$q = substr($keyword, 1,$pos - 1); | |
if(preg_match('#帮助|作业|通知#i',$q)) | |
return "去去去,瞎捣乱,关键词你也来改"; | |
$a = substr($keyword, $pos + 1); | |
if($q==""||$a=="") | |
return "问题答案未填完整,换个姿势再来一次!"; | |
$orginQ = $q; | |
$orgina = $a; | |
$q =addslashes($q); | |
$a =addslashes($a); | |
$query ="select * from talk where question = '".$q."' and answer = '".$a."'"; | |
$exist = mysqli_query($dbc,$query); | |
if($exist->num_rows >0) | |
{ | |
return "讨厌,已经有一模一样的就不要再插了嘛 /::~"; | |
} | |
else | |
{ | |
$query ="insert into talk values('".$q."','".$a."')"; | |
$result = mysqli_query($dbc,$query); | |
if($result) | |
return "/:8-)已记录:" . $orginQ . '/' . $orgina ; | |
else | |
return "插入失败!"; | |
} | |
} | |
else | |
return "分隔符改成#了(在英文标点里找),可以输入表情 /::B"; | |
} | |
else | |
{ | |
$orginKeyword =$keyword; | |
$keyword=addslashes($keyword); | |
$query ="select * from talk where question like '%".$keyword."%' order by rand() limit 1"; | |
$result = mysqli_query($dbc,$query); | |
if($result) | |
{ | |
$num_results = $result -> num_rows; | |
if($num_results == 0) | |
return "我不知道【".$orginKeyword."】是神马东东/:&-(,教教我吧,下次我就能回答你了/:8*~可以按照 “_问题#回答” 的格式使我变得更聪(xié)明(è),可以加表情的~。输入“帮助”看看更多功能~"; | |
else | |
{ | |
$row = mysqli_fetch_array($result); | |
return $row['answer']; | |
} | |
} | |
else | |
{ | |
return "数据库查询失败"; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment