Class: Heathrow::Sources::Telegram
- Inherits:
-
Object
- Object
- Heathrow::Sources::Telegram
- Defined in:
- lib/heathrow/sources/telegram.rb
Instance Attribute Summary collapse
-
#last_fetch_time ⇒ Object
readonly
Returns the value of attribute last_fetch_time.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #authenticate ⇒ Object
- #can_reply? ⇒ Boolean
- #fetch_messages ⇒ Object
-
#initialize(source) ⇒ Telegram
constructor
A new instance of Telegram.
- #send_message(to, subject, body, in_reply_to = nil) ⇒ Object
- #test_connection ⇒ Object
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_time ⇒ Object (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 |
#source ⇒ Object (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
#authenticate ⇒ Object
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
66 67 68 |
# File 'lib/heathrow/sources/telegram.rb', line 66 def can_reply? true end |
#fetch_messages ⇒ Object
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 = [] begin # Use Bot API or MTProto based on configuration if @config['bot_token'] = elsif @config['api_id'] && @config['api_hash'] = else puts "Telegram: No valid credentials configured" if ENV['DEBUG'] end rescue => e puts "Telegram fetch error: #{e.}" if ENV['DEBUG'] puts e.backtrace.join("\n") if ENV['DEBUG'] end 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 (to, subject, body, in_reply_to = nil) if @config['bot_token'] (to, body, in_reply_to) elsif @config['session_string'] (to, body, in_reply_to) else { success: false, message: "Telegram not configured for sending" } end end |
#test_connection ⇒ Object
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.}" } end end |