【问题标题】:Slack API PostmessageSlack API 消息传递
【发布时间】:2016-10-27 18:03:06
【问题描述】:

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

【问题讨论】:

    标签: ruby api httparty slack


    【解决方案1】:

    我认为最简洁的答案是这样的:

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

    【讨论】:

      【解决方案2】:

      我已经设法回答了这个问题:

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

      【讨论】:

        猜你喜欢
        • 2015-10-04
        • 2017-07-12
        • 1970-01-01
        • 2022-12-10
        • 2022-01-12
        • 2015-10-02
        • 2018-06-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多