Class: Telegram::Client
- Includes:
- Logging
- Defined in:
- lib/telegram/client.rb
Overview
Telegram Client
Instance Attribute Summary collapse
-
#auth_properties ⇒ Object
Event listeners that can respond to the event arrives.
-
#chats ⇒ Array<TelegramChat>
readonly
Chats that current user joined.
-
#connection ⇒ ConnectionPool
readonly
Socket connection pool, includes Connection.
-
#contacts ⇒ Array<TelegramContact>
readonly
Current user’s contact list.
-
#on ⇒ Object
Event listeners that can respond to the event arrives.
-
#profile ⇒ TelegramContact
readonly
Current user’s profile.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#connect { ... } ⇒ Object
Start telegram-cli daemon.
-
#connected? ⇒ bool
Connection pool status.
-
#create_pool ⇒ Object
private
Create a connection pool based on the Connection and given configuration.
-
#execute ⇒ Object
private
Execute telegram-cli daemon and wait for the response.
-
#initialize {|config| ... } ⇒ Client
constructor
Initialize Telegram Client.
-
#on_connect ⇒ Object
private
A event listener that will be called if the Connection successes on either of ConnectionPool.
-
#on_disconnect ⇒ Object
private
A event listener that will be called if the Connection closes on either of ConnectionPool.
- #on_disconnect=(callback) ⇒ Object
-
#poll ⇒ Object
private
Do the long-polling from stdout of the telegram-cli.
-
#process_data ⇒ Object
private
Process given data to make Event instance.
Methods included from Logging
configure_logger_for, #logger, logger_for
Methods inherited from API
#add_contact, #chat_add_user, #chat_del_user, #create_group_chat, #disconnect, #download_attachment, #mark_read, #msg, #send_contact, #send_file, #send_photo, #send_typing, #send_typing_abort, #send_video, #update!, #update_chats!, #update_contacts!, #update_profile!
Constructor Details
#initialize {|config| ... } ⇒ Client
Initialize Telegram Client
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/telegram/client.rb', line 60 def initialize(&block) @config = Telegram::Config.new @auth_properties = Telegram::AuthProperties.new yield @config, @auth_properties @logger = @config.logger if @config.logger @connected = 0 @stdout = nil @connect_callback = nil @on = {} @profile = nil @contacts = [] @chats = [] @starts_at = nil @events = EM::Queue.new logger.info("Initialized") end |
Instance Attribute Details
#auth_properties ⇒ Object
Event listeners that can respond to the event arrives
54 55 56 |
# File 'lib/telegram/client.rb', line 54 def auth_properties @auth_properties end |
#chats ⇒ Array<TelegramChat> (readonly)
Returns Chats that current user joined.
46 47 48 |
# File 'lib/telegram/client.rb', line 46 def chats @chats end |
#connection ⇒ ConnectionPool (readonly)
Returns Socket connection pool, includes Telegram::Connection.
34 35 36 |
# File 'lib/telegram/client.rb', line 34 def connection @connection end |
#contacts ⇒ Array<TelegramContact> (readonly)
Returns Current user’s contact list.
42 43 44 |
# File 'lib/telegram/client.rb', line 42 def contacts @contacts end |
#on ⇒ Object
Event listeners that can respond to the event arrives
54 55 56 |
# File 'lib/telegram/client.rb', line 54 def on @on end |
#profile ⇒ TelegramContact (readonly)
Returns Current user’s profile.
38 39 40 |
# File 'lib/telegram/client.rb', line 38 def profile @profile end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
48 49 50 |
# File 'lib/telegram/client.rb', line 48 def stdout @stdout end |
Instance Method Details
#connect { ... } ⇒ Object
Start telegram-cli daemon
144 145 146 147 148 149 |
# File 'lib/telegram/client.rb', line 144 def connect(&block) logger.info("Trying to start telegram-cli and then connect") @connect_callback = block process_data EM.defer(method(:execute), method(:create_pool), method(:execution_failed)) end |
#connected? ⇒ bool
Returns Connection pool status.
193 194 195 |
# File 'lib/telegram/client.rb', line 193 def connected? @connected == @config.size end |
#create_pool ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a connection pool based on the Telegram::Connection and given configuration
154 155 156 157 158 159 160 161 |
# File 'lib/telegram/client.rb', line 154 def create_pool(*) @connection = ConnectionPool.new(@config.size) do client = EM.connect_unix_domain(@config.sock, Connection) client.on_connect = self.method(:on_connect) client.on_disconnect = self.method(:on_disconnect) client end end |
#execute ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute telegram-cli daemon and wait for the response
82 83 84 85 86 87 |
# File 'lib/telegram/client.rb', line 82 def execute cli_arguments = Telegram::CLIArguments.new(@config) command = "'#{@config.daemon}' #{cli_arguments.to_s}" @stdout = IO.popen(command, 'a+') initialize_stdout_reading end |
#on_connect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A event listener that will be called if the Telegram::Connection successes on either of Telegram::ConnectionPool
166 167 168 169 170 171 172 173 |
# File 'lib/telegram/client.rb', line 166 def on_connect @connected += 1 if connected? logger.info("Successfully connected to the Telegram CLI") EM.defer(&method(:poll)) update!(&@connect_callback) end end |
#on_disconnect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A event listener that will be called if the Telegram::Connection closes on either of Telegram::ConnectionPool
178 179 180 181 182 183 184 185 |
# File 'lib/telegram/client.rb', line 178 def on_disconnect @connected -= 1 if @connected == 0 logger.info("Disconnected from Telegram CLI") close_stdout @disconnect_callback.call if @disconnect_callback end end |
#on_disconnect=(callback) ⇒ Object
187 188 189 |
# File 'lib/telegram/client.rb', line 187 def on_disconnect=(callback) @disconnect_callback = callback end |
#poll ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Do the long-polling from stdout of the telegram-cli
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/telegram/client.rb', line 92 def poll logger.info("Start polling for events") while (data = @stdout.gets) begin brace = data.index('{') data = data[brace..-2] data = Oj.load(data, mode: :compat) @events << data rescue end end end |
#process_data ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Process given data to make Event instance
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/telegram/client.rb', line 108 def process_data process = Proc.new { |data| begin type = case data['event'] when 'message' if data['from']['peer_id'] != @profile.id EventType::RECEIVE_MESSAGE else EventType::SEND_MESSAGE end end action = data.has_key?('action') ? case data['action'] when 'chat_add_user' ActionType::CHAT_ADD_USER when 'create_group_chat' ActionType::CREATE_GROUP_CHAT when 'add_contact' ActionType::ADD_CONTACT else ActionType::UNKNOWN_ACTION end : ActionType::NO_ACTION event = Event.new(self, type, action, data) @on[type].call(event) if @on.has_key?(type) rescue Exception => e logger.error("Error occurred during the processing: #{data}\n #{e.inspect} #{e.backtrace}") end @events.pop(&process) } @events.pop(&process) end |