php给微信公众号接入聊天机器人程序+采坑记录

php给微信公众号接入聊天机器人程序

今天逛了下我的公众号,突然心血来潮,想添加个自动聊天功能,于是…动手…!!
主要用到的api:

  1. 图灵机器人api
  2. 青云客智能聊天机器人API
  3. 茉莉机器人API

至于为什么要用三个,因为我之前的图灵机器人好像"不行了",不知道出了啥问题,同时图灵机器人免费版的有调用次数限制,就打算放弃图灵,去寻找其他api,
就找到了其余两个,业务逻辑写好后,逐渐发现后两个api还不够智能(这点纯属个人看法),于是又重新拾起图灵,折腾了一番后终于搞定了,当时接入的图灵报错主要错误有两个:

  1. json数据错误
  2. 返回 “加密方式错误”

对于返回 "加密方式错误"的情况,我没有多想,直接删除当前的机器人,重新新建一个,但是后来百度了一下,发现原来是api密钥开关打开了,关掉便可.
在这里插入图片描述
整个过程觉得最恶心的就是提交的json数据了,这玩意儿搞了我好多个小时呢.返回的结果只有一个

{'intent': {'code': 4000}, 'results': [{'groupType': 0, 'resultType': 'text', 'values': {'text': '请求参数缺失或格式错误!'}}]}

开始以为是参数错了或者少了,于是我再用python试了一遍,完美通过
在这里插入图片描述
这是我的php

        $param = "{
    'perception': {
        'inputText': {
            'text': $word,
        }
    },
    'userInfo': {
        'apiKey': $appkey,
        'userId': 'robot1',
        }
    }";

这下证明参数是没错的,又试了json字符串转对象,转数组对象,数组转…等等各种尝试都不行,终于>>>
在这里插入图片描述
明显加了 “{}” 和单引号,于是乎…
在这里插入图片描述

在这里插入图片描述

上面便是我今日主要采坑记录…下面接入到我的公众号去
这次功能开发承接到上次开发的公众号.

检索微信关键字无匹配后,调用机器人
在这里插入图片描述

新建robot类,写个getText方法,实例化分情况实例化三个api对象,优先实现图灵机器人,一旦图灵机器人调用次数用完,返回错误代码err,接着使用其余的机器人api,以此类推(代码写得有点不好,自学新手,大佬有更好的建议,欢迎来吐槽哈)
在这里插入图片描述

图灵机器人api

<?php

class tuling_robot {
    private $api="http://openapi.tuling123.com/openapi/api/v2";
    private $robot1="-----------appkey1--------------";
    private $robot2="-----------appkey2--------------";

    public function getText($word) {
        $param=$this->getParam($word,$this->robot1); //拼接json数据
        $json=$this->Http_post($param);
        $code=$json->intent->code;//响应码
        if ($code=="4003"){ //4003  调用次数用完
            $param=$this->getParam($word,$this->robot2);
            $json=$this->Http_post($param);
            $code=$json->intent->code;//响应码
            if ($code=="4003"){  //4003  调用次数用完
                $contentStr = ($json->results)[0]->values->text;//回复内容
                return $contentStr;
            }else{
                return "err"; //两个机器人调用次数全都用完
            }
        }elseif ($code!="5000"&&$code!="6000"&&$code!="4000"&&$code!="4001"&&$code!="4002"&&$code!="4003"&&$code!="4005"&&$code!="4007"&&$code!="4100"&&$code!="4200"&&$code!="4300"&&$code!="4400"&&$code!="4500"&&$code!="4600"&&$code!="4602"&&$code!="7002"&&$code!="8008"&&$code!="0"){  //各种错误代码,因为我发现调用api返回的code,不一定每次都相同
            return $contentStr = ($json->results)[0]->values->text;//回复内容
        }else{
            return "err"; //其他错误
        }
    }

    public function getParam($word,$appkey){ //拼接参数
        $param = "{
    'perception': {
        'inputText': {
            'text': '{$word}',
        }
    },
    'userInfo': {
        'apiKey': '{$appkey}',
        'userId': 'robot1',
        }
    }";
        return $param;
    }

    public function Http_post($param){ //发起网络请求
        // 初始化curl
        $curl = curl_init();
		// 请求地址,
        curl_setopt($curl,CURLOPT_URL,"http://openapi.tuling123.com/openapi/api/v2");
		// POST请求
        curl_setopt($curl,CURLOPT_POST,1);
		// 发送POST请求时传递的参数或数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,$param);
		// 设置捕获的字符串内容返回但不输出
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
		// 执行并接收返回的json格式字符串内容
        $jsonstr = curl_exec($curl);
        curl_close($curl);
		// 转化为json对象
        $json = json_decode($jsonstr);
        return $json;
    }
}

青云客智能聊天机器人API
这个api目前没有apikey,貌似只需要关键字,然后一个get请求搞定,这点个人就觉得很不错,唯一缺点就是不够图灵智能,响应速度也不够图灵快
这里用QueryList发起网络请求,主要是因为我当初自学php时,百度找php网络请求和爬虫时看到这个,就学习了一波,公众号起初就是用QueryList发送网络请求的,

<?php
    require_once (dirname(__FILE__).'/../QueryList/vendor/autoload.php');
    use QL\QueryList;

class qingyun_robot {

    public function getText($word){
        $text="404";
        $api="http://api.qingyunke.com/api.php?key=free&appid=0&msg=".$word;
        $html=QueryList::get($api)->getHtml();
        $json=json_decode($html);
        $result=$json->result;
        $content=$json->content;
        if ($result==0){
            $text=str_replace("{br}","\n",$content);
            return $text;
        }
        return $text;
    }
}

茉莉机器人API
这个用起来稍微有点麻烦,因为他有来两种返回结果,一种是纯文本,一种是json,只有特定的关键字才会返回json,其余一律返回文本,第二种中返回的结果除了是个json之外,他的value都是经过Unicode编码过的,在python中直接将整个json进行解码是可以的,但是到了php就不行了,这里我将他拆开分别进行解码成中文
这是经过编码的返回结果:

{"title":"\u673a\u4f1a\u662f\u81ea\u5df1\u521b\u9020\u7684","content":"\u53f8\u9a6c\u5149\u62cd\u62cd\u738b\u5b89\u77f3\u7684\u80a9\u8180\uff1a\u201c\u4ecb\u752b\uff0c\u8ddf\u6211\u6597\uff1f\u4f60\u8fd8\u662f\u592a\u5355\u7eaf\u4e86\u3002\u201d\r\n\u738b\u5b89\u77f3\u6de1\u5b9a\u5730\u56de\u51fb\uff1a\u201c\u6709\u4ec0\u4e48\u4e86\u4e0d\u8d77\u7684\uff1f\u4e0d\u5c31\u662f\u7838\u4e2a\u6c34\u7f38\u561b\uff1f\r\n\u6362\u4e86\u6211\u4e5f\u4e00\u5b9a\u4f1a\u8fd9\u4e48\u505a\uff0c\u6211\u548c\u4f60\u6bd4\uff0c\u53ea\u662f\u5c11\u4e00\u4e2a\u673a\u4f1a\u7f62\u4e86\u3002\u201d\r\n\u53f8\u9a6c\u5149\u773c\u4e2d\u5c04\u51fa\u4e24\u9053\u5bd2\u5149\uff1a\u201c\u673a\u4f1a\uff0c\u662f\u81ea\u5df1\u521b\u9020\u7684\u3002\u4f60\u53ea\u77e5\u9053\u8001\u592b\u7838\u7f38\uff0c\r\n\u5374\u4e0d\u77e5\u9053\u90a3\u5b69\u5b50\u662f\u600e\u4e48\u6389\u8fdb\u53bb\u7684\u5427\uff1f\u201d"}

  class liliRobot{
        private  $api="http://i.itpk.cn/api.php?question=%s&api_key=自己的api_key&api_secret=自己的api_secret";

        public function getText($text){
            $api=sprintf($this->api,$text);
            $html=QueryList::get($api)->getHtml();
            if (strpos($html,'{"title":"')!==false){
                $title=$this->get_between($html,'{"title":"','","content":"');
                $content=$this->get_between($html,'","content":"','"}');
                $title=$this->unicodeDecode($title);
                $content=$this->unicodeDecode($content);
                return str_replace("&nbsp","",trim($content));
            }else{
                return $html;
            }

        }

        function get_between($input, $start, $end) { //截取字符串,将json字符串的title和content分别截取出来解码
            $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
            return $substr;
        }
        function unicodeDecode($unicode_str){//转换编码
            $json = '{"str":"'.$unicode_str.'"}';
            $arr = json_decode($json,true);
            if(empty($arr)) return '';
            return $arr['str'];
        }
    }

so…三个api调用代码都已经写好了,在外面调用即可,…
运行效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值