能力介绍
私信、群消息管理能力,是抖音开放平台基于抖音及抖音极速版即时通讯基础功能,提供的接口&数据开放服务,支持抖音内的经营类账号通过授权第三方应用获取及发送抖音私信、群消息,并支持丰富的富媒体消息类型,包括不限于文字、图片、视频、消息卡片等。
接入流程
接入前准备
应用入驻
- 开发者需要入驻抖音开放平台,创建一个以消息管理为核心功能场景之一的网站应用,获取应用clientkey
接入抖音授权
- 目前消息管理相关能力仅支持web扫码授权
接口列表
OpenAPI 列表
请求域名 https://open.douyin.com
OpenAPI | 描述 | 对应Scope权限 | 权限名 | 须用户授权 |
---|---|---|---|---|
发送抖音私信消息 | im.direct_message | 私信消息管理:接收和发送抖音私信消息 | 是 | |
发送抖音群聊消息 | im.group_message | 群消息管理:接收和发送抖音群聊消息 | 是 | |
撤回私信、群聊消息 | im.recall_message | 消息撤回管理 | 是 | |
创建/更新留资卡片 | im.message_card | 消息卡片管理 | 是 | |
查询留资卡片 | im.message_card | 消息卡片管理 | 是 | |
删除留资卡片 | im.message_card | 消息卡片管理 | 是 | |
创建/修改小程序引导卡片模板 | im.microapp_card | 小程序引导卡片模板管理 | 否 | |
查询小程序引导卡片模板 | im.microapp_card | 小程序引导卡片模板管理 | 否 | |
删除小程序引导卡片模板 | im.microapp_card | 小程序引导卡片模板管理 | 否 | |
上传图片工具 | tool.image.upload | 上传图片 | 否 |
webhook 事件订阅
Webhook | 事件名 | Scope权限 | 权限名 | 须用户授权 |
---|---|---|---|---|
im_send_msg | im.direct_message | 私信消息管理:接收和发送抖音私信消息 | 是 | |
im_receive_msg | im.direct_message | 私信消息管理:接收和发送抖音私信消息 | 是 | |
im_enter_direct_msg | im.direct_message | 私信消息管理:接收和发送抖音私信消息 | 是 | |
im_group_send_msg | im.group_message | 群消息管理:接收和发送抖音群聊消息 | 是 | |
im_group_receive_msg | im.group_message | 群消息管理:接收和发送抖音群聊消息 | 是 |
开发代码展示:
$daid = $this->request->getIntParam('daid', 0);
//应用类型输出
$where = [
['name' => 'cl_ds_id', 'oper' => '=', 'value' => $this->sid],
];
if (!empty($daid)) {
$where[] = ['name' => 'dl_qyh_uid', 'oper' => '=', 'value' => $daid];
}
$this->output['enter_id'] = $daid;
$sort = ['cl_create_time' => 'DESC'];
$chat_list_model = new App_Model_Douyin_MysqlChatListStorage();
$chat_result = $chat_list_model->getList($where, $this->index, $this->count, $sort);
$intent_model = new App_Model_Douyin_MysqlIntentUserStorage();
#$account_model = new App_Model_Douyin_MysqlDyAccountStorage();
$chat_list = [];
foreach ($chat_result as $item) {
#$account = $account_model->getRowByIdSid($item['cl_qyh_uid'], $this->sid);
$intention = $intent_model->getUserByOpenId($this->sid, $item['cl_from_openid']);
$each = [
'user_nickname' => empty($intention) ? '匿名' : $intention['iu_nickname'],
'user_avatar' => empty($intention) ? parent::TEMPLATE_PLACEHOLDER_IMAGE : $intention['iu_avatar'],
'user_newmsg' => $item['cl_new_text'],
'user_newtime' => date('Y-m-d H:i:s', $item['cl_new_time']),
'user_openid' => $item['cl_from_openid'],
'qyh_uid' => $item['cl_qyh_uid'],
'undo_count' => $item['cl_undo_count'],
];
array_push($chat_list, $each);
}
$this->displayJson($chat_list);
$from_openid = $this->request->getStrParam('from_openid');
$qyh_uid = $this->request->getIntParam('qyh_uid');
$letter_model = new App_Model_Douyin_MysqlLetterStorage();
$detail_result = $letter_model->getChatListOrder($from_openid, $qyh_uid, $this->sid, $this->index, $this->count);
$chat_detail = [];
foreach ($detail_result as $item) {
$each = [
'msg_type' => $item['dl_msg_type'],
'msg_content' => $item['dl_msg_content'],
'msg_time' => date('Y-m-d H:i:s', $item['dl_create_time']),
'send_receive' => intval($item['dl_send_receive']), //1收到的消息,2发送的消息
];
array_push($chat_detail, $each);
}
$account_model = new App_Model_Douyin_MysqlDyAccountStorage();
$qyh_account = $account_model->getRowByIdSid($qyh_uid, $this->sid);
$intent_model = new App_Model_Douyin_MysqlIntentUserStorage();
$from_account = $intent_model->getUserByOpenId($this->sid, $from_openid, $qyh_uid);
$return_data = [
'from_user' => [
'nickname' => empty($from_account) ? '匿名' : $from_account['iu_nickname'],
'avatar' => empty($from_account) ? parent::TEMPLATE_PLACEHOLDER_IMAGE : $from_account['iu_avatar'],
'openid' => $from_openid,
],
'to_user' => [
'nickname' => $qyh_account['da_nickname'],
'avatar' => $qyh_account['da_avatar'],
'qyh_uid' => $qyh_uid,
],
'chat_detail' => $chat_detail
];
$chat_list_model = new App_Model_Douyin_MysqlChatListStorage();
$cl_where = [
['name' => 'cl_ds_id', 'oper' => '=', 'value' => $this->sid],
['name' => 'cl_qyh_uid', 'oper' => '=', 'value' => $qyh_uid],
['name' => 'cl_from_openid', 'oper' => '=', 'value' => $from_openid],
];
$chat_list_model->updateValue(['cl_undo_count' => 0], $cl_where);
$this->displayJson($return_data);