Class: Spree::Admin::TelegramBotsController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/spree/admin/telegram_bots_controller.rb

Instance Method Summary collapse

Instance Method Details

#collection_url(options = {}) ⇒ Object

override



43
44
45
# File 'app/controllers/spree/admin/telegram_bots_controller.rb', line 43

def collection_url(options = {})
  admin_telegram_bots_url(options)
end

#model_classObject

override



33
34
35
# File 'app/controllers/spree/admin/telegram_bots_controller.rb', line 33

def model_class
  SpreeCmCommissioner::TelegramBot
end

#obfuscate_token(token) ⇒ Object



6
7
8
9
10
11
# File 'app/controllers/spree/admin/telegram_bots_controller.rb', line 6

def obfuscate_token(token)
  return nil if token.blank?
  return nil if token.length <= 10

  "#{token[0, 5]}#{'*' * (token.length - 10)}#{token[-5, 5]}"
end

#object_nameObject

override



38
39
40
# File 'app/controllers/spree/admin/telegram_bots_controller.rb', line 38

def object_name
  'spree_cm_commissioner_telegram_bot'
end

#set_webhookObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/spree/admin/telegram_bots_controller.rb', line 13

def set_webhook
  telegram_bot = SpreeCmCommissioner::TelegramBot.find(params[:id])

  client = ::Telegram::Bot::Client.new(telegram_bot.token)

  response = client.set_webhook(
    url: "#{request.base_url}/api/webhook/telegram_bots",
    secret_token: telegram_bot.secure_token
  )

  if response['ok']
    flash[:success] = 'Webhook is set successfully.' # rubocop:disable Rails/I18nLocaleTexts
  else
    flash[:error] = 'Failed to set webhook.' # rubocop:disable Rails/I18nLocaleTexts
  end

  redirect_back fallback_location: collection_url
end