Slack-API NodeJS 机器人 response_url 不工作
Slack-API NodeJS bot response_url not working
我目前正在使用 NodeJS 开发自己的 slack 机器人。
目前还不是一个复杂的机器人,您只需键入一个命令即可得到答案。效果不错。
现在我想稍后发送一个答案,因为我的程序调用了一个外部API。
正如我在 slack-API documentation 结尾的标题“延迟响应和多重响应”中所读到的,我需要对 url 执行 post 请求] 我收到名为“response_url”
这是我在 post 请求中发送的内容:
{
"response_type": "in_channel",
"text": "My delayed text"
}
但是当我在 url 上使用此内容执行我的 post 请求时,没有任何附加...
这是我的代码中有趣的部分:
// modules
var http = require('http');
var https = require('http');
var url = require('url');
// Listen slack post request on /
app.post("/", function(req, res) {
var token = req.body.token;
var team_id = req.body.team_id;
var team_domain = req.body.team_domain;
var channel_id = req.body.channel_id;
var channel_name = req.body.channel_name;
var user_id = req.body.user_id;
var user_name = req.body.user_name;
var command = req.body.command;
var text = req.body.text;
var response_url = req.body.response_url;
if (isTokenValid(token)) {
var subcommands = text.trim().split(/\s+/);
// Check if sub-command is provided
if (subcommands.length == 0 || text == '') {
res.status(200).send('Usage: ' + USAGE);
}else { // check sub-command
switch(subcommands[0]) {
case 'test':
res.status(200).send(sendTest(response_url));
default:
res.end('Command "' + subcommands[0] + '" does not exists.\nUsage: ' + USAGE);
}
}
// Bad token
}else {
console.log('Bad token: ' + token);
res.setHeader('Content-Type', 'text/plain');
res.status(403).send('Bad token. This is a personal slack plugin owned by Ninjava team');
}
});
function sendTest(response_url) {
var options = {
host: 'my_api.com',
port: '80',
path: '/v2/content',
method: 'GET'
};
getReq(options, function(output) {
var obj = JSON.parse(output);
var myresult = obj.result;
var host = url.parse(response_url).hostname;
var path = url.parse(response_url).pathname;
var data = {
"response_type": "in_channel",
"text" : "This is the delayed message !"
};
postReq(host, path, data, true, function(output) {
console.log(output);
});
});
return "This is a direct message. Waiting for the delayed message...";
}
function getReq(options, callback) {
//making the http get call
var getReq = http.request(options, function(res) {
var output = '';
res.on('data', function(chunk) {
output += chunk;
});
res.on('end', function() {
process.stdout.write('\r');
callback(output);
});
});
//end the request
getReq.end();
getReq.on('error', function(err){
console.log("Error: ", err);
});
}
function postReq(host, path, data, ssl, callback) {
var post_data = JSON.stringify(data);
var post_options = {
host: host,
port: '80',
path: path,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
var protocol = ssl ? https : http;
var post_req = protocol.request(post_options, function(res, b, c) {
var output = '';
res.setEncoding( 'utf8' );
res.on('data', function(chunk) {
output += chunk;
});
res.on('end', function() {
callback(output);
});
});
post_req.on('error', function(err){
console.log("Error: ", err);
});
// post the data
post_req.write(post_data);
post_req.end();
}
我做了很多测试。
我使用 https://requestb.in/q9zahiq9?inspect 来测试我的机器人 return 和 post 方法(用我的 [=54= 替换 slack response_url ] url)。一切似乎都还好
我使用 PostMan 检查我的 post 请求是否被 slack-API 识别,只是在 [=] 上执行 post 请求55=] 和我在 post 开头提供的 json object,我成功地在我的松弛频道上收到了消息。
我不知道为什么 slack 不识别我的请求以及如何调试它...
提前致谢
编辑:
我解决了,请看下面我的回答
我建议你看一下 nodejslack 的代码,我构建的代码有一些与 GET 请求类似的问题,所以我在一些应该是 GET 的请求中使用了 POST 请求。听起来像是 slack api 上的错误,但我注意到当我们发送带有 JSON 主体的 GET 请求时,如果您请求没有任何数据的 GET(例如 GET 所有表情符号的列表) ) 你不会有这个问题。
如果有人感兴趣,我解决了我的问题!
我刚刚更改了请求方式:
function sendDataToSlack(response_url, data, callback) {
request({
url: response_url,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: JSON.stringify(data)
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(body);
}
else {
console.log("error: " + error)
console.log("response.statusCode: " + response.statusCode)
console.log("response.statusText: " + response.statusText)
}
});
}
而且效果很好
我目前正在使用 NodeJS 开发自己的 slack 机器人。
目前还不是一个复杂的机器人,您只需键入一个命令即可得到答案。效果不错。
现在我想稍后发送一个答案,因为我的程序调用了一个外部API。 正如我在 slack-API documentation 结尾的标题“延迟响应和多重响应”中所读到的,我需要对 url 执行 post 请求] 我收到名为“response_url”
这是我在 post 请求中发送的内容:
{
"response_type": "in_channel",
"text": "My delayed text"
}
但是当我在 url 上使用此内容执行我的 post 请求时,没有任何附加...
这是我的代码中有趣的部分:
// modules
var http = require('http');
var https = require('http');
var url = require('url');
// Listen slack post request on /
app.post("/", function(req, res) {
var token = req.body.token;
var team_id = req.body.team_id;
var team_domain = req.body.team_domain;
var channel_id = req.body.channel_id;
var channel_name = req.body.channel_name;
var user_id = req.body.user_id;
var user_name = req.body.user_name;
var command = req.body.command;
var text = req.body.text;
var response_url = req.body.response_url;
if (isTokenValid(token)) {
var subcommands = text.trim().split(/\s+/);
// Check if sub-command is provided
if (subcommands.length == 0 || text == '') {
res.status(200).send('Usage: ' + USAGE);
}else { // check sub-command
switch(subcommands[0]) {
case 'test':
res.status(200).send(sendTest(response_url));
default:
res.end('Command "' + subcommands[0] + '" does not exists.\nUsage: ' + USAGE);
}
}
// Bad token
}else {
console.log('Bad token: ' + token);
res.setHeader('Content-Type', 'text/plain');
res.status(403).send('Bad token. This is a personal slack plugin owned by Ninjava team');
}
});
function sendTest(response_url) {
var options = {
host: 'my_api.com',
port: '80',
path: '/v2/content',
method: 'GET'
};
getReq(options, function(output) {
var obj = JSON.parse(output);
var myresult = obj.result;
var host = url.parse(response_url).hostname;
var path = url.parse(response_url).pathname;
var data = {
"response_type": "in_channel",
"text" : "This is the delayed message !"
};
postReq(host, path, data, true, function(output) {
console.log(output);
});
});
return "This is a direct message. Waiting for the delayed message...";
}
function getReq(options, callback) {
//making the http get call
var getReq = http.request(options, function(res) {
var output = '';
res.on('data', function(chunk) {
output += chunk;
});
res.on('end', function() {
process.stdout.write('\r');
callback(output);
});
});
//end the request
getReq.end();
getReq.on('error', function(err){
console.log("Error: ", err);
});
}
function postReq(host, path, data, ssl, callback) {
var post_data = JSON.stringify(data);
var post_options = {
host: host,
port: '80',
path: path,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
var protocol = ssl ? https : http;
var post_req = protocol.request(post_options, function(res, b, c) {
var output = '';
res.setEncoding( 'utf8' );
res.on('data', function(chunk) {
output += chunk;
});
res.on('end', function() {
callback(output);
});
});
post_req.on('error', function(err){
console.log("Error: ", err);
});
// post the data
post_req.write(post_data);
post_req.end();
}
我做了很多测试。
我使用 https://requestb.in/q9zahiq9?inspect 来测试我的机器人 return 和 post 方法(用我的 [=54= 替换 slack response_url ] url)。一切似乎都还好
我使用 PostMan 检查我的 post 请求是否被 slack-API 识别,只是在 [=] 上执行 post 请求55=] 和我在 post 开头提供的 json object,我成功地在我的松弛频道上收到了消息。
我不知道为什么 slack 不识别我的请求以及如何调试它...
提前致谢
编辑: 我解决了,请看下面我的回答
我建议你看一下 nodejslack 的代码,我构建的代码有一些与 GET 请求类似的问题,所以我在一些应该是 GET 的请求中使用了 POST 请求。听起来像是 slack api 上的错误,但我注意到当我们发送带有 JSON 主体的 GET 请求时,如果您请求没有任何数据的 GET(例如 GET 所有表情符号的列表) ) 你不会有这个问题。
如果有人感兴趣,我解决了我的问题!
我刚刚更改了请求方式:
function sendDataToSlack(response_url, data, callback) {
request({
url: response_url,
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: JSON.stringify(data)
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(body);
}
else {
console.log("error: " + error)
console.log("response.statusCode: " + response.statusCode)
console.log("response.statusText: " + response.statusText)
}
});
}
而且效果很好