Module: Slack::API::Chat

Extended by:
Chat
Included in:
Chat
Defined in:
lib/slack-wrapper/api/chat.rb

Instance Method Summary collapse

Instance Method Details

#delete(ts, channel) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slack-wrapper/api/chat.rb', line 38

def delete(ts, channel)
  if Slack::API::Auth
    text = URI.escape(text)
    uri  = URI.parse('https://slack.com/api/chat.delete')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&as_user=true&ts=#{ts}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#post(text, channel, obj = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/slack-wrapper/api/chat.rb', line 57

def post(text, channel, obj=nil)
  if Slack::API::Auth
    text = URI.escape(text)
    uri  = URI.parse('https://slack.com/api/chat.postMessage')
    case obj
      when nil
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true"
      when Array
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true&attachments=#{URI.escape(obj.to_json)}"
      else
        Slack::Errors.new({'error'  => 'invalid_attachment',
                           'detail' => 'Attachment object must be an Array'})
    end
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new(url)
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#post_ephemeral(text, channel, user, obj = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/slack-wrapper/api/chat.rb', line 104

def post_ephemeral(text, channel, user, obj=nil)
  if Slack::API::Auth
    text = URI.escape(text)
    uri  = URI.parse('https://slack.com/api/chat.postEphemeral')
    case obj
      when nil
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true&user=#{user}"
      when Array
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true&user=#{user}&attachments=#{URI.escape(obj.to_json)}"
      else
        Slack::Errors.new({'error'  => 'invalid_attachment',
                           'detail' => 'Attachment object must be an Array'})
    end
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new(url)
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#post_me(text, channel) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/slack-wrapper/api/chat.rb', line 85

def post_me(text, channel)
  if Slack::API::Auth
    text = URI.escape(text)
    uri  = URI.parse('https://slack.com/api/chat.meMessage')
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new("#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}")
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end

#update(text, ts, channel, obj = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/slack-wrapper/api/chat.rb', line 10

def update(text, ts, channel, obj=nil)
  if Slack::API::Auth
    text = URI.escape(text)
    uri  = URI.parse('https://slack.com/api/chat.update')
    case obj
      when nil
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true&ts=#{ts}"
      when Array
        url = "#{uri.to_s}?token=#{Slack::Config.token}&channel=#{channel}&text=#{text}&as_user=true&attachments=#{URI.escape(obj.to_json)}&ts=#{ts}"
      else
        Slack::Errors.new({'error'  => 'invalid_attachment',
                           'detail' => 'Attachment object must be an Array'})
    end
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req  = Net::HTTP::Post.new(url)
    resp = http.request(req)
    false unless resp.code == 200
    if JSON.parse(resp.body)['ok']
      true
    else
      Slack::Errors.new(JSON.parse(resp.body))
    end
  else
    Slack::Errors.new({"error" => "not_authed"})
  end
end