Class: XNM::Telegram::HTTPCore
- Inherits:
-
Object
- Object
- XNM::Telegram::HTTPCore
- Defined in:
- lib/xnm/telegram/HTTPCore.rb
Overview
This class handles the direct connection to the Telegram API All it does is handle a receive loop with the fitting update IDs, as well as a rescue’d “perform post” message.
Direct Known Subclasses
Instance Method Summary collapse
-
#attach_receptor(receptorClass) ⇒ Object
TODO check if the class supports handle_packet.
- #feed_receptors(data) ⇒ Object
-
#initialize(apikey) ⇒ HTTPCore
constructor
A new instance of HTTPCore.
-
#perform_post(method, data = nil) ⇒ Object
Perform a POST request.
-
#receive_loop ⇒ Object
Handle receiving of the data from Telegram API This is done via the “getUpdates” HTTP Request, with a timeout of 20s Update-ID offset is handled automagically by the system, so you needn’t worry about it.
Constructor Details
#initialize(apikey) ⇒ HTTPCore
Returns a new instance of HTTPCore.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/xnm/telegram/HTTPCore.rb', line 12 def initialize(apikey) @apikey = apikey; @lastUpdateID = 0; # Start the receive loop. Shouldn't crash, but if it does we # want to know. @receiveThread = Thread.new do receive_loop(); end @receiveThread.abort_on_exception = true # Receptors are class instances that will receive updates # from the HTTP update connection @receptors = Array.new(); end |
Instance Method Details
#attach_receptor(receptorClass) ⇒ Object
TODO check if the class supports handle_packet
123 124 125 |
# File 'lib/xnm/telegram/HTTPCore.rb', line 123 def attach_receptor(receptorClass) @receptors << receptorClass; end |
#feed_receptors(data) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xnm/telegram/HTTPCore.rb', line 79 def feed_receptors(data) # Hand it out to the receptors @receptors.each do |r| begin r.handle_packet(data); rescue => e warn "Error in repector: #{e}" warn e.backtrace.join("\n"); end end end |
#perform_post(method, data = nil) ⇒ Object
Perform a POST request.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/xnm/telegram/HTTPCore.rb', line 58 def perform_post(method, data = nil) call_address = "https://api.telegram.org/bot#{@apikey}/#{method}" # Rescue-construct to prevent a HTTP error from # crashing our system. timeoutLen = data[:timeout] if data.is_a? Hash timeoutLen ||= 4; retryCount = 0; begin Timeout.timeout(timeoutLen) do return _raw_post(call_address, _double_json_data(data)); end rescue retryCount += 1; return {} if retryCount >= 3; sleep 0.5; retry end end |
#receive_loop ⇒ Object
Handle receiving of the data from Telegram API This is done via the “getUpdates” HTTP Request, with a timeout of 20s Update-ID offset is handled automagically by the system, so you needn’t worry about it.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/xnm/telegram/HTTPCore.rb', line 96 def receive_loop() loop do begin # Perform the update request packet = perform_post("getUpdates", {timeout: 20, offset: @lastUpdateID + 1}) next unless packet[:ok]; # Check if there even was a message sent by Telegram # Due to the 20s timeout, a zero-length reply can happen next if packet[:result].length == 0; # Handle each result individually. packet[:result].each do |data| hUpdateID = data[:update_id].to_i # Calculate the maximum Update ID (for the offset "getUpdates" parameter) @lastUpdateID = [hUpdateID, @lastUpdateID].max feed_receptors data end rescue sleep 1 retry end end end |