Class: Kogno::WhatsApp::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/core/lib/whatsapp/api.rb

Class Method Summary collapse

Class Method Details

.contacts(params) ⇒ Object



140
141
142
143
144
145
# File 'lib/core/lib/whatsapp/api.rb', line 140

def contacts(params)
  {
    type: :contacts,
    contacts: params
  }
end

.interactive_buttons(text, buttons) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/core/lib/whatsapp/api.rb', line 70

def interactive_buttons(text, buttons)

  {
    type: :interactive,
    interactive: {
      type: :button,
      body: {
        text: text
      },
      action: {
        buttons: buttons
      }
    }
  }    

end

.interactive_list(params) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/core/lib/whatsapp/api.rb', line 105

def interactive_list(params)

  {
    type: :interactive,
    interactive: {
      type: :list,
      header: params[:header],
      footer: params[:footer],
      body: {
        text: params[:text]
      },
      action: {
        button: params[:button],
        sections: params[:sections]
      }
    }
  }    

end

.location(location_data) ⇒ Object



125
126
127
128
129
130
# File 'lib/core/lib/whatsapp/api.rb', line 125

def location(location_data) 
  {
    type: :location,
    location: location_data
  }
end

.media(type, params) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/core/lib/whatsapp/api.rb', line 132

def media(type, params) 
  media_object = {
    type: type       
  }
  media_object[type] = params
  media_object
end

.request(body, type, method = :post) ⇒ Object

:messages, :messenger_profile



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/core/lib/whatsapp/api.rb', line 12

def request(body,type,method=:post) # :messages, :messenger_profile

  url = URI("#{Kogno::Application.config.whatsapp.graph_url}/#{Kogno::Application.config.whatsapp.phone_number_id}/#{type}")

  logger.write "REQUEST TO: #{url}", :pink
  logger.write "SENT: #{JSON.parse(body)}", :blue      

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  case method
    when :delete
      request = Net::HTTP::Delete.new(url)
    else 
      request = Net::HTTP::Post.new(url)
  end    
  
  request["Authorization"] = "Bearer #{Kogno::Application.config.whatsapp.access_token}"
  request["content-type"] = "application/json"

  request.body = body

  response = http.request(request)
  # logger.debug_json JSON.parse(body), :green
  response_hash = JSON.parse(response.read_body, {:symbolize_names => true})
  logger.write "RESPONSE: #{response_hash}\n", :light_blue
  return(response_hash)

end

.send(recipient, message, type = "messages") ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/core/lib/whatsapp/api.rb', line 44

def send(recipient,message, type="messages")
  body = {
    messaging_product: :whatsapp,
    recipient_type: :individual,
    to: recipient, 
  }.merge(message).to_json

  return(self.request(body,type))

end

.template_message(template_name, components = [], lang = "en_US") ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/lib/whatsapp/api.rb', line 55

def template_message(template_name, components=[], lang="en_US")

  {
    type: :template,
    template: { 
      name: template_name, 
      language: { 
        code: lang
      },
      components: components
    } 
  }    

end