Can
Be Better

EasyWechat+Laravel接入图灵机器人

很简单,上代码

public function serve()
    {
        $wechat = app('wechat');
        $user = $wechat->user;
        $server = $wechat->server;
        $server->setMessageHandler(function ($message) use ($user){
            switch ($message->MsgType) {
                //事件类型消息(点击菜单、关注、扫码),略
                case 'event':
                    switch ($message->Event) {
                        case 'subscribe':
                            // code...
                            break;

                        default:
                            // code...
                            break;
                    }
                    break;
                //文本信息处理
                case 'text':
                    //获取到用户发送的文本内容
                    $content = $message->Content;
                    //发送到图灵机器人接口
                    $url = "http://www.tuling123.com/openapi/api?key=【api key】&info=".$content;
                    //获取图灵机器人返回的内容
                    $content = file_get_contents($url);
                    //对内容json解码
                    $content = json_decode($content);
                    //把内容发给用户
                    return new Text(['content' => $content->text]);
                    break;
                //图片信息处理,略
                case 'image':
                    $mediaId  = $message->MediaId;
                    return new Image(['media_id' => $mediaId]);
                    break;
                //声音信息处理,略
                case 'voice':
                    $mediaId  = $message->MediaId;
                    return new Voice(['media_id' => $mediaId]);
                    break;
                //视频信息处理,略
                case 'video':
                    $mediaId  = $message->MediaId;
                    return new Video(['media_id' => $mediaId]);
                    break;
                //坐标信息处理,略
                case 'location':
                    return new Text(['content' => $message->Label]);
                    break;

                //链接信息处理,略
                case 'link':
                    return new Text(['content' => $message->Description]);
                    break;

                default:
                    break;
            }
        });
        return $wechat->server->serve();
    }

 

 

不开启评论,如有问题疑问请发邮件。i@ltq.im最长的路 » EasyWechat+Laravel接入图灵机器人