配置 Webhooks

tips 说明

兔小巢支持在用户有新的反馈时,实时通过 Webhooks 把反馈推送到指定的地址以便产品做后续处理。例如产品需要通过小红点的形式提醒用户的反馈已得到管理员的回复。

原理是 HTTP 以 POST 方法向指定的 URL 请求,同时带上 MIME 头为 application /json 的 JSON 格式的数据。 目前支持的事件用户发贴、回贴,如果你还需要其他类型事件,请联系我们。

配置步骤

1、填写服务地址

管理员登录兔小巢管理后台(txc.qq.com),点击左侧边栏-开发-Webhooks ,在链接地址处填写一个公网可访问的 HTTP/HTTPS 服务地址保存并发布。

tips 常见问题

Q1: 如何检测配置成功呢?

A1: 点击保存并发布,兔小巢服务器会向该地址发送一个测试请求。

下面以 PHP 开发语言为示例:

$event = json_decode(file_get_contents("php://input")); // 接受请求参数

// 写入文件
$myfile = fopen("tucao.log", "w") or die("Unable to open file!");
fwrite($myfile, json_encode($event));
fclose($myfile);

查看 tucao.log 内容是否接收到相关测试请求数据,例如下面:

{"id":"N4YnjFcdAGLBmCpsWJO8S3oUzlV6ZfXy","type":"webhook.test",
"payload":{"post":{"id":"oW4Q5MnZVtaRTj9s3AXqHcBv07ikudFU",
"user":{"nickname":"system"}}},"created_at":"2018-10-18T15:33:09+08:00"}

2、接收具体回调内容

下面以 PHP 开发语言为示例

$event = json_decode(file_get_contents("php://input")); // 接受请求参数

// 对异步通知做处理
if (!isset($event->type)) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
    exit("fail");
}
switch ($event->type) {
    case "post.created":
        // 业务代码
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
        break;
    case "reply.created":
        // 业务代码
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
        break;
    default:
        header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
        break;
}

warn 重要提示

接受webhooks内容成功后,请尽快返回200状态码,否则将视为推送失败。推送失败的webhooks将会重入队列推送,累计7次推送重试。

3、数据格式字典 (后续会有调整)

如果需要以管理员身份登录,请使用管理员的 QQ 号作为 openid,登录兔小巢管理后台(txc.qq.com)用验证码绑定验证移动端管理员权限。验证码可以在 管理员 - 编辑 - 移动端权限中获取。

// 主贴发表 post.created
{
    "id": "3cd589289b1362a8d17e6444ebbdbb11",
    "type": "post.created",
    "payload": {
        "post": {
            "id": "153984852048553113",
            "has_admin_reply": true,
            "avatar_url": "https://txc.qq.com/static/desktop/img/products/def-product-logo.png",
            "nick_name": "tucao_test",
            "content": "666666666",
            "openid": "",
            "user_id": 1,
            "user": {
                // ...
            },
            "is_admin": true,
            "is_top": false,
            "is_good": false,
            "is_spam": false,
            "is_todo": false,
            "is_locked": false,
            "is_hidden": false,
            "is_notice": false,
            "is_liked": false,
            "is_abuse": false,
            "reply_count": 0,
            "like_count": 0,
            "images_count": 0,
            "created_at": "2018-10-18 15:42:00",
            "time": "现在",
            "updated_at": "2018-10-18 15:42:00",
            "last_reply_at": null,
            "images": [],
            "replies": [],
            "extra": [],
            "faq_id": 0,
            "replies_all": [],
            "type": 3,          // type  3 表示该反馈为 iwant 类型
            "categories": [],   // 反馈分类
            "user_contacts": [] // 用户信息收集
        }
    },
    "created_at": "2018-10-18T15:42:00+08:00"
}

// 回复发表 reply.created
{
    "id": "95c1eb7da00ae1f8958b91a307467db7",
    "type": "reply.created",
    "payload": {
        "reply": {
            "id": "153984916895006730",
            "f_title_id": "153984852048553113",
            "type": 0,
            "parent_reply_id": "",
            "user_id": 2,
            "uin": 0,
            "nick_name": "tucao_test_2",
            "avatar_url": "https://txc.qq.com/static/desktop/img/products/def-product-logo.png",
            "content": "兔小巢好好好好呀呀呀",
            "is_admin": false,
            "is_hidden": false,
            "is_liked": false,
            "is_spam": false,
            "is_pending": false,
            "is_abuse": false,
            "replies_count": null,
            "created_at": "2018-10-18 15:52:48",
            "updated_at": "2018-10-18 15:52:48",
            "time": "现在",
            "images": [],
            "like_count": null,
            "status": true,
            "user": {
                // ...
            }
        }
    },
    "created_at": "2018-10-18T15:52:48+08:00"
}

// user (后续会调整)
"user": {
    "id": 1,
    "openid": "tucao_123",
    "nickname": "tucao_test",
    "avatar": "https://txc.qq.com/static/desktop/img/products/def-product-logo.png",
    "os": " ",
    "client": " ",
    "network_type": null,
    "posts_count_last_3_months": 0,
    "posts_count": 0,
    "mark_as_good_count": "",
    "mark_as_spam_count": 0,
    "is_spam": false,
    "is_admin": null
}

// replies_all  (后续会调整)
"replies_all": {
"153984916895006730": {
        "self": {
            "id": "153984916895006730",
            "f_title_id": "153984852048553113",
            "type": 0,
            "parent_reply_id": "",
            "user_id": 2,
            "uin": "0",
            "nick_name": "tucao_test_2",
            "avatar_url": "https://txc.qq.com/static/desktop/img/products/def-product-logo.png",
            "content": "兔小巢好好好好呀呀呀",
            "is_admin": false,
            "is_hidden": false,
            "is_liked": false,
            "is_spam": false,
            "is_pending": false,
            "is_abuse": false,
            "replies_count": 0,
            "created_at": "2018-10-18 15:52:48",
            "updated_at": "2018-10-18 15:52:48",
            "time": "1秒前",
            "images": [],
            "like_count": 0,
            "status": true
        }
    }
}

4、安全相关

IP 来源限制 如果你需要对 tucao 的访问来源进行限制,目前的 IP 出口列表如下,但这些 IP 将来可能会变化,建议不设限制。

43.137.209.232

43.137.216.168

106.53.107.73

106.53.177.23

106.53.32.38