Slack API Postmessage

时间:2016-10-27 18:03:07

标签: ruby api httparty slack

我正在研究Slack API,用于教授Ruby中的API消耗。 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

2 个答案:

答案 0 :(得分:0)

我设法回答了这个问题:

    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内容。

答案 1 :(得分:0)

我认为最干净的答案就是:

url = 'https://someslackwebhookurl/adfdsf/sdfsfsd'
msg = 'We are fumanchu'
HTTParty.post(url, body: {"text":"#{msg}"}.to_json)