一.echobot示例

1.在客户端搜索"echobot", 并点击进入私聊界面

2.点击"开启"以后,机器人会根据用户所发的文字原封不动发一条文字消息回来

3.机器人PHP代码如下

define('MESSAGE_TEXT_TYPE', 0);
define('REQUEST_TALK_MESSAGE_TYPE', 1);

// api访问地址
$API_URL = 'https://botapi.chaoxin.com/';

// 从机器人管理页面获得的token
$API_TOKEN = '<bot_token>';

// 获取客户端的请求
$msg = json_decode(file_get_contents('php://input'), true);

// 当请求类型为1时表示机器人接受聊天消息
if ($msg['request_type'] == REQUEST_TALK_MESSAGE_TYPE)
{
    // 当聊天消息为文本消息时
    if ($msg['message_type'] == MESSAGE_TEXT_TYPE)
    {
         $text = $msg['text'];

        // 构建发送api请求的参数
        $parameters = array(
            'chat_id' => $msg['chat_id'],
            'chat_type' => $msg['chat_type'],
            'text' => $text
        );

        // 请求发送文字消息协议
        apiRequest('sendTextMessage', $parameters);
    }
}

function apiRequest($method, $parameters)
{
    global $API_URL, $API_TOKEN;
    $url = $API_URL.$method.'/'.$API_TOKEN;

    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $url);

    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ci, CURLOPT_POST, true);
    curl_setopt($ci, CURLOPT_POSTFIELDS, $parameters);

    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);

    $response = curl_exec($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

    curl_close($ci);

    return array($http_code, $response);
}

二.inlinebot示例

1.在输入框输入"@inlinebot 古典音乐家"

2.在返回的结果条目中选择你要发送图片的音乐家

3.机器人PHP代码如下

define('REQUEST_INLINE_TYPE', 2);
define('BASIC_INLINE_RESPONSE_TYPE', 1);
define('IMAGE_TEXT_MIXED_STYLE_TYPE', 0);

// api访问地址
$API_URL = 'https://botapi.chaoxin.com/';

// 从机器人管理页面获得的token
$API_TOKEN = '<bot_token>';

// 获取客户端的请求
$msg = json_decode(file_get_contents('php://input'), true);

// 当请求类型为2时表示机器人接受输入框指令
if ($msg['request_type'] == REQUEST_INLINE_TYPE)
{
    //匹配输入框指令的文本
    if ($msg['text'] == "古典音乐家")
    {       
        $messages = array();

        // 构建第一个条目
        $message = array();
        $message['icon'] = $message['image_url'] = 'https://r.chaoxin.com/a81259c/2016-10-24_17/c3ff1/62e39/1477300996_319717_570_800.jpg';
        $message['message_id'] = md5($message['icon']);
        $message['title'] = '莫扎特';
        $message['description'] = '奥地利音乐家';
        $messages[] = $message;

        // 构建第二个条目
        $message = array();
        $message['icon'] = $message['image_url'] = 'https://r.chaoxin.com/a81259c/2016-10-24_17/7fe6f/26d47/1477301236_826017_440_477.jpg';
        $message['message_id'] = md5($message['icon']);
        $message['title'] = '贝多芬';
        $message['description'] = '德国音乐家';
        $messages[] = $message;

        // 构建发送api请求的参数
        $parameters = array(
            'uuid'=> $msg['uuid'],
            'type' => BASIC_INLINE_RESPONSE_TYPE, //为用户可点击的消息内容
            'style' => IMAGE_TEXT_MIXED_STYLE_TYPE, //为图文混排模式
            'messages' => json_encode($messages, JSON_UNESCAPED_UNICODE)
        );
        apiRequest('sendImageInlineResponse', $parameters);
    }
}

function apiRequest($method, $parameters)
{
    global $API_URL, $API_TOKEN;
    $url = $API_URL.$method.'/'.$API_TOKEN;

    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $url);

    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ci, CURLOPT_POST, true);
    curl_setopt($ci, CURLOPT_POSTFIELDS, $parameters);

    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);

    $response = curl_exec($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

    curl_close($ci);

    return array($http_code, $response);
}

三.replykeyboardbot示例

1.在客户端搜索"rkbot", 并点击进入私聊界面

2.输入"/button"以后,客户端会返回两排按钮

3.第一行按钮中"时间"按钮为基本文本消息按钮,点击机器人会返回当前时间

4.第一行按钮中"打招呼"按钮为提示文本消息按钮,点击后会在输入框显示"/say ", 打任意文本后机器人会发相应的文本消息回来

5.第二行按钮仅演示多行按钮是如何被创建的

define('MESSAGE_TEXT_TYPE', 0);
define('REQUEST_TALK_MESSAGE_TYPE', 1);
define('RESIZE_CUSTOM_KEYBOARD_TYPE', 1);
define('TEXT_KEYBOARD_TYPE', 0);
define('PROMPT_KEYBOARD_TYPE', 3);

// api访问地址
$API_URL = 'https://botapi.chaoxin.com/';

// 从机器人管理页面获得的token
$API_TOKEN = '<bot_token>';

// 当请求类型为1时表示机器人接受聊天消息
if ($msg['request_type'] == REQUEST_TALK_MESSAGE_TYPE)
{
    // 当消息不是文本消息时, 不处理请求
    if ($msg['message_type'] != MESSAGE_TEXT_TYPE) exit;

    // 当消息文本为"/button"时,开始返回按钮
    if ($msg['message']['text'] == '/button')
    {
        // 设置按钮模式
        $reply_keyboard['resize_keyboard'] = RESIZE_CUSTOM_KEYBOARD_TYPE;

        // 设置第一行第一个按钮,为基本文本消息按钮
        $keyboard = array();
        $keyboard['type'] = TEXT_KEYBOARD_TYPE;
        $keyboard['text'] = '时间';
        $keyboards[] = $keyboard;

        // 设置第一行第二个按钮,为提示文本消息按钮
        $keyboard = array();
        $keyboard['type'] = PROMPT_KEYBOARD_TYPE;
        $keyboard['text'] = '打招呼';
        $keyboard['content'] = '/say ';
        $keyboard['hint'] = '输入您想打招呼的文字';
        $keyboards[] = $keyboard;
        $reply_keyboard['keyboard'][] = $keyboards;

        // 设置第二行第一个按钮
        $keyboards = array();
        $keyboard = array();
        $keyboard['type'] = TEXT_KEYBOARD_TYPE;
        $keyboard['text'] = '吃饭';
        $keyboards[] = $keyboard;

        // 设置第二行第二个按钮
        $keyboard = array();
        $keyboard['type'] = TEXT_KEYBOARD_TYPE;
        $keyboard['text'] = '睡觉';
        $keyboards[] = $keyboard;

        // 设置第二行第三个按钮
        $keyboard = array();
        $keyboard['type'] = TEXT_KEYBOARD_TYPE;
        $keyboard['text'] = '打豆豆';
        $keyboards[] = $keyboard;
        $reply_keyboard['keyboard'][] = $keyboards;

        // 构建发送api请求的参数
        $parameters = array(
                'chat_id' => $msg['chat_id'],
                'chat_type' => $msg['chat_type'],
                'text' => '返回如下按钮',
                'reply_keyboard'=> json_encode($reply_keyboard)
        );
    }

    // 处理第一行第一个按钮的请求,并返回当前时间给客户端
    if ($msg['message']['text'] == '时间')
    {
        $parameters = array(
                'chat_id' => $msg['chat_id'],
                'chat_type' => $msg['chat_type'],
                'text' => date('Y-m-d h:i:s'),
        );
    }

    // 处理第一行第二个按钮的请求,当文本是以"/say "起始时, 将之后的文字以文本消息发给客户端
    if (substr($msg['message']['text'], 0, 5) == '/say ')
    {
        $text = substr($msg['message']['text'], 5);
        $parameters = array(
                'chat_id' => $msg['chat_id'],
                'chat_type' => $msg['chat_type'],
                'text' => $text
        );
    }

    // 处理第二行第二个按钮的请求,并将文本消息发给客户端
    if (in_array($msg['message']['text'], ['吃饭', '睡觉', '打豆豆']))
    {
        //构建发送api请求的参数
        $parameters = array(
                'chat_id' => $msg['chat_id'],
                'chat_type' => $msg['chat_type'],
                'text' => $msg['message']['text']
        );
    }

    apiRequest('sendTextMessage', $parameters);

function apiRequest($method, $parameters)
{
    global $API_URL, $API_TOKEN;
    $url = $API_URL.$method.'/'.$API_TOKEN;

    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $url);

    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ci, CURLOPT_POST, true);
    curl_setopt($ci, CURLOPT_POSTFIELDS, $parameters);

    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);

    $response = curl_exec($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

    curl_close($ci);

    return array($http_code, $response);
}