Sha256: 58d81c6ebe606a9d1fb46aa3c153d309fab4d8b1dbc6141252185a56b03f71cd

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Telegram
  module Bot
    module Tasks
      extend self

      def set_webhook
        routes = Rails.application.routes.url_helpers
        cert_file = ENV.fetch('CERT', nil)
        cert = File.open(cert_file) if cert_file
        each_bot do |key, bot|
          route_name = RoutesHelper.route_name_for_bot(bot)
          url = routes.send("#{route_name}_url")
          say("Setting webhook for #{key}...")
          bot.set_webhook(
            url: url,
            certificate: cert,
            ip_address: ENV.fetch('IP_ADDRESS', nil),
            drop_pending_updates: drop_pending_updates,
          )
        end
      end

      def delete_webhook
        each_bot do |key, bot|
          say("Deleting webhook for #{key}...")
          bot.delete_webhook(drop_pending_updates: drop_pending_updates)
        end
      end

      def log_out
        each_bot do |key, bot|
          say("Logging out #{key}...")
          bot.log_out
        end
      end

      def close
        each_bot do |key, bot|
          say("Closing #{key}...")
          bot.close
        end
      end

      private

      def say(text)
        puts(text) unless Rails.env.test? # rubocop:disable Rails/Output
      end

      def each_bot(&block)
        id = ENV['BOT']&.to_sym
        bots = id ? {id => Client.by_id(id)} : Telegram.bots
        bots.each { |key, bot| bot.async(false) { block[key, bot] } }
      end

      def drop_pending_updates
        ENV['DROP_PENDING_UPDATES']&.downcase == 'true'
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
telegram-bot-0.16.5 lib/telegram/bot/tasks.rb
telegram-bot-0.16.4 lib/telegram/bot/tasks.rb
telegram-bot-0.16.3 lib/telegram/bot/tasks.rb