Module: OpenAI::Utils

Included in:
OpenAIBot
Defined in:
lib/open_ai/utils.rb

Instance Method Summary collapse

Instance Method Details

#attempt(times, exception = Net::ReadTimeout) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/open_ai/utils.rb', line 5

def attempt(times, exception=Net::ReadTimeout)
  retries ||= 0
  yield
rescue exception => e
  retries += 1
  if retries < times
    retry
  else
    reply(e.message, parse_mode: "Markdown")
  end
end

#download_file(voice, dir = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/open_ai/utils.rb', line 17

def download_file(voice, dir=nil)
  return unless voice

  file_path = @api.get_file(file_id: voice.file_id).file_path

  url = "https://api.telegram.org/file/bot#{config.token}/#{file_path}"

  file = Down.download(url)
  dir ||= "."

  FileUtils.mkdir(dir) unless Dir.exist? dir
  FileUtils.mv(file.path, "#{dir.delete_suffix("/")}/#{file.original_filename}")
  file
end