Class: Tgbot::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/tgbot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, data) ⇒ Update

Returns a new instance of Update.



54
55
56
57
# File 'lib/tgbot.rb', line 54

def initialize bot, data
  @bot = bot
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



142
143
144
145
# File 'lib/tgbot.rb', line 142

def method_missing meth, *args, &blk
  return @data[meth] if args.empty? && @data[meth]
  @bot.send(meth, *args, &blk)
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



52
53
54
# File 'lib/tgbot.rb', line 52

def bot
  @bot
end

#dataObject (readonly)

Returns the value of attribute data.



52
53
54
# File 'lib/tgbot.rb', line 52

def data
  @data
end

Instance Method Details

#interrupt!Object Also known as: done!



59
60
61
# File 'lib/tgbot.rb', line 59

def interrupt!
  @bot.instance_variable_get(:@tasks).clear
end

#match(pattern) ⇒ Object



78
79
80
81
# File 'lib/tgbot.rb', line 78

def match pattern
  return nil if pattern.nil? || text.nil?
  text.match(pattern)
end

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/tgbot.rb', line 72

def match? pattern
  return true if pattern.nil?
  return false if text.nil?
  !!match(pattern)
end

#messageObject



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

def message
  @data.to_h.find { |k, v| k.match? /message|post/ }&.last
end

#message_idObject



138
139
140
# File 'lib/tgbot.rb', line 138

def message_id
  message&.message_id
end

#reply(*things, media: true, style: nil, **options) ⇒ Object



91
92
93
94
95
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tgbot.rb', line 91

def reply *things, media: true, style: nil, **options
  meth = :send_message
  payload = { chat_id: message&.chat.id }
  things.each do |x|
    if IO === x
      magic = MimeMagic.by_magic(x)
      case
      when magic.audio?
        meth = :send_audio
        payload[:audio] = x
      when magic.image?
        meth = :send_photo
        payload[:photo] = x
      when magic.video?
        meth = :send_video
        payload[:video] = x
      else
        meth = :send_document
        payload[:document] = x
      end
    else
      payload[:text] = x.to_s
      payload[:parse_mode] = 'Markdown'
    end
  end
  payload = payload.merge options
  case style
  when :at
    if payload[:text] && message.from
      from = message.from
      if payload[:parse_mode].match? /Markdown/i
        prefix = "[#{from.first_name}](tg://user?id=#{from.id}) "
      elsif payload[:parse_mode].match? /HTML/i
        prefix = "<a href=\"tg://user?id=#{from.id}\">#{from.first_name}</a> "
      else
        prefix = ''
      end
      payload[:text] = prefix + payload[:text]
    end
  when nil
    if !payload[:reply_to_message_id]
      payload[:reply_to_message_id] = message_id
    end
  end
  @bot.send meth, payload
end

#retry!(n = 1) ⇒ Object



65
66
67
68
69
70
# File 'lib/tgbot.rb', line 65

def retry! n=1
  @retried ||= n
  return if @retried <= 0
  @bot.instance_variable_get(:@updates) << self
  @retried -= 1
end

#textObject



87
88
89
# File 'lib/tgbot.rb', line 87

def text
  message&.text&.gsub(/(\/\w+)(@\w+)/, '\1')
end