Slack 传入 Webhook API

Slack Incoming Webhook API

我可以通过 CURL POST 到 Slack 传入 API 端点,但是当尝试使用下面的方法时它不起作用。如果关闭,我假设格式化。我该如何解决这个问题?

parms = {text: text_for_slack, channel: "#customer_sessions", username: "SessionBot", icon_emoji: ":raised_hands:"}
x = Net::HTTP.post_form(URI.parse(ENV['SessionSlackURL'].to_s), parms.to_s)

您可以 post 使用两种方法(来自传入 webhook 的松弛配置的文本):

You have two options for sending data to the Webhook URL above: Send a JSON string as the payload parameter in a POST request Send a JSON string as the body of a POST request

json在正文中。

require "net/http"
require "uri"
require "json"

parms = {
    text: text_for_slack, 
    channel: "#customer_sessions", 
    username: "SessionBot", 
    icon_emoji: ":raised_hands:"
}

uri = URI.parse(ENV['SessionSlackURL'])
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri.request_uri)
request.body = parms.to_json

response = http.request(request)

json作为参数

parms_form = { 
    "payload" => {
        text: text_for_slack, 
        channel: "#customer_sessions", 
        username: "SessionBot", 
        icon_emoji:":raised_hands:"
        }.to_json
    }

request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(parms_form)