Class: Telegram::TelegramBase

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/telegram/models.rb

Overview

Base class for Telegram models

See Also:

Since:

  • 0.1.0

Direct Known Subclasses

TelegramChat, TelegramContact

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #logger, logger_for

Instance Attribute Details

#clientClient (readonly)

Returns Root client instance.

Returns:

  • (Client)

    Root client instance

Since:

  • 0.1.0


11
12
13
# File 'lib/telegram/models.rb', line 11

def client
  @client
end

#idInteger (readonly)

Returns Identifier.

Returns:

  • (Integer)

    Identifier

Since:

  • 0.1.0


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

Parameters:

  • client (Client)

    Root client instance

  • raw (Integer)

    Raw data on either of telegram objects

Since:

  • 0.1.0


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

Since:

  • 0.1.1


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

Parameters:

  • path (String)

    The absoulte path of the image you want to send

  • refer (TelegramMessage)

    referral of the method call

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.1


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

Parameters:

  • url (String)

    The URL of the image you want to send

  • refer (TelegramMessage)

    referral of the method call

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.1


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

Parameters:

  • text (String)

    text you want to send for

  • refer (TelegramMessage)

    referrer of the method call

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.0


78
79
80
# File 'lib/telegram/models.rb', line 78

def send_message(text, refer, &callback)
  @client.msg(targetize, text, &callback)
end

#send_stickerObject

This method is abstract.

Send a sticker

Since:

  • 0.1.0


83
84
85
# File 'lib/telegram/models.rb', line 83

def send_sticker()

end

#send_typing(&callback) ⇒ Object

Send typing signal

Parameters:

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.1


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

Parameters:

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.1


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

Parameters:

  • path (String)

    The absoulte path of the video you want to send

  • refer (TelegramMessage)

    referral of the method call

  • callback (Block)

    Callback block that will be called when finished

Since:

  • 0.1.1


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

#targetizeObject

Convert to telegram-cli target format from Telegram::TelegramChat or Telegram::TelegramContact

Since:

  • 0.1.1


36
37
38
# File 'lib/telegram/models.rb', line 36

def targetize
  @type == 'encr_chat' ? @title : to_tg
end