Rails 中的 Slack Webhook

Slack webhook in Rails

我正在打开一个 Rails 控制台,尝试使用 HTTParty 通过网络挂钩将消息发送到松弛通道。

当我使用 Postman 时一切正常,但当我尝试 HTTParty 时它不起作用。在 Postman 中,我简单地 POST 到 url 与表单数据 keypayloadvalue 的:

{"channel" => "#some_channel", "username" => "webhookbot", "text" => "Testing message", "icon_emoji" => ":ghost:"}

在 rails 控制台中,我正在尝试:

HTTParty.post(webhook_url, body: {"channel" => "#some_channel", "username" => "webhookbot", "text" => "Testing message", "icon_emoji" => ":ghost:"}, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded'})

我收到此错误消息:

Errno::ECONNREFUSED: Connection refused - connect(2) for "hooks.slack.com" port 443

这是代理问题吗?

试试这个:

HTTParty.post(webhook_url,
  body: {
    "channel" => "#some_channel",
    "username" => "webhookbot",
    "text" => "Testing message",
    "icon_emoji" => ":ghost:"
  }.to_json,
  headers: { 
    'Content-Type' => 'application/json'
  }
)