Class: TeleNotify::TelegramUser

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/tele_notify/telegram_user.rb

Constant Summary collapse

@@next_update_id =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_urlObject



21
22
23
24
25
26
27
# File 'lib/tele_notify/telegram_user.rb', line 21

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

.configure_dev_url(url) ⇒ Object



17
18
19
# File 'lib/tele_notify/telegram_user.rb', line 17

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

.configure_home_url(url) ⇒ Object



13
14
15
# File 'lib/tele_notify/telegram_user.rb', line 13

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

.configure_token(token) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/tele_notify/telegram_user.rb', line 30

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

.send_message_to_all(text) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/tele_notify/telegram_user.rb', line 41

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

Instance Method Details

#send_message(text) ⇒ Object



50
51
52
53
# File 'lib/tele_notify/telegram_user.rb', line 50

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