Class: Heathrow::Sources::Telegram

Inherits:
Object
  • Object
show all
Defined in:
lib/heathrow/sources/telegram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Telegram

Returns a new instance of Telegram.



14
15
16
17
18
19
# File 'lib/heathrow/sources/telegram.rb', line 14

def initialize(source)
  @source = source
  @config = source.config.is_a?(String) ? JSON.parse(source.config) : source.config
  @last_fetch_time = Time.now
  @last_message_id = nil
end

Instance Attribute Details

#last_fetch_timeObject (readonly)

Returns the value of attribute last_fetch_time.



12
13
14
# File 'lib/heathrow/sources/telegram.rb', line 12

def last_fetch_time
  @last_fetch_time
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/heathrow/sources/telegram.rb', line 12

def source
  @source
end

Instance Method Details

#authenticateObject



56
57
58
59
60
61
62
63
64
# File 'lib/heathrow/sources/telegram.rb', line 56

def authenticate
  if @config['api_id'] && @config['api_hash'] && @config['phone_number']
    authenticate_mtproto
  else
    puts "For user account access, configure api_id, api_hash, and phone_number"
    puts "For bot access, configure bot_token"
    false
  end
end

#can_reply?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/heathrow/sources/telegram.rb', line 66

def can_reply?
  true
end

#fetch_messagesObject



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

def fetch_messages
  messages = []
  
  begin
    # Use Bot API or MTProto based on configuration
    if @config['bot_token']
      messages = fetch_bot_messages
    elsif @config['api_id'] && @config['api_hash']
      messages = fetch_mtproto_messages
    else
      puts "Telegram: No valid credentials configured" if ENV['DEBUG']
    end
    
  rescue => e
    puts "Telegram fetch error: #{e.message}" if ENV['DEBUG']
    puts e.backtrace.join("\n") if ENV['DEBUG']
  end
  
  messages
end

#send_message(to, subject, body, in_reply_to = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/heathrow/sources/telegram.rb', line 70

def send_message(to, subject, body, in_reply_to = nil)
  if @config['bot_token']
    send_bot_message(to, body, in_reply_to)
  elsif @config['session_string']
    send_mtproto_message(to, body, in_reply_to)
  else
    { success: false, message: "Telegram not configured for sending" }
  end
end

#test_connectionObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/heathrow/sources/telegram.rb', line 42

def test_connection
  begin
    if @config['bot_token']
      test_bot_connection
    elsif @config['api_id'] && @config['api_hash']
      test_mtproto_connection
    else
      { success: false, message: "No Telegram credentials configured" }
    end
  rescue => e
    { success: false, message: "Connection test failed: #{e.message}" }
  end
end