☰
instagram-private-api 创建群聊线程响应详解:DirectRepositoryCreateGroupThreadResponseUsersItem 接口字段全解析
2026/10/15 9:54:38 网站建设 项目流程
  • 后端

【免费下载链接】instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.

项目地址:https://gitcode.com/gh_mirrors/in/instagram-private-api
点击查看免费下载

本篇技术指南以 instagram-private-api 仓库中docs/interfaces/responses/DirectRepositoryCreateGroupThreadResponseUsersItem.md为骨架,系统讲解通过ig.direct.createGroupThread()创建 Instagram 群聊会话(Direct Thread)后,服务端返回的**用户条目(UsersItem)**数据结构的全部字段语义、嵌套类型与实战用法。读完本文,你将能够直接解析创建群聊的响应对象、按字段筛选群成员信息,并在 TypeScript 中安全地消费这类强类型响应。

接口在仓库中的定位与调用背景

在开始逐字段分析之前,先明确该接口在项目中的上下文:它并非独立存在的类型,而是DirectRepository.createGroupThread()返回的根响应对象中users数组的元素类型。

createGroupThread是 src/repositories/direct.repository.ts 中定义的低层仓库方法,向 Instagram 私有接口POST /api/v1/direct_v2/create_group_thread/发送签名表单请求:

public async createGroupThread( recipientUsers: string[], threadTitle: string, ): Promise<DirectRepositoryCreateGroupThreadResponseRootObject> { const { body } = await this.client.request.send({ url: '/api/v1/direct_v2/create_group_thread/', method: 'POST', form: this.client.request.sign({ _csrftoken: this.client.state.cookieCsrfToken, _uuid: this.client.state.uuid, _uid: this.client.state.cookieUserId, recipient_users: JSON.stringify(recipientUsers), thread_title: threadTitle, }), }); return body; }

调用时传入两个参数:recipientUsers(接收者用户 ID 字符串数组)与threadTitle(群聊标题),返回的body即DirectRepositoryCreateGroupThreadResponseRootObject。在该根对象中,users字段的类型正是DirectRepositoryCreateGroupThreadResponseUsersItem[](见 源码),也就是说,每个成功加入群聊的成员都会以本文讲解的接口形态出现在响应里。

接口定义位置与完整类型结构

本接口的 TypeScript 定义位于 src/responses/direct.repository.create-group-thread.response.ts,其完整结构如下:

export interface DirectRepositoryCreateGroupThreadResponseUsersItem { pk: number; username: string; full_name: string; is_private: boolean; profile_pic_url: string; profile_pic_id: string; friendship_status: DirectRepositoryCreateGroupThreadResponseFriendshipStatus; is_verified: boolean; has_anonymous_profile_picture: boolean; is_directapp_installed: boolean; is_using_unified_inbox_for_direct: boolean; }

整个响应文件(src/responses/direct.repository.create-group-thread.response.ts)共声明了四个相互关联的接口:

接口名职责
DirectRepositoryCreateGroupThreadResponseRootObject创建群聊的根响应,包含users、inviter、thread_id等 33 个字段
DirectRepositoryCreateGroupThreadResponseUsersItem本文主题,根响应中users数组的元素
DirectRepositoryCreateGroupThreadResponseFriendshipStatususers条目中嵌套的双方关系状态
DirectRepositoryCreateGroupThreadResponseInviter创建群聊的发起者信息

该接口在仓库的类型总览中登记于 docs/modules/responses.md(Interfaces 列表),由 TypeDoc 从源码自动生成 API 文档,因此文档与源码一一对应、字段完全一致。

字段语义逐一详解

DirectRepositoryCreateGroupThreadResponseUsersItem共声明 11 个属性,可分为四类:身份标识、个人资料、账号状态与关系状态。下面按源码声明顺序逐一说明。

身份标识字段

字段类型说明
pknumberInstagram 用户的数字主键(user id),全库通用的用户唯一标识。可用于后续ig.user.info(pk)、ig.feed.user(pk)等一切以用户为主键的调用
usernamestring用户的登录用户名(如instagram.official),不含@前缀

pk是整个响应中最常被提取的字段,因为它是与仓库中绝大多数用户级 API(AccountRepository、UserRepository、FriendshipRepository等)对接的桥梁。

个人资料字段

字段类型说明
full_namestring用户显示的全名,可为空字符串;它独立于username,是资料页展示用的昵称
profile_pic_urlstring用户头像的 CDN 图片 URL,可直接用于下载或展示
profile_pic_idstring头像媒体的唯一标识(media id)。注意它与profile_pic_url是独立的两个字段,有些接口返回中该字段可能缺失或为空

账号状态字段

字段类型说明
is_privateboolean账号是否为私密账号(private account)。私密账号无法直接查看其帖子与粉丝
is_verifiedboolean是否通过官方认证(蓝色徽章)。可用于群成员列表中对认证账号做特殊展示
has_anonymous_profile_pictureboolean是否仍在使用系统默认头像(匿名头像)。结合profile_pic_url可判断用户是否主动设置过头像
is_directapp_installedboolean该用户设备上是否安装了独立的 Direct 应用(Instagram 曾独立上架的私信应用)。对判断消息送达形态有一定参考意义
is_using_unified_inbox_for_directboolean该用户是否使用「统一收件箱」接收私信。true表示其私信与普通消息在同一个收件箱中展示

关系状态字段

字段类型说明
friendship_statusDirectRepositoryCreateGroupThreadResponseFriendshipStatus当前登录账号与该用户之间的双向关系状态,为嵌套对象,详细结构见下一节

嵌套结构:friendship_status 子接口

friendship_status的类型不是布尔值或字符串,而是完整的关系状态对象,定义于同一响应文件的 第 51-59 行:

export interface DirectRepositoryCreateGroupThreadResponseFriendshipStatus { following: boolean; blocking: boolean; is_private: boolean; incoming_request: boolean; outgoing_request: boolean; is_bestie: boolean; is_restricted: boolean; }

各字段含义如下:

字段类型业务含义
followingboolean当前登录账号是否已关注该成员
blockingboolean当前账号是否已拉黑该成员
is_privateboolean该成员是否为私密账号(与顶层is_private冗余但含义一致)
incoming_requestboolean该成员是否向当前账号发来过关注请求
outgoing_requestboolean当前账号是否已向该成员发出待处理关注请求
is_bestieboolean该成员是否处于当前账号的「密友(Besties)」列表中
is_restrictedboolean该成员是否被当前账号「限制(Restricted)」

这些字段组合起来可以直接支撑"群成员亲疏程度"的判定逻辑,例如:following && is_bestie为true时,可以认为该成员与登录账号关系紧密;blocking为true时则应在 UI 中做特殊处理。

在根响应对象中的位置与数据流

users数组只是根响应 33 个字段之一。完整的根对象结构定义于 src/responses/direct.repository.create-group-thread.response.ts,其 TypeDoc 页面为 docs/interfaces/responses/DirectRepositoryCreateGroupThreadResponseRootObject.md。与群成员直接相关的关键字段包括:

  • thread_id/thread_v2_id:新建群聊的线程 ID(两代版本),后续向该群发消息需用到;
  • users:群成员列表,元素即本文讲解的接口;
  • left_users:已离开的成员列表;
  • admin_user_ids:管理员用户 ID 数组(number[]),可用来与users[i].pk交叉比对判断谁是管理员;
  • inviter:发起建群者的资料(DirectRepositoryCreateGroupThreadResponseInviter);
  • is_group、named、thread_title:群聊属性(是否为群、是否有名称、群标题);
  • approval_required_for_new_members:新成员是否需要审批。

典型的数据流是:createGroupThread(recipientUsers, threadTitle)返回根对象 → 通过users拿到成员列表 → 用user.pk、user.username、user.friendship_status等字段完成群成员信息的渲染、筛选或后续 API 调用。

实战示例:解析并消费群成员数据

结合 examples 目录的工程风格(先ig.state.generateDevice()再ig.account.login()),一个完整的消费示例:

import { IgApiClient } from 'instagram-private-api'; const ig = new IgApiClient(); ig.state.generateDevice(process.env.IG_USERNAME); // 可选:ig.state.proxyUrl = process.env.IG_PROXY; async function main() { await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD); const recipientUsers = ['user_pk_1', 'user_pk_2', 'user_pk_3']; const thread = await ig.direct.createGroupThread(recipientUsers, '项目讨论群'); // thread.users 的元素类型即 DirectRepositoryCreateGroupThreadResponseUsersItem for (const user of thread.users) { console.log({ pk: user.pk, username: user.username, full_name: user.full_name, is_private: user.is_private, is_verified: user.is_verified, following: user.friendship_status.following, is_bestie: user.friendship_status.is_bestie, avatar: user.profile_pic_url, }); } // 用 admin_user_ids 与 users 交叉比对,找出群管理员 const admins = thread.users.filter((u) => thread.admin_user_ids.includes(u.pk)); console.log('admins:', admins.map((u) => u.username)); // 拿到 thread_id 后即可通过 DirectThreadEntity 在群内发消息 return thread.thread_id; } main();

注意:ig.direct即DirectRepository在IgApiClient上的实例(命名遵循 README 中"去掉Repository后缀、转为 camelCase"的约定,详见 README 的 Repositories 章节)。创建成功后的群聊线程还可以通过 src/entities/direct-thread.entity.ts 的DirectThreadEntity继续执行broadcastText、broadcastPhoto、addUser、updateTitle等操作,实现完整的群聊自动化流程。

类型安全与使用注意事项

  • 字段可能缺失:DirectRepositoryCreateGroupThreadResponseUsersItem是全必填的严格接口,但 Instagram 服务端在不同网络或账号场景下可能返回缺字段的 JSON。若对接不稳定,建议在消费前用'profile_pic_id' in user或可选链做防御性判断。
  • pk精度问题:Instagram 的用户pk数值很大,TypeScript 的number类型在严格场景下可能损失精度;若需把pk序列化到字符串键或与字符串 ID 混用,建议先String(pk)转换。
  • 响应类型仅用于解析:本接口及同类*Response*接口(完整清单见 docs/modules/responses.md)仅用于标注响应体结构,不应作为请求表单类型使用;请求侧参数应使用 src/types 目录下的*Options类型。
  • 低层方法 vs 高层实体:createGroupThread是一次请求一个响应的低层仓库方法(仓库方法遵循"每次方法恰好发送一个 API 请求"的约定);若要做"建群 + 发首条消息"的组合操作,可结合DirectThreadEntity或SimulateService高层服务编排,见 docs/modules/services.md。

小结

DirectRepositoryCreateGroupThreadResponseUsersItem是 instagram-private-api 创建群聊场景下描述"单个群成员"的标准类型:顶层承载用户身份、资料与账号状态,嵌套的friendship_status则补充双向关系细节。理解它的字段语义,配合 src/repositories/direct.repository.ts 的请求入口与根对象结构,即可在 TypeScript 项目中安全地完成建群、成员筛选、管理员识别与后续群聊消息发送的完整开发闭环。

  • 后端

【免费下载链接】instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.

项目地址:https://gitcode.com/gh_mirrors/in/instagram-private-api
点击查看免费下载
上一篇:深入浅出 NYU-DLSP20 优化方法(二):自适应学习率、归一化层与"优化的终结"——从 RMSprop/ADAM 到加速 MRI 重建实战
下一篇:Sharp-dumpkey 1分钟快速提取微信数据库AES密钥:解锁你的本地聊天记录

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询