Class: Telegram_2

Inherits:
Object
  • Object
show all
Defined in:
lib/imperituroard/platforms/public/telegram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(telegram_api_url, telegram_chat_id, telegram_log = 1) ⇒ Telegram_2

Returns a new instance of Telegram_2.



5
6
7
8
9
# File 'lib/imperituroard/platforms/public/telegram.rb', line 5

def initialize(telegram_api_url, telegram_chat_id, telegram_log=1)
  @telegram_api_url = telegram_api_url
  @telegram_chat_id = telegram_chat_id
  @telegram_log = telegram_log
end

Instance Attribute Details

#telegram_api_urlObject

Returns the value of attribute telegram_api_url.



3
4
5
# File 'lib/imperituroard/platforms/public/telegram.rb', line 3

def telegram_api_url
  @telegram_api_url
end

#telegram_chat_idObject

Returns the value of attribute telegram_chat_id.



3
4
5
# File 'lib/imperituroard/platforms/public/telegram.rb', line 3

def telegram_chat_id
  @telegram_chat_id
end

#telegram_logObject

Returns the value of attribute telegram_log.



3
4
5
# File 'lib/imperituroard/platforms/public/telegram.rb', line 3

def telegram_log
  @telegram_log
end

Instance Method Details

#telegram_message(message) ⇒ Object

procedure for send log to telegram chat



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/imperituroard/platforms/public/telegram.rb', line 12

def telegram_message(message)
  begin
    if telegram_log == 1
      uri = URI.parse(telegram_api_url)
      https_connector = Net::HTTP.new(uri.host, uri.port)
      https_connector.use_ssl = true
      data = {chat_id: telegram_chat_id, text: message}
      request_mess = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' => 'application/json'})
      request_mess.body = data.to_json
      response_mess = https_connector.request(request_mess)
      body = response_mess.body
      return {:code => 200,
              :result => 'Request completed successfully',
              :body => {:telegram_resp => JSON.parse(body.to_s),
                        :description => "Telegram message to telegram_chat_id: #{telegram_chat_id.to_s}"}}
    else
      return {:code => 404, :result => 'Notification disabled'}
    end
  rescue
    return {:code => 507, :result => 'Unknown SDK error'}
  end
end

#telegram_message2(message) ⇒ Object

procedure for send log to telegram chat in separate thread



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/imperituroard/platforms/public/telegram.rb', line 36

def telegram_message2(message)
  begin
    if telegram_log == 1
      Thread.new do
        begin
          uri = URI.parse(telegram_api_url)
          https_connector = Net::HTTP.new(uri.host, uri.port)
          https_connector.use_ssl = true
          data = {chat_id: telegram_chat_id, text: message}
          request_mess = Net::HTTP::Post.new(uri.request_uri, {'Content-Type': 'application/json'})
          request_mess.body = data.to_json
          response_mess = https_connector.request(request_mess)
          body = response_mess.body
          return {:code => 200,
                  :result => 'Request completed successfully',
                  :body => {:telegram_resp => JSON.parse(body.to_s),
                            :description => "Telegram message to telegram_chat_id: #{telegram_chat_id.to_s}"}}
        rescue
          return {:code => 508, :result => 'Unknown SDK error'}
        end
      end
    else
      return {:code => 404, :result => 'Notification disabled'}
    end
  rescue
    return {:code => 507, :result => 'Unknown SDK error'}
  end
end