码迷,mamicode.com
首页 > 微信 > 详细

记微信开发(图灵机器人接入)

时间:2016-03-21 12:07:25      阅读:475      评论:0      收藏:0      [点我收藏+]

标签:

记微信开发(图灵机器人接入)

记微信开发(图灵机器人接入)

效果图:

技术分享

技术分享
()申请APIKEY步骤略)
code:

<?php
/**
* wechat php test
*/

//define your token
define("TOKEN", "jiekou");//定义识别码
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();

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);
$RX_TYPE = trim($postObj->MsgType);

switch($RX_TYPE)
{
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}

public function handleText($postObj)
{
$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";
$str = mb_substr($keyword,0,2,"UTF-8");
$str_valid = mb_substr($keyword,0,-2,"UTF-8");
if($str == "翻译" && !empty($str_valid)){
$word = mb_substr($keyword,2,202,"UTF-8");
//调用有道词典
$contentStr = $this->youdaoDic($word);
}elseif($str != "翻译" && $keyword=="1"){
$contentStr = "公司简介";
}elseif($str != "翻译" && $keyword=="2"){
$contentStr = "最新优惠";
}else{
$contentStr = $this->tuling($keyword);
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}

public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "感谢您关注"."\n"."微信号:zhuzhu365"."\n"."回复:"."\n"."【1】公司简介"."\n"."【2】最新优惠"."\n"."【翻译+内容】即可翻译文本信息"."\n"."回复任意非以上内容,将接入机器人小猪猪~"."\n"."更多内容,敬请期待...";
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}

public function responseText($object, $content, $flag=0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>"
;
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
public function youdaoDic($word){

$keyfrom = "notfound"; //申请APIKEY 时所填表的网站名称的内容
$apikey = "APIKEY"; //从有道申请的APIKEY

//有道翻译-xml格式
$url_youdao = ‘http://fanyi.youdao.com/fanyiapi.do?keyfrom=‘.$keyfrom.‘&key=‘.$apikey.‘&type=data&doctype=xml&version=1.1&q=‘.$word;

$xmlStyle = simplexml_load_file($url_youdao);

$errorCode = $xmlStyle->errorCode;

$paras = $xmlStyle->translation->paragraph;

if($errorCode == 0){
return $paras;
}else{
return "无法进行有效的翻译";
}
}
// 图灵机器人
public function tuling($keyword) {
$apiKey = "APIKEY";//自己申请的APIKEY
$apiURL = "http://www.tuling123.com/openapi/api?key=".$apiKey."&info=". $keyword;
$json=file_get_contents($apiURL);

$result=json_decode($json,true);

//$errorCode=$result[‘result‘];

$response=$result[‘text‘];

if(!empty($response)){
return $response;
}else{
$ran=rand(1,5);
switch($ran){
case 1:
return "小猪猪今天累了,明天再陪你聊天吧。";
break;
case 2:
return "小猪猪睡觉喽~~";
break;
case 3:
return "呼呼~~呼呼~~";
break;
case 4:
return "你话好多啊,不跟你聊了";
break;
case 5:
return "感谢您关注zhuzhu365";
break;
default:
return "感谢您关注zhuzhu365";
break;
}
}
}


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;
}
}
}

?>

记微信开发(图灵机器人接入)

标签:

原文地址:http://www.cnblogs.com/XBlack/p/5301120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!