Module: GrubY::Filters

Defined in:
lib/gruubY/filters.rb

Defined Under Namespace

Classes: EntityFilter, Filter

Class Method Summary collapse

Class Method Details

.adminObject



148
# File 'lib/gruubY/filters.rb', line 148

def admin = Filter.new("admin") { |_f, _c, u| %w[administrator creator].include?(message_from(u)&.chat_member_status.to_s) }

.allObject



71
72
73
# File 'lib/gruubY/filters.rb', line 71

def all
  Filter.new("all") { true }
end

.animationObject



110
# File 'lib/gruubY/filters.rb', line 110

def animation = has_key_filter("animation")

.audioObject



106
# File 'lib/gruubY/filters.rb', line 106

def audio = has_key_filter("audio")

.botObject



82
83
84
# File 'lib/gruubY/filters.rb', line 82

def bot
  Filter.new("bot") { |_f, _c, u| message_from(u)&.from&.is_bot == true }
end

.businessObject



151
# File 'lib/gruubY/filters.rb', line 151

def business = has_key_filter("business_connection_id")

.captionObject



104
# File 'lib/gruubY/filters.rb', line 104

def caption = has_key_filter("caption")

.channelObject



192
193
194
# File 'lib/gruubY/filters.rb', line 192

def channel
  Filter.new("channel") { |_f, _c, u| message_from(u)&.chat&.type == "channel" }
end

.channel_chat_createdObject



139
# File 'lib/gruubY/filters.rb', line 139

def channel_chat_created = has_key_filter("channel_chat_created")

.chat(chats = nil) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/gruubY/filters.rb', line 247

def chat(chats = nil)
  require "set"
  EntityFilter.new("chat", chats) do |u, values|
    msg = message_from(u)
    cid = msg&.chat&.id || u&.chat&.id
    uname = msg&.chat&.username || u&.chat&.username
    values.empty? || values.include?(cid.to_s) || values.include?(uname.to_s) || values.include?("me")
  end
end

.chat_sharedObject



117
# File 'lib/gruubY/filters.rb', line 117

def chat_shared = has_key_filter("chat_shared")

.command(commands, prefixes: "/", case_sensitive: false) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/gruubY/filters.rb', line 204

def command(commands, prefixes: "/", case_sensitive: false)
  list = Array(commands).map(&:to_s)
  prefix_list = prefixes.nil? ? [""] : Array(prefixes).map(&:to_s)
  Filter.new("command") do |_f, _c, u|
    msg = message_from(u)
    text = msg&.text.to_s
    next false if text.empty?
    next false unless prefix_list.any? { |p| p.empty? || text.start_with?(p) }

    cmd = text.split(/\s+/, 2).first.to_s
    cmd = cmd.sub(/\A[#{Regexp.escape(prefix_list.join)}]/, "") unless prefix_list.include?("")
    cmd = cmd.split("@", 2).first
    cmd = cmd.downcase unless case_sensitive
    expected = case_sensitive ? list : list.map(&:downcase)
    expected.include?(cmd)
  end
end

.contactObject



122
# File 'lib/gruubY/filters.rb', line 122

def contact = has_key_filter("contact")

.create(func:, name: "CustomFilter", **kwargs) ⇒ Object



65
66
67
68
69
# File 'lib/gruubY/filters.rb', line 65

def create(func:, name: "CustomFilter", **kwargs)
  Filter.new(name) do |filter, client, update|
    func.call(filter, client, update, **kwargs)
  end
end

.delete_chat_photoObject



136
# File 'lib/gruubY/filters.rb', line 136

def delete_chat_photo = has_key_filter("delete_chat_photo")

.diceObject



128
# File 'lib/gruubY/filters.rb', line 128

def dice = has_key_filter("dice")

.directObject



196
197
198
# File 'lib/gruubY/filters.rb', line 196

def direct
  Filter.new("direct") { |_f, _c, u| message_from(u)&.chat&.is_direct_messages == true }
end

.documentObject



107
# File 'lib/gruubY/filters.rb', line 107

def document = has_key_filter("document")

.forumObject



200
201
202
# File 'lib/gruubY/filters.rb', line 200

def forum
  Filter.new("forum") { |_f, _c, u| message_from(u)&.chat&.is_forum == true }
end

.forwardedObject



103
# File 'lib/gruubY/filters.rb', line 103

def forwarded = Filter.new("forwarded") { |_f, _c, u| !!message_from(u)&.forward_origin }

.from_scheduledObject



155
# File 'lib/gruubY/filters.rb', line 155

def from_scheduled = has_key_filter("from_scheduled")

.gameObject



111
# File 'lib/gruubY/filters.rb', line 111

def game = has_key_filter("game")

.game_high_scoreObject



143
# File 'lib/gruubY/filters.rb', line 143

def game_high_score = has_key_filter("game_high_score")

.giftObject



115
# File 'lib/gruubY/filters.rb', line 115

def gift = has_key_filter("gift")

.gift_codeObject



114
# File 'lib/gruubY/filters.rb', line 114

def gift_code = has_key_filter("gift_code")

.gift_offerObject



158
# File 'lib/gruubY/filters.rb', line 158

def gift_offer = has_key_filter("gift_offer")

.gift_offer_acceptedObject



159
# File 'lib/gruubY/filters.rb', line 159

def gift_offer_accepted = has_key_filter("gift_offer_accepted")

.gift_offer_rejectedObject



160
# File 'lib/gruubY/filters.rb', line 160

def gift_offer_rejected = has_key_filter("gift_offer_rejected")

.giveawayObject



112
# File 'lib/gruubY/filters.rb', line 112

def giveaway = has_key_filter("giveaway")

.giveaway_winnersObject



113
# File 'lib/gruubY/filters.rb', line 113

def giveaway_winners = has_key_filter("giveaway_winners")

.groupObject



188
189
190
# File 'lib/gruubY/filters.rb', line 188

def group
  Filter.new("group") { |_f, _c, u| %w[group supergroup].include?(message_from(u)&.chat&.type) }
end

.group_chat_createdObject



137
# File 'lib/gruubY/filters.rb', line 137

def group_chat_created = has_key_filter("group_chat_created")

.has_key_filter(key) ⇒ Object



268
269
270
# File 'lib/gruubY/filters.rb', line 268

def has_key_filter(key)
  Filter.new(key) { |_f, _c, u| present_key?(message_from(u), key) }
end

.incomingObject



90
91
92
# File 'lib/gruubY/filters.rb', line 90

def incoming
  Filter.new("incoming") { |_f, _c, u| message_from(u)&.from&.is_bot != true }
end

.inline_keyboardObject



145
# File 'lib/gruubY/filters.rb', line 145

def inline_keyboard = has_key_filter("reply_markup")

.left_chat_memberObject



133
# File 'lib/gruubY/filters.rb', line 133

def left_chat_member = has_key_filter("left_chat_member")

.linked_channelObject



157
# File 'lib/gruubY/filters.rb', line 157

def linked_channel = has_key_filter("is_automatic_forward")

.live_locationObject



124
# File 'lib/gruubY/filters.rb', line 124

def live_location = Filter.new("live_location") { |_f, _c, u| !!message_from(u)&.location&.dig("live_period") }

.locationObject



123
# File 'lib/gruubY/filters.rb', line 123

def location = has_key_filter("location")

.meObject



75
76
77
78
79
80
# File 'lib/gruubY/filters.rb', line 75

def me
  Filter.new("me") do |_f, _c, u|
    msg = message_from(u)
    msg&.from&.is_bot == false && (msg&.from&.is_self == true || %w[me self].include?(msg&.from&.username.to_s.downcase))
  end
end

.mediaObject



175
176
177
178
179
180
181
182
# File 'lib/gruubY/filters.rb', line 175

def media
  Filter.new("media") do |_f, _c, u|
    msg = message_from(u)
    next false unless msg

    %w[audio document photo sticker video animation voice video_note contact location venue poll].any? { |k| present_key?(msg, k) }
  end
end

.media_groupObject



119
# File 'lib/gruubY/filters.rb', line 119

def media_group = has_key_filter("media_group_id")

.media_spoilerObject



130
# File 'lib/gruubY/filters.rb', line 130

def media_spoiler = has_key_filter("has_media_spoiler")

.mentionedObject



146
# File 'lib/gruubY/filters.rb', line 146

def mentioned = Filter.new("mentioned") { |_f, _c, u| message_from(u)&.entities&.any? { |e| e["type"] == "mention" } }

.message_from(update) ⇒ Object



282
283
284
285
286
287
288
289
290
# File 'lib/gruubY/filters.rb', line 282

def message_from(update)
  return update if update.is_a?(GrubY::Message)
  return update.message if update.respond_to?(:message)
  return update.edited_message if update.respond_to?(:edited_message)
  return update.business_message if update.respond_to?(:business_message)
  return update.edited_business_message if update.respond_to?(:edited_business_message)

  nil
end

.migrate_from_chat_idObject



141
# File 'lib/gruubY/filters.rb', line 141

def migrate_from_chat_id = has_key_filter("migrate_from_chat_id")

.migrate_to_chat_idObject



140
# File 'lib/gruubY/filters.rb', line 140

def migrate_to_chat_id = has_key_filter("migrate_to_chat_id")

.new_chat_membersObject



132
# File 'lib/gruubY/filters.rb', line 132

def new_chat_members = has_key_filter("new_chat_members")

.new_chat_photoObject



135
# File 'lib/gruubY/filters.rb', line 135

def new_chat_photo = has_key_filter("new_chat_photo")

.new_chat_titleObject



134
# File 'lib/gruubY/filters.rb', line 134

def new_chat_title = has_key_filter("new_chat_title")

.outgoingObject



94
95
96
97
98
99
# File 'lib/gruubY/filters.rb', line 94

def outgoing
  Filter.new("outgoing") do |_f, _c, u|
    msg = message_from(u)
    msg && msg.from && (msg.from.is_self == true || msg.from.id == msg.chat&.id)
  end
end


156
# File 'lib/gruubY/filters.rb', line 156

def paid_message = has_key_filter("is_paid_post")

.photoObject



108
# File 'lib/gruubY/filters.rb', line 108

def photo = has_key_filter("photo")

.pinned_messageObject



142
# File 'lib/gruubY/filters.rb', line 142

def pinned_message = has_key_filter("pinned_message")

.pollObject



127
# File 'lib/gruubY/filters.rb', line 127

def poll = has_key_filter("poll")

.present_key?(obj, key) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
279
280
# File 'lib/gruubY/filters.rb', line 272

def present_key?(obj, key)
  return false unless obj
  value = if obj.respond_to?(:[])
            obj[key]
          elsif obj.respond_to?(key)
            obj.public_send(key)
          end
  !(value.nil? || (value.respond_to?(:empty?) && value.empty?))
end

.privateObject



184
185
186
# File 'lib/gruubY/filters.rb', line 184

def private
  Filter.new("private") { |_f, _c, u| message_from(u)&.chat&.type == "private" }
end

.quoteObject



129
# File 'lib/gruubY/filters.rb', line 129

def quote = has_key_filter("quote")

.regex(pattern, flags: 0) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gruubY/filters.rb', line 222

def regex(pattern, flags: 0)
  reg = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern.to_s, flags)
  Filter.new("regex") do |_f, _c, u|
    target = if u.respond_to?(:data) && !u.data.to_s.empty?
               u.data.to_s
             elsif u.respond_to?(:query)
               u.query.to_s
             else
               msg = message_from(u)
               msg&.text.to_s.empty? ? msg&.caption.to_s : msg&.text.to_s
             end
    !!(target =~ reg)
  end
end

.replyObject



102
# File 'lib/gruubY/filters.rb', line 102

def reply = Filter.new("reply") { |_f, _c, u| !!(message_from(u)&.reply_to_message || message_from(u)&.reply_to_story) }

.reply_keyboardObject



144
# File 'lib/gruubY/filters.rb', line 144

def reply_keyboard = has_key_filter("reply_markup")

.scheduledObject



154
# File 'lib/gruubY/filters.rb', line 154

def scheduled = has_key_filter("is_scheduled")

.self_destructionObject



105
# File 'lib/gruubY/filters.rb', line 105

def self_destruction = has_key_filter("self_destruction")

.sender_chatObject



86
87
88
# File 'lib/gruubY/filters.rb', line 86

def sender_chat
  Filter.new("sender_chat") { |_f, _c, u| !message_from(u)&.sender_chat.nil? }
end

.serviceObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gruubY/filters.rb', line 162

def service
  Filter.new("service") do |_f, _c, u|
    msg = message_from(u)
    next false unless msg

    %w[left_chat_member new_chat_title new_chat_photo delete_chat_photo group_chat_created
       supergroup_chat_created channel_chat_created migrate_to_chat_id migrate_from_chat_id
       pinned_message video_chat_started video_chat_ended video_chat_members_invited successful_payment].any? do |k|
      present_key?(msg, k)
    end
  end
end

.stickerObject



109
# File 'lib/gruubY/filters.rb', line 109

def sticker = has_key_filter("sticker")

.storyObject



131
# File 'lib/gruubY/filters.rb', line 131

def story = has_key_filter("story")

.successful_paymentObject



153
# File 'lib/gruubY/filters.rb', line 153

def successful_payment = has_key_filter("successful_payment")

.supergroup_chat_createdObject



138
# File 'lib/gruubY/filters.rb', line 138

def supergroup_chat_created = has_key_filter("supergroup_chat_created")

.textObject



101
# File 'lib/gruubY/filters.rb', line 101

def text = has_key_filter("text")

.topic(topics = nil) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/gruubY/filters.rb', line 257

def topic(topics = nil)
  require "set"
  EntityFilter.new("topic", topics) do |u, values|
    msg = message_from(u)
    tid = msg&.message_thread_id || msg&.direct_messages_topic&.dig("topic_id")
    values.empty? || values.include?(tid.to_s)
  end
end

.user(users = nil) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/gruubY/filters.rb', line 237

def user(users = nil)
  require "set"
  EntityFilter.new("user", users) do |u, values|
    msg = message_from(u)
    uid = msg&.from&.id || u&.from&.id
    uname = msg&.from&.username || u&.from&.username
    values.empty? || values.include?(uid.to_s) || values.include?(uname.to_s) || values.include?("me")
  end
end

.users_sharedObject



116
# File 'lib/gruubY/filters.rb', line 116

def users_shared = has_key_filter("users_shared")

.venueObject



125
# File 'lib/gruubY/filters.rb', line 125

def venue = has_key_filter("venue")

.via_botObject



147
# File 'lib/gruubY/filters.rb', line 147

def via_bot = has_key_filter("via_bot")

.videoObject



118
# File 'lib/gruubY/filters.rb', line 118

def video = has_key_filter("video")

.video_chat_endedObject



150
# File 'lib/gruubY/filters.rb', line 150

def video_chat_ended = has_key_filter("video_chat_ended")

.video_chat_members_invitedObject



152
# File 'lib/gruubY/filters.rb', line 152

def video_chat_members_invited = has_key_filter("video_chat_members_invited")

.video_chat_startedObject



149
# File 'lib/gruubY/filters.rb', line 149

def video_chat_started = has_key_filter("video_chat_started")

.video_noteObject



121
# File 'lib/gruubY/filters.rb', line 121

def video_note = has_key_filter("video_note")

.voiceObject



120
# File 'lib/gruubY/filters.rb', line 120

def voice = has_key_filter("voice")

.web_pageObject



126
# File 'lib/gruubY/filters.rb', line 126

def web_page = Filter.new("web_page") { |_f, _c, u| !!message_from(u)&.link_preview_options }