使用Rocket.Chat API'chat.postMessage'命令时出现“无效频道”错误
我的代码如下所示,任何人都可以在这里看到我的错吗?谢谢!
我为代码中的频道名称尝试了不同的格式(顺便说一下,这是一个公共频道,它也是创建的初始频道,并且具有通用ID)
"GENERAL"
"@GENERAL"
"#GENERAL"
“My_Chat_Room”
“@My_Chat_Room”
“#My_Chat_Room”
以上所有内容,在'#'或'@'后面有空格,并且所有这些都用小写字母
function postMsg(){
var request = require(‘request’);
var url = ‘https://my.rocketchat.server/api/v1/login/’;
var user = ‘Test_User’;
var pass = ‘DFSdnwe89eq339’;
// Save these for future requests
var userId;
var authToken;
request.post(
{
uri: url,
form: { username: user, password: pass }
},
function(err, httpResponse, body) {
if (err) {
return console.error(‘Login failed:’, err);
}
var json = JSON.parse(body);
authToken = json.data.authToken;
userId = json.data.userId;
console.log(‘Login successful!:’ + “\n” + "User ID: " + userId + “\n” + "Auth Token: " + authToken);
var postMsgUrl = 'https://my.rocketchat.server/api/v1/chat.postMessage';
request.post({
uri: postMsgUrl,
headers: {
'X-User-Id': userId,
'X-Auth-Token': authToken
},
data: {
"channel": "My_Chat_Room",
"text": "test message",
}
}, function(err, httpResponse, body) {
if (err) {
console.log('post failed:', err, body);
}
console.log('Server responded with:', httpResponse, body);
});
}
)}
预期结果是请求中指定的文本被发布到频道,但是我得到的只是“无效频道”错误
答案 0 :(得分:0)
您需要告诉火箭您的请求具有json负载。
添加此标头:"Content-type:application/json"