将 slack webhook 与节点一起使用
Using slack webhook with node
我正在尝试使用 slack webhook。我可以阅读很多关于我应该如何进行的变体,但直到现在,none 其中的工作正常。
我正在使用 request
节点模块进行 api 调用,但如果需要我可以更改。
首先尝试关注
import request from 'request';
const url = 'https://hooks.slack.com/services/xxx';
const text = '(test)!';
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
payload : JSON.stringify({ text }),
},
(error, res, body) => console.log(error, body, res.statusCode)
);
我得到:null 400 'invalid_payload'
接下来尝试关注
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ text }),
},
(error, res, body) => console.log(error, body, res.statusCode)
);
这次成功了,但 Slack 显示:%28test%29%21
而不是 (test)!
我是不是漏掉了什么?
你可以试试 slack-node 模块,将 post 包装到钩子上。这是我用来为 AWS 实例推送通知的简化修改的真实世界示例。
[编辑] 更改为使用您的文字
现在,使用 slack-node,您自己 assemble {},自己添加文本:和其他参数并将其传递给 .webhook
const Slack = require('slack-node');
const webhookUri = 'https://hooks.slack.com/services/xxxxx';
const slack = new Slack();
slack.setWebhook(webhookUri);
text = "(test)!"
slack.webhook({
text: text
// text: JSON.stringify({ text })
}, function(err, response) {
console.log(err, response);
});
根据您的第二个示例和工作邮递员请求,这就是我让它工作的方式,请原谅我的更改要求,因为我现在是 运行 较旧的节点版本。我不确定您想要 post 到 Slack 的数据是什么样子,这可能会改变您想要 assemble 的方式。
const request = require('request');
const url = 'https://hooks.slack.com/services/xxxxx';
const text = '(test)!';
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : {payload: JSON.stringify({ text } )}
},
(error, res, body) => console.log(error, body, res.statusCode)
);
如果你想使用请求,你可能想检查 slack-node 如何 posting 数据,这里是从 slack-node
中截取的相关内容
Slack.prototype.webhook = function(options, callback) {
var emoji, payload;
emoji = this.detectEmoji(options.icon_emoji);
payload = {
response_type: options.response_type || 'ephemeral',
channel: options.channel,
text: options.text,
username: options.username,
attachments: options.attachments,
link_names: options.link_names || 0
};
payload[emoji.key] = emoji.val;
return request({
method: "POST",
url: this.webhookUrl,
body: JSON.stringify(payload),
timeout: this.timeout,
maxAttempts: this.maxAttempts,
retryDelay: 0
}, function(err, body, response) {
if (err != null) {
return callback(err);
}
return callback(null, {
status: err || response !== "ok" ? "fail" : "ok",
statusCode: body.statusCode,
headers: body.headers,
response: response
});
});
};
我终于选择了比 slack-node 更喜欢的 Slack-webhook。 d parolin 的解决方案是我问题的最佳答案,但我想提及 pthm 的工作以完成。
我正在尝试使用 slack webhook。我可以阅读很多关于我应该如何进行的变体,但直到现在,none 其中的工作正常。
我正在使用 request
节点模块进行 api 调用,但如果需要我可以更改。
首先尝试关注
import request from 'request';
const url = 'https://hooks.slack.com/services/xxx';
const text = '(test)!';
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
payload : JSON.stringify({ text }),
},
(error, res, body) => console.log(error, body, res.statusCode)
);
我得到:null 400 'invalid_payload'
接下来尝试关注
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : JSON.stringify({ text }),
},
(error, res, body) => console.log(error, body, res.statusCode)
);
这次成功了,但 Slack 显示:%28test%29%21
而不是 (test)!
我是不是漏掉了什么?
你可以试试 slack-node 模块,将 post 包装到钩子上。这是我用来为 AWS 实例推送通知的简化修改的真实世界示例。
[编辑] 更改为使用您的文字
现在,使用 slack-node,您自己 assemble {},自己添加文本:和其他参数并将其传递给 .webhook
const Slack = require('slack-node');
const webhookUri = 'https://hooks.slack.com/services/xxxxx';
const slack = new Slack();
slack.setWebhook(webhookUri);
text = "(test)!"
slack.webhook({
text: text
// text: JSON.stringify({ text })
}, function(err, response) {
console.log(err, response);
});
根据您的第二个示例和工作邮递员请求,这就是我让它工作的方式,请原谅我的更改要求,因为我现在是 运行 较旧的节点版本。我不确定您想要 post 到 Slack 的数据是什么样子,这可能会改变您想要 assemble 的方式。
const request = require('request');
const url = 'https://hooks.slack.com/services/xxxxx';
const text = '(test)!';
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : {payload: JSON.stringify({ text } )}
},
(error, res, body) => console.log(error, body, res.statusCode)
);
如果你想使用请求,你可能想检查 slack-node 如何 posting 数据,这里是从 slack-node
中截取的相关内容Slack.prototype.webhook = function(options, callback) {
var emoji, payload;
emoji = this.detectEmoji(options.icon_emoji);
payload = {
response_type: options.response_type || 'ephemeral',
channel: options.channel,
text: options.text,
username: options.username,
attachments: options.attachments,
link_names: options.link_names || 0
};
payload[emoji.key] = emoji.val;
return request({
method: "POST",
url: this.webhookUrl,
body: JSON.stringify(payload),
timeout: this.timeout,
maxAttempts: this.maxAttempts,
retryDelay: 0
}, function(err, body, response) {
if (err != null) {
return callback(err);
}
return callback(null, {
status: err || response !== "ok" ? "fail" : "ok",
statusCode: body.statusCode,
headers: body.headers,
response: response
});
});
};
我终于选择了比 slack-node 更喜欢的 Slack-webhook。 d parolin 的解决方案是我问题的最佳答案,但我想提及 pthm 的工作以完成。