Slack Incoming Webhook API

2.9k views Asked by At

I'm able to POST to the Slack incoming API endpoint via CURL, but when trying with the below its not working. I'm assuming the formatting if off. How can I fix this?

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

There are 1 answers

1
osowskit On BEST ANSWER

You can post using two methods (text from slack configuration for incoming 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 in the body.

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 as parameter

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)