Module: Rubydium::Mixins::OtherActions

Defined in:
lib/rubydium/mixins/other_actions.rb

Instance Method Summary collapse

Instance Method Details

#definitely_message(maybe_message) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubydium/mixins/other_actions.rb', line 22

def definitely_message(maybe_message)
  return maybe_message if maybe_message.is_a? Telegram::Bot::Types::Message

  if maybe_message["message_id"]
    Telegram::Bot::Types::Message.new(maybe_message)
  elsif maybe_message["result"]["message_id"]
    Telegram::Bot::Types::Message.new(maybe_message["result"])
  else
    nil
  end
rescue StandardError
  nil
end

#safe_delete(maybe_message, only_bot_or_owner: true) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/rubydium/mixins/other_actions.rb', line 4

def safe_delete(maybe_message, only_bot_or_owner: true)
  message = definitely_message(maybe_message)
  from_owner = message&.from&.username == config.owner_username
  from_bot = message&.from&.id == config.bot_id
  return false unless only_bot_or_owner || from_bot || from_owner

  safe_delete_by_id(message.message_id, from_bot: true)
end

#safe_delete_by_id(id, from_bot: false) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/rubydium/mixins/other_actions.rb', line 13

def safe_delete_by_id(id, from_bot: false)
  return false unless bot_can_delete_messages? || from_bot

  result = @api.delete_message(chat_id: @chat.id, message_id: id)
  result["ok"]
rescue Telegram::Bot::Exceptions::ResponseError
  false
end