Slack API 留言
Slack API Postmessage
我正在查看 Slack API 用于教学 API 在 Ruby 中的消费。 Slack API 是否需要所有参数作为查询字符串的一部分,或者它是否也可以采用 post-Params。
像这样的留言:
def self.sendmsg(channel, msg)
url = BASE_URL + "chat.postMessage?" + "token=#{TOKEN}" + "&text=#{msg}&channel=#{channel}"
data = HTTParty.post(url)
end
然而这不是:
def self.sendmsg(channel, msg)
url = BASE_URL + "chat.postMessage?" + "token=#{TOKEN}"
data = HTTParty.post(url,
body: {
"text" => "#{msg}",
"channel" => "#{channel}"
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
end
我已经成功回答了这个问题:
data = HTTParty.post(url,
body: {
"text" => "#{msg}",
"channel" => "#{channel}",
"username" => bot_name,
"icon_url" => icon_url,
"as_user" => "false"
},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
奇怪的是,它不适用于 JSON 内容。
我认为最干净的答案就是这样:
url = 'https://someslackwebhookurl/adfdsf/sdfsfsd'
msg = 'We are fumanchu'
HTTParty.post(url, body: {"text":"#{msg}"}.to_json)
我正在查看 Slack API 用于教学 API 在 Ruby 中的消费。 Slack API 是否需要所有参数作为查询字符串的一部分,或者它是否也可以采用 post-Params。
像这样的留言:
def self.sendmsg(channel, msg)
url = BASE_URL + "chat.postMessage?" + "token=#{TOKEN}" + "&text=#{msg}&channel=#{channel}"
data = HTTParty.post(url)
end
然而这不是:
def self.sendmsg(channel, msg)
url = BASE_URL + "chat.postMessage?" + "token=#{TOKEN}"
data = HTTParty.post(url,
body: {
"text" => "#{msg}",
"channel" => "#{channel}"
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
end
我已经成功回答了这个问题:
data = HTTParty.post(url,
body: {
"text" => "#{msg}",
"channel" => "#{channel}",
"username" => bot_name,
"icon_url" => icon_url,
"as_user" => "false"
},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
奇怪的是,它不适用于 JSON 内容。
我认为最干净的答案就是这样:
url = 'https://someslackwebhookurl/adfdsf/sdfsfsd'
msg = 'We are fumanchu'
HTTParty.post(url, body: {"text":"#{msg}"}.to_json)