Class: FORCAST::Telegram::Send

Inherits:
Object
  • Object
show all
Defined in:
lib/forcast/utils/telegram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat_id) ⇒ Send

Returns a new instance of Send.



10
11
12
# File 'lib/forcast/utils/telegram.rb', line 10

def initialize(chat_id)
  self.chat_id = chat_id.to_s
end

Instance Attribute Details

#chat_idObject

Returns the value of attribute chat_id.



8
9
10
# File 'lib/forcast/utils/telegram.rb', line 8

def chat_id
  @chat_id
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/forcast/utils/telegram.rb', line 4

def headers
  @headers
end

#json_sendObject

Returns the value of attribute json_send.



6
7
8
# File 'lib/forcast/utils/telegram.rb', line 6

def json_send
  @json_send
end

#metodoObject

Returns the value of attribute metodo.



7
8
9
# File 'lib/forcast/utils/telegram.rb', line 7

def metodo
  @metodo
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/forcast/utils/telegram.rb', line 5

def url
  @url
end

Instance Method Details

#constructorObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/forcast/utils/telegram.rb', line 48

def constructor 
    yield
    self.headers = {}
    resp = SERVIDORTELEGRAM.conexion(self.metodo, self.headers, self.url, self.json_send)
    puts resp
    return true if resp[1].blank?
    json_recived = {}
    logger.info "[TELEGRAM] "+resp[1].to_s
    if resp[0] && self.valid_json?(resp[1]) 
      json_recived = JSON.parse(resp[1])
      unless json_recived.is_a?(Array)
        unless json_recived["error"].nil?
          logger.info "[TELEGRAM] "+json_recived["error"].to_s
          return false
        end
      end
      return  json_recived
    else
      return false
    end
end

#getUpdatesObject



14
15
16
17
18
19
20
21
# File 'lib/forcast/utils/telegram.rb', line 14

def getUpdates
  json_recived = constructor do
      self.metodo = :get
      self.url = "/getUpdates"           
      self.json_send = {}
  end

end

#sendMessage(message) ⇒ Object

Mostrar usuarios



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/forcast/utils/telegram.rb', line 23

def sendMessage(message)
  json_recived = constructor do
      self.metodo = :post
      self.url = "/sendMessage?"
      self.url += "chat_id="+self.chat_id.to_s
      self.url += "&text="+message 
      self.url += "&parse_mode=HTML"             
      self.json_send = {}
  end

end

#sendPhoto(url_imagen, message) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/forcast/utils/telegram.rb', line 35

def sendPhoto(url_imagen,message)
  json_recived = constructor do
      self.metodo = :post
      self.url = "/sendPhoto?"
      self.url += "chat_id="+self.chat_id.to_s
      self.url += "&photo="+url_imagen
      self.url += "&caption="+message
      self.url += "&parse_mode=HTML" 
      self.json_send = {}
  end

end

#valid_json?(json) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/forcast/utils/telegram.rb', line 70

def valid_json?(json)
  begin
    JSON.parse(json)
    return true
  rescue JSON::ParserError => e
    puts e
    return false
  end
end