Module: Tramway::Bots::Telegram::MessagesManager

Included in:
Custom::Notify::File, Custom::Notify::Text, Custom::Scenario
Defined in:
lib/tramway/bots/telegram/messages_manager.rb

Instance Method Summary collapse

Instance Method Details

#log_message(message, user, chat, bot) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tramway/bots/telegram/messages_manager.rb', line 4

def log_message(message, user, chat, bot)
  file_path = "#{Rails.root}/lib/tasks/bot_telegram/bot_message_attributes.yml"
  telegram_message_attributes = YAML.load_file(file_path)['telegram_message']['attributes']

  message_object = Tramway::Bots::Telegram::Message.find_or_create_by(
    telegram_message_id: message.message_id,
    bot_id: bot.id,
    user_id: user.id,
    chat_id: chat.id
  )

  message_object.update! text: message.try(:text), 
    project_id: Project.find_by(title: 'PurpleMagic').id,
    options: (telegram_message_attributes.reduce({}) do |hash, attribute|
                hash.merge! attribute => message.send(attribute)
              end)

  message_object
end

#message_to_chat(bot_api, chat_id, message_obj, **options) ⇒ Object

:reek:FeatureEnvy { enabled: false }



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tramway/bots/telegram/messages_manager.rb', line 25

def message_to_chat(bot_api, chat_id, message_obj, **options)
  case message_obj.class.to_s
  when 'String'
    send_string bot_api, chat_id, message_obj, **options
  when 'Tramway::Bots::Telegram::Custom::Message'
    bot_api.send_message chat_id: chat_id, **message_obj.options.merge(options)
    send_file bot_api, chat_id, message_obj if message_obj.file.present?
  else
    raise message_obj.class.to_s
  end
rescue StandardError => error
  Airbrake.notify error
end

#message_to_user(bot_api, message_obj, chat_id) ⇒ Object

:reek:FeatureEnvy { enabled: true }



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tramway/bots/telegram/messages_manager.rb', line 40

def message_to_user(bot_api, message_obj, chat_id)
  case message_obj.class.to_s
  when 'String'
    send_string bot_api, chat_id, message_obj
  when 'Tramway::Bots::Telegram::Scenario::Step'
    send_scenario_step bot_api, chat_id, message_obj
  when 'Tramway::Bots::Telegram::Custom::Message'
    bot_api.send_message chat_id: chat_id, **message_obj.options
  end
rescue StandardError => error
  Airbrake.notify error
end

#send_file(bot_api, chat_id, message_obj) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tramway/bots/telegram/messages_manager.rb', line 53

def send_file(bot_api, chat_id, message_obj)
  mime_type = case message_obj.file.file.file[-3..].downcase
              when 'jpg', 'png'
                [:photo, 'image/jpeg']
              when 'mp3'
                [:voice, 'audio/mpeg']
              end
  params = {
    chat_id: chat_id,
    mime_type[0] => Faraday::UploadIO.new(message_obj.file.file.file, mime_type[1])
  }
  if message_obj.reply_markup.present?
    params.merge!(
      reply_markup: Telegram::Bot::Types::ReplyKeyboardMarkup.new(**message_obj.reply_markup),
      parse_mode: :markdown
    )
  end

  bot_api.public_send "send_#{mime_type[0]}", **params
end