Class: Telegram::TelegramBase
- Inherits:
-
Object
- Object
- Telegram::TelegramBase
- Includes:
- Logging
- Defined in:
- lib/telegram/models.rb
Overview
Base class for Telegram models
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
Root client instance.
-
#id ⇒ Integer
readonly
Identifier.
Class Method Summary collapse
-
.pick_or_new(client, raw) ⇒ Object
Return an instance if exists, or else create a new instance and return.
Instance Method Summary collapse
-
#fail_back(&callback) ⇒ Object
private
Execute a callback block with failure result.
-
#send_image(path, refer, &callback) ⇒ Object
Send an image.
-
#send_image_url(url, opt, refer, &callback) ⇒ Object
Send an image with given url, not implemen.
-
#send_message(text, refer, &callback) ⇒ Object
Send a message with given text.
- #send_sticker ⇒ Object abstract
-
#send_typing(&callback) ⇒ Object
Send typing signal.
-
#send_typing_abort(&callback) ⇒ Object
Abort sending typing signal.
-
#send_video(path, refer, &callback) ⇒ Object
Send a video.
-
#targetize ⇒ Object
Convert to telegram-cli target format from TelegramChat or TelegramContact.
Methods included from Logging
configure_logger_for, #logger, logger_for
Instance Attribute Details
#client ⇒ Client (readonly)
Returns Root client instance.
11 12 13 |
# File 'lib/telegram/models.rb', line 11 def client @client end |
#id ⇒ Integer (readonly)
Returns Identifier.
14 15 16 |
# File 'lib/telegram/models.rb', line 14 def id @id end |
Class Method Details
.pick_or_new(client, raw) ⇒ Object
Return an instance if exists, or else create a new instance and return
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/telegram/models.rb', line 21 def self.pick_or_new(client, raw) # where to search for where = if self == TelegramChat client.chats elsif self == TelegramContact client.contacts end # pick a first item if exists, or else create where.find { |obj| obj.id == raw['peer_id'] } or self.new(client, raw) end |
Instance Method Details
#fail_back(&callback) ⇒ 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 a callback block with failure result
44 45 46 |
# File 'lib/telegram/models.rb', line 44 def fail_back(&callback) callback.call(false, {}) unless callback.nil? end |
#send_image(path, refer, &callback) ⇒ Object
Send an image
93 94 95 96 97 98 99 100 |
# File 'lib/telegram/models.rb', line 93 def send_image(path, refer, &callback) if @type == 'encr_chat' logger.warn("Currently telegram-cli has a bug with send_typing, then prevent this for safety") return end fail_back(&callback) if not File.exist?(path) @client.send_photo(targetize, path, &callback) end |
#send_image_url(url, opt, refer, &callback) ⇒ Object
Send an image with given url, not implemen
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/telegram/models.rb', line 108 def send_image_url(url, opt, refer, &callback) begin opt = {} if opt.nil? http = EM::HttpRequest.new(url, :connect_timeout => 2, :inactivity_timeout => 5).get opt file = Tempfile.new(['image', 'jpg']) http.stream { |chunk| file.write(chunk) } http.callback { file.close type = FastImage.type(file.path) if %i(jpeg png gif).include?(type) send_image(file.path, refer, &callback) else fail_back(&callback) end } rescue Exception => e logger.error("An error occurred during the image downloading: #{e.inspect} #{e.backtrace}") fail_back(&callback) end end |
#send_message(text, refer, &callback) ⇒ Object
Send a message with given text
78 79 80 |
# File 'lib/telegram/models.rb', line 78 def (text, refer, &callback) @client.msg(targetize, text, &callback) end |
#send_sticker ⇒ Object
Send a sticker
83 84 85 |
# File 'lib/telegram/models.rb', line 83 def send_sticker() end |
#send_typing(&callback) ⇒ Object
Send typing signal
52 53 54 55 56 57 58 |
# File 'lib/telegram/models.rb', line 52 def send_typing(&callback) if @type == 'encr_chat' logger.warn("Currently telegram-cli has a bug with send_typing, then prevent this for safety") return end @client.send_typing(targetize, &callback) end |
#send_typing_abort(&callback) ⇒ Object
Abort sending typing signal
64 65 66 67 68 69 70 |
# File 'lib/telegram/models.rb', line 64 def send_typing_abort(&callback) if @type == 'encr_chat' logger.warn("Currently telegram-cli has a bug with send_typing, then prevent this for safety") return end @client.send_typing_abort(targetize, &callback) end |
#send_video(path, refer, &callback) ⇒ Object
Send a video
137 138 139 140 |
# File 'lib/telegram/models.rb', line 137 def send_video(path, refer, &callback) fail_back(&callback) if not File.exist?(path) @client.send_video(targetize, path, &callback) end |
#targetize ⇒ Object
Convert to telegram-cli target format from Telegram::TelegramChat or Telegram::TelegramContact
36 37 38 |
# File 'lib/telegram/models.rb', line 36 def targetize @type == 'encr_chat' ? @title : to_tg end |