Class: GrubY::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/gruubY/media.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Media

Returns a new instance of Media.



5
6
7
# File 'lib/gruubY/media.rb', line 5

def initialize(token)
  @base = "https://api.telegram.org/bot#{token}/"
end

Instance Method Details

#post_file(method, field_name, chat_id, file) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gruubY/media.rb', line 21

def post_file(method, field_name, chat_id, file)
  uri = URI(@base + method)

  File.open(file, "rb") do |io|
    req = Net::HTTP::Post.new(uri)
    form_data = [
      ["chat_id", chat_id],
      [field_name, io]
    ]

    req.set_form form_data, "multipart/form-data"
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(req)
    end
  end
end

#send_audio(chat_id, file) ⇒ Object



17
18
19
# File 'lib/gruubY/media.rb', line 17

def send_audio(chat_id, file)
  post_file("sendAudio", "audio", chat_id, file)
end

#send_photo(chat_id, file) ⇒ Object



9
10
11
# File 'lib/gruubY/media.rb', line 9

def send_photo(chat_id, file)
  post_file("sendPhoto", "photo", chat_id, file)
end

#send_video(chat_id, file) ⇒ Object



13
14
15
# File 'lib/gruubY/media.rb', line 13

def send_video(chat_id, file)
  post_file("sendVideo", "video", chat_id, file)
end