Sha256: 70f30f5e6b5737d68cd1398c343dcd58bebd15fc1941e29e7562321534c37a51

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'rest-client'

module TeleNotify
  class TelegramUser < ::ActiveRecord::Base

    validates_presence_of :telegram_id
    validates_uniqueness_of :telegram_id

    @@next_update_id = 0

    def self.configure_home_url(url)
      @@home_url = url
    end

    def self.configure_dev_url(url)
      @@dev_url = url
    end

    def self.active_url
      if Rails.env.production?
        @@home_url
      else
        @@dev_url
      end
    end


    def self.configure_token(token)
      if token =~ /^[0-9]+:[\w-]+$/ #hacker proof
        @@token = token
        @@url = "https://api.telegram.org/bot" + token + "/"
        @@callback_url = active_url + "/" + @@token
        RestClient.post(@@url + "setWebhook", { url: @@callback_url })
      else
        raise "Invalid token! Please add a valid Telegram token in config/initializers/tele_notify.rb or see https://github.com/ppati000/tele_notify for further instructions."
      end
    end

    def self.send_message_to_all(text)
      success = true
      TeleNotify::TelegramUser.all.each do |user|
        success = false if !user.send_message(text)
      end
      success
    end


    def send_message(text)
      response = JSON.parse(RestClient.post(@@url + "sendMessage", chat_id: self.telegram_id, text: text), { symbolize_names: true })
      response[:ok]
    end


  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tele_notify-0.1.1 lib/tele_notify/telegram_user.rb