Slack Incoming Webhook API

时间:2015-06-18 09:03:02

标签: ruby-on-rails slack-api

我能够通过CURL向Slack传入的API端点发送POST,但是当尝试使用下面的它不能正常工作时。如果关闭,我会假设格式化。我该如何解决这个问题?

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)

1 个答案:

答案 0 :(得分:11)

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

  

您可以通过两种方式将数据发送到上面的Webhook网址:   在POST请求中发送JSON字符串作为payload参数   发送JSON字符串作为POST请求的主体

身体中的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)