直达内容

创建会话

POST /conversations

新建群组或机器人第一次与用户对话之前,需要调用该 API,确保先创建会话。

API 端点 URL

 https://api.mixin.one/conversations

授权和其他选项

授权
Authorized
限制No limitation

请求体

{
"category": "'GROUP' or 'CONTACT'",
"conversation_id": "Unique identifier, see below to generate.",
"name": "Group name, valid when category is 'GROUP', 512 characters at most",
"participants": "Member list '[{ user_id: UUID }]', up to 256 people.",
}
信息

您创建的会话是您的 机器人或者 dApp 与普通 Mixin Messenger 用户之间的会话。 您不能使用用户的 access_token 来创建它们。 请使用机器人或者 dApp 的 Access Token 创建会话

信息

当 category 为CONTACT时,参与者的值应该是一个长度等于 1 的数组,user_id 的值为对方的 user_id。

请求示例

curl -i -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" https://api.mixin.one/conversations --data PAYLOAD
Responses
{
"data": {
"type": "conversation",
"conversation_id": "928c5c40-769c-3e97-8387-fb1ae0645311",
"creator_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"category": "GROUP",
"name": "",
"icon_url": "",
"announcement": "",
"created_at": "2018-05-16T12:34:44.134238105Z",
"code_id": "d8244b92-30e9-44b5-bfb0-ce597c788125",
"code_url": "https://mixin.one/codes/d8244b92-30e9-44b5-bfb0-ce597c788125",
"mute_until": "2018-05-16T12:34:44.143010035Z",
"participants": [
{
"type": "participant",
"user_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"role": "OWNER",
"created_at": "2018-05-16T12:34:44.134238105Z"
},
{
"type": "participant",
"user_id": "e8e5b807-fa8b-455a-8dfa-b189d28310ff",
"role": "",
"created_at": "2018-05-16T12:34:44.149277666Z"
},
...
],
"participant_sessions": [
{
"user_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"session_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
},
...
],
"circles": [
{
"circle_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"conversation_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"user_id": "8dcf823d-9eb3-4da2-8734-f0aad50c0da6",
"created_at": "2018-05-16T12:34:44.149277666Z"
},
...
]
}
}

生成唯一会话 ID

单聊: category = "CONTACT"

会话的唯一标识是根据双方生成的,在 Go 中这样实现:

func UniqueConversationId(userId, recipientId string) string {
minId, maxId := userId, recipientId
if strings.Compare(userId, recipientId) > 0 {
maxId, minId = userId, recipientId
}
h := md5.New()
io.WriteString(h, minId)
io.WriteString(h, maxId)
sum := h.Sum(nil)
sum[6] = (sum[6] & 0x0f) | 0x30
sum[8] = (sum[8] & 0x3f) | 0x80
return uuid.FromBytesOrNil(sum).String()
}

群聊: category = "GROUP"

golang
uuid.NewV4().String()
swift
UUID().uuidString.lowercased()

请注意,UUID 字符串会无例外地转换为小写