微信订阅号的学习

  1. 开发阶段的回复功能已经在前几篇博文中总结过了。

  2. 图灵机

    微信订阅号的学习_第1张图片

图灵机器人平台是一个免费的API开放平台,基于自然语言处理、知识库和云计算等技术,为广大开发者、合作伙伴提供的一系列智能语义处理能力(包括语义理解、智能问答、知识库对接等)的服务平台。
图灵机器人平台能被运用到智能手机,智能电视,智能车载,智能移动端,网站客服等多领域,实现自然语言的智能交互。
同时,图灵机器人平台能“一键式”接入微信公众平台,实现智能聊天机器人,智能客服机器人等服务。

调用api 博文:http://blog.csdn.net/pamchen/article/details/31743583

调用图灵机智能机器人的接口的代码:

<?php
/**
  * wechat php test
  */
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$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;
            //$type=$postobj->MsgType;
            $type =$postObj->MsgType;
   $customevent=$postObj->Event;
            $latitude=$postObj->Location_X;
            $longitude=$postObj->Location_Y;
   $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>";  
  $musicTpl="<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
          <CreateTime>%s</CreateTime>
           <MsgType><![CDATA[%s]]></MsgType>
           <Music>
                <Title><![CDATA[%s]]></Title>
              <Description><![CDATA[%s]]></Description>
                <MusicUrl><![CDATA[%s]]></MusicUrl>
                <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
                   </Music>
        </xml>";
      $newsTpl="<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <ArticleCount>2</ArticleCount>
    <Articles>
    <item>
    <Title><![CDATA[女神的气质大衣]]></Title> 
    <Description><![CDATA[一款舒适的长大衣是寒冷季节的首选]]></Description>
    <PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/1.jpg]]></PicUrl>
    <Url><![CDATA[http://www.baidu.com]]></Url>
    </item>
    <item>
    <Title><![CDATA[墨概念呢子外套]]></Title>
    <Description><![CDATA[秋冬女装大衣2014新款韩版修身大毛领毛呢外套]]></Description>
    <PicUrl><![CDATA[http://4.zhangxuman.sinaapp.com/2.jpg]]></PicUrl>
    <Url><![CDATA[http://www.baidu.com]]></Url>
    </item>
    </Articles>
    </xml>";
             if($type=="event" )//and $customrevent=="subscribe")
            {
                 $msgType = "text";
                    $contentStr="感谢您的关注\n 回复1可查看联系方式 \n 回复2可还钱 \n 回复3可查看各种小吃"; 
                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                 echo $resultStr;
            }
            else 
   {
                switch($type)
                {
                    case "text":
                 {
                      
                     if(!empty( $keyword ))
                     {
                       
                         if($keyword=="1")
                         {
                             $msgType = "text";
                             $contentStr="广东工业大学工二馆,联系方式:18790652159,联系人:张星星";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                         }
                         else if($keyword=="2")
                         {
                             $msgType = "text";
                             $contentStr="请将钱打入工行卡88888888,于五个工作日内,谢谢配合";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                         }
                         else if($keyword=="3")
                         {
                             $msgType = "text";
                             $contentStr="饿了吗?河南小吃,广东小吃,台湾小吃,各种美味等着你哦";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                         }
                         else if($keyword=="音乐")
                         {
                            $Title="纯真年代";
                            $Description="离婚前规则主题曲";
                            $MusicUrl="http://1.zhangxuman.sinaapp.com/music/1.mp3";
                            $HQMusicUrl= $MusicUrl;
                            $msgType="music";
                            $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$Title,$Description,$MusicUrl,$HQMusicUrl);     
                           
                         }
                         
                         else if($keyword=="购物时尚")
                         {
    
                             //$msgType="text";
                            $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time); 
                             
                         }
                         else     //这个else里面是调用图灵机的apidai'm
                         { 
                             $apiKey = "70fc220d8d6364d9a6fe894d7ec7ec59"; 
                             $apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO";//2 3 1
                             $url = str_replace("INFO", $keyword, str_replace("KEY", $apiKey, $apiURL));
                             
                             /** 方法一、用file_get_contents 以get方式获取内容 */ 
                                    $res =file_get_contents($url);      //这三句是解析api返回的res,并用contentStr输出
                                    $apiObj =json_decode($res);
                                    $contentStr = $apiObj->text;
                                    $msgType = "text";
                                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    
                         }   
                     }
                     else
                     {
                         $contentStr= "Input something...";
                     }
                            
                        break;     
                 }
                   
                    case "voice":
                    {
                        $msgType = "text";
                        $contentStr="语音功能尚未完善,请输入文本信息";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    case "image":
                    {
                        $msgType = "text";
                        $contentStr="你的图片很漂亮";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }                    
                    case "location":
                    {
                        $msgType = "text";
                        $contentStr="你的纬度是{$latitude},经度是{$longitude},我们已经锁定!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    case "link":
                    {
                        $msgType = "text";
                        $contentStr="你的链接有病毒吧!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                    
                    default :
                    {
                        $msgType = "text";
                        $contentStr="此功能尚未开发";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        break;
                    }
                }
                
   }
      
       echo $resultStr;
  }
             else 
        {
         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;
  }
 
 }
}
?>

你可能感兴趣的:(微信订阅号的学习)