Home  >  Article  >  Backend Development  >  PHP WeChat public platform development chat robot development_PHP tutorial

PHP WeChat public platform development chat robot development_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:131313browse

[PHP WeChat public platform development series]

01. Configure WeChat interface
02. Public platform sample code analysis
03. Subscription event (subscribe) processing
04. Development of simple reply function
05. Weather forecast function development
06. Translation function development
07. Chatbot function development


URL of this article: http://www.phpchina.com/archives/view-43393-1.html
This series is contributed by PHPChina's specially invited author @David_Tang. Please indicate the author's information and the address of this article when reprinting.

1. Introduction

The previous article introduced the development of the translation function of the WeChat public platform, which realizes translation between Chinese, English and Japanese, and can also be used in real life. In the next article, we will complete a more interesting function, which is a chatbot that can chat with you and make you happy when you are bored.

2. Idea analysis

In this experiment, we will call the API provided by the official Xiaohuangji (http://www.simsimi.com/), combined with crawling the webpage of the Xiaojiu robot (http://www.xiaojo.com/), complement each other. Simsimi is charged, but you can try a 7-day test and you can use 100 replies for free every day; Xiaojiu robot can be used without restrictions, but only if it is not officially blocked.

3. Little Yellow Chicken API Analysis

3.1 API & URL

Official API address: http://developer.simsimi.com/api

Request URL: http://sandbox.api.simsimi.com/request.p

The free version is used here for testing. The paid version is similar except that the URL address is different.

3.2 Request examples and parameter description

Request example:

http://sandbox.api.simsimi.com/request.p?key=your_trial_key&lc=en&ft=1.0&text=hi

Parameter description:

key: applied API Key

lc: Language code, supported languages, use ch for simplified Chinese, zh for traditional Chinese, and en for English. For details, please refer to: http://developer.simsimi.com/lclist

ft: whether to set filter,

​0.0: Unfiltered (contains cursing, sexual content);

1.0: Filter uncivilized words (only supports Korean for now)

text: requested text

3.3 Return value analysis

result: execution result return code

    • 100-OK.
    • 400-Bad Request.
    • 401-Unauthorized.
    • 404-Not found.
    • 500-Server Error.

id: Reply message id (this item is only available when result=100)

response: reply message (this item is only available when result=100)

msg: Status corresponding to the execution result return code

4. Obtain the Little Yellow Chicken API Key

4.1 Register simsimi account

URL: http://developer.simsimi.com/signUp

4.2 Activate account

4.3 Obtain API Key

5. Specific implementation

5.1 Implementation of calling Xiaohuangji API

Call the simsim($keyword) function and replace "Your API Key" with the applied API Key.

PHP WeChat public platform development chat robot development_PHP tutorial
    //小黄鸡
    public function simsim($keyword){

        $key="41250a68-3cb5-43c8-9aa2-d7b3caf519b1";
        $url_simsimi="http://sandbox.api.simsimi.com/request.p?key=".$key."&lc=ch&ft=0.0&text=".$keyword;
        
        $json=file_get_contents($url_simsimi);  // 把整个文件读入一个字符串中

        $result=json_decode($json,true);  // 对JSON 格式的字符串进行编码

        //$errorCode=$result['result'];  // 调试用

        $response=$result['response'];  // 回复的消息

        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 "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
                default:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
            }
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

Description:

Because sometimes the little yellow chicken does not reply, a judgment is added to the simsim() function. If $response is not empty, $response is returned; if $response is empty, a small code is added to let it Reply with customized messages randomly so you can respond to requests.

5.2 Call Xiaojiu robot to implement

Xiaojiu Robot does not provide an API, so web crawling can only be achieved through PHP functions. The code is as follows:

PHP WeChat public platform development chat robot development_PHP tutorial
    //小九机器人
    public function xiaojo($keyword){

        $curlPost=array("chat"=>$keyword);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');//抓取指定网页
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        if(!empty($data)){
            return $data;
        }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 "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
                default:
                    return "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽";
                    break;
            }
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

5.3 Two Dragons Playing with Phoenix

We can also integrate the Xiaohuangji and Xiaojiu robots above. The specific code is as follows:

PHP WeChat public platform development chat robot development_PHP tutorial
    //双龙戏凤
    public function chatter($keyword){

        $curlPost=array("chat"=>$keyword);
        $ch = curl_init();    //初始化curl
        curl_setopt($ch, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');    //抓取指定网页
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 0);    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);    //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);    //运行curl
        curl_close($ch);

        if(!empty($data)){
            return $data." [/::)小九]";
        }else{
            return $this->simsim($keyword)." [simsim/::D]";
        }
    }
PHP WeChat public platform development chat robot development_PHP tutorial

6. Test

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740331.htmlTechArticle[PHP WeChat public platform development series] 01. Configure WeChat interface 02. Public platform sample code analysis 03. Subscribe to events (subscribe) processing 04. Simple reply function development 05. Weather forecast function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn