在节点上使用松弛Webhook

沙古

我正在尝试使用松弛的Webhook。我可以阅读有关如何进行的很多变化,但是直到现在,它们都无法正常工作。

我正在使用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)!

我错过了什么?

d帕罗林

根据您的第二个示例和工作正常的Postman的要求,这就是我的工作方式,请原谅我由于正在运行较旧的节点版本而需要进行的更改。我不确定要发布到Slack的数据是什么样子,这可能会改变组装方式。

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发布数据的方式,此处从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
      });
    });
  };

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章