Module: PgReports::TelegramSender

Defined in:
lib/pg_reports/telegram_sender.rb

Overview

Sends reports to Telegram using telegram-bot-ruby gem

Class Method Summary collapse

Class Method Details

.send_file(content, filename:, caption: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pg_reports/telegram_sender.rb', line 20

def send_file(content, filename:, caption: nil)
  ensure_configured!
  ensure_telegram_gem!

  # Create a temporary file
  temp_file = Tempfile.new([File.basename(filename, ".*"), File.extname(filename)])
  begin
    temp_file.write(content)
    temp_file.rewind

    bot.api.send_document(
      chat_id: chat_id,
      document: Faraday::UploadIO.new(temp_file.path, "text/plain", filename),
      caption: caption&.truncate(1024)
    )
  ensure
    temp_file.close
    temp_file.unlink
  end
end

.send_message(text, parse_mode: "Markdown") ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/pg_reports/telegram_sender.rb', line 9

def send_message(text, parse_mode: "Markdown")
  ensure_configured!
  ensure_telegram_gem!

  bot.api.send_message(
    chat_id: chat_id,
    text: truncate_message(text),
    parse_mode: parse_mode
  )
end

.send_report(report) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/pg_reports/telegram_sender.rb', line 41

def send_report(report)
  if report.to_text.length > 4000
    send_file(report.to_text, filename: report_filename(report), caption: report.title)
  else
    send_message(report.to_markdown)
  end
end