Sha256: c4706b7a15ec4b5d322bd484d3e7f78d65ace5845673491cdace033bea9cee3b

Contents?: true

Size: 614 Bytes

Versions: 2

Compression:

Stored size: 614 Bytes

Contents

module TelegramChatbot
  class TelegramBot < ApplicationRecord
    validates :token, :username, presence: true, if: :enabled?

    before_create :post_webhook_to_telegram, if: :enabled?
    before_update :post_webhook_to_telegram, if: :enabled?

    has_many :chat_groups, foreign_key: :bot_token, primary_key: :token

    def post_webhook_to_telegram
      bot = Telegram::Bot::Client.new(token: token, username: username)

      begin
        request = bot.set_webhook(url: ENV["TELEGRAM_CALLBACK_URL"])

        self.actived = request["ok"]
      rescue
        self.actived = false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
telegram_chatbot-0.1.3 app/models/telegram_chatbot/telegram_bot.rb
telegram_chatbot-0.1.1 app/models/telegram_chatbot/telegram_bot.rb