Class: SpreeCmCommissioner::TelegramChatsFinder

Inherits:
BaseInteractor show all
Defined in:
app/interactors/spree_cm_commissioner/telegram_chats_finder.rb

Instance Method Summary collapse

Instance Method Details

#callObject



5
6
7
8
# File 'app/interactors/spree_cm_commissioner/telegram_chats_finder.rb', line 5

def call
  updates = fetch_updates
  context.chats = get_chats(updates)
end

#fetch_updatesObject



24
25
26
# File 'app/interactors/spree_cm_commissioner/telegram_chats_finder.rb', line 24

def fetch_updates
  ::Telegram.bot.get_updates
end

#get_chats(updates) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/interactors/spree_cm_commissioner/telegram_chats_finder.rb', line 10

def get_chats(updates)
  updates['result'].filter_map do |update|
    chat = update.dig('my_chat_member', 'chat')

    if chat.present? && chat['title'] == telegram_chat_name.strip && chat['type'] == telegram_chat_type
      return {
        id: chat['id'],
        title: chat['title'],
        type: chat['type']
      }
    end
  end
end