如何通过 API 分配主题

是否可以通过 API 进行分配?我在文档中没有看到,但我很乐意被惊喜 :slight_smile:

编辑:看起来我将尝试逆向工程

3 个赞

首先,我们的目标是拥有一个稳定的API,这是一项需要多年努力才能实现的目标,但毫无疑问是我们希望达到的地方。

在此期间,一个合理的2025年技巧是依靠AI代理来解决这些问题:

这展示了如何依靠我们的GitHub助手来回答问题!

4 个赞

你弄明白了吗?如果弄明白了,可以在这里分享你的经验吗?

1 个赞

嘿!
还没有!这个项目因为更紧急的事情被推迟了,但我下周会重新开始做 :slight_smile:

4 个赞

@tobiaseigen 这在我的 node js 上运行正常

var https = require(‘https’);

// 配置
var CONFIG = {
apiUrl: ‘YOURDISCOURSEDOMAIN’,
apiKey: ‘YOURAPIKEY’,
apiUsername: ‘YOURAPIUSER’,
assignToUsername: ‘USERNAMETOASSIGNTO’  // 要分配话题的用户名
};

// 要分配的话题 ID
var topicIds = [634]; // 用话题 ID 更改

function assignTopic(topicId, callback) {
var postData = JSON.stringify({
target_id: topicId,
target_type: ‘Topic’,
username: CONFIG.assignToUsername
});

var options = {
hostname: CONFIG.apiUrl,
port: 443,
path: ‘/assign/assign.json’,
method: ‘PUT’,
rejectUnauthorized: false,
headers: {
‘Api-Key’: CONFIG.apiKey,
‘Api-Username’: CONFIG.apiUsername,
‘Content-Type’: ‘application/json’,
‘Content-Length’: postData.length
}
};

var req = https.request(options, function(res) {
var data = ‘’;

res.on('data', function(chunk) {
  data += chunk;
});

res.on('end', function() {
  console.log('状态:', res.statusCode);
  console.log('原始响应:', data);
  try {
    var response = JSON.parse(data);
    if (res.statusCode === 200) {
      console.log('话题 ' + topicId + ' 分配成功');
    } else {
      console.log('话题 ' + topicId + ' 错误:', response.errors || response.error_type || response);
    }
  } catch (e) {
    console.log('解析错误:', e.message);
  }
  if (callback) callback();
});

});

req.on(‘error’, function(e) {
console.error('话题 ' + topicId + ' 的请求错误:', e);
if (callback) callback();
});

req.write(postData);
req.end();
}

// 顺序运行
var index = 0;
function next() {
if (index < topicIds.length) {
assignTopic(topicIds[index], function() {
index++;
setTimeout(next, 500);
});
}
}

console.log('正在分配话题:', topicIds.join(‘, ‘));
console.log('分配给:', CONFIG.assignToUsername);
console.log(’’);
next();
1 个赞

Discourse Assign 暴露了以下 API 端点:

分配 (PUT /assign/assign.json)

必需参数:

  • target_id - 主题或帖子 ID
  • target_type - 必须是 "Topic""Post"

外加以下之一:

  • username - 要分配的用户名
  • group_name - 要分配的组名

可选参数:

  • note - 分配备注
  • status - 分配状态
  • should_notify - 发送通知 (默认值: true)

取消分配 (PUT /assign/unassign.json)

必需参数:

  • target_id - 主题或帖子 ID
  • target_type - 必须是 "Topic""Post"

示例 (curl)

# 将主题 123 分配给用户 "john"
curl -X PUT "https://your-discourse.com/assign/assign.json" \
  -H "Api-Key: YOUR_API_KEY" \
  -H "Api-Username: YOUR_USERNAME" \
  -H "Content-Type: application/json" \
  -d '{"target_id": 123, "target_type": "Topic", "username": "john"}'

# 分配给一个组而不是用户
curl -X PUT "https://your-discourse.com/assign/assign.json" \
  -H "Api-Key: YOUR_API_KEY" \
  -H "Api-Username: YOUR_USERNAME" \
  -H "Content-Type: application/json" \
  -d '{"target_id": 123, "target_type": "Topic", "group_name": "support-team"}'

备注

  • API 用户必须具有分配权限(位于允许分配的组中)
  • 您也可以使用 target_type: "Post" 和帖子 ID 来分配单个帖子

@opcourdis 上面的 Node.js 示例看起来是正确的!:+1:

2 个赞

感谢分享,很高兴看到我们可以包含所有这些参数