Class: Kirimi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kirimi/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
'https://api.kirimi.id'
DEFAULT_TIMEOUT =
30

Instance Method Summary collapse

Constructor Details

#initialize(user_code:, secret:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/kirimi/client.rb', line 13

def initialize(user_code:, secret:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT)
  @user_code = user_code
  @secret    = secret
  @base_url  = base_url.chomp('/')
  @timeout   = timeout
end

Instance Method Details

#broadcast_message(device_id:, label:, numbers:, message:, delay: nil, delay_min: nil, delay_max: nil, media_url: nil, file_name: nil, started_at: nil, enable_typing_effect: nil, typing_speed_ms: nil) ⇒ Object

--- Broadcast ---



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kirimi/client.rb', line 62

def broadcast_message(device_id:, label:, numbers:, message:, delay: nil, delay_min: nil,
                      delay_max: nil, media_url: nil, file_name: nil, started_at: nil,
                      enable_typing_effect: nil, typing_speed_ms: nil)
  body = {
    device_id: device_id,
    label:     label,
    numbers:   Array(numbers),
    message:   message
  }
  body[:delay]              = delay           if delay
  body[:delayMin]           = delay_min       if delay_min
  body[:delayMax]           = delay_max       if delay_max
  body[:media_url]          = media_url       if media_url
  body[:fileName]           = file_name       if file_name
  body[:started_at]         = started_at      if started_at
  body[:enableTypingEffect] = enable_typing_effect unless enable_typing_effect.nil?
  body[:typingSpeedMs]      = typing_speed_ms if typing_speed_ms
  post('/v1/broadcast-message', body)
end

#cancel_deposit(ref:) ⇒ Object



235
236
237
# File 'lib/kirimi/client.rb', line 235

def cancel_deposit(ref:)
  post('/v1/cancel-deposit', { ref: ref })
end

#connect_device(device_id:) ⇒ Object



123
124
125
# File 'lib/kirimi/client.rb', line 123

def connect_device(device_id:)
  post('/v1/connect-device', { device_id: device_id })
end

#create_deposit(nominal:) ⇒ Object



227
228
229
# File 'lib/kirimi/client.rb', line 227

def create_deposit(nominal:)
  post('/v1/create-deposit', { nominal: nominal })
end

#create_device(package_id:, voucher_code: nil) ⇒ Object

--- Devices ---



117
118
119
120
121
# File 'lib/kirimi/client.rb', line 117

def create_device(package_id:, voucher_code: nil)
  body = { package_id: package_id }
  body[:voucher_code] = voucher_code if voucher_code
  post('/v1/create-device', body)
end

#deposit_status(ref:) ⇒ Object



231
232
233
# File 'lib/kirimi/client.rb', line 231

def deposit_status(ref:)
  post('/v1/deposit-status', { ref: ref })
end

#device_status(device_id:) ⇒ Object



140
141
142
# File 'lib/kirimi/client.rb', line 140

def device_status(device_id:)
  post('/v1/device-status', { device_id: device_id })
end

#device_status_enhanced(device_id:) ⇒ Object



144
145
146
# File 'lib/kirimi/client.rb', line 144

def device_status_enhanced(device_id:)
  post('/v1/device-status-enhanced', { device_id: device_id })
end

#generate_otp(device_id:, phone:, otp_length: nil, otp_type: nil, custom_otp_text: nil, custom_otp_message: nil, enable_typing_effect: nil, typing_speed_ms: nil) ⇒ Object

--- OTP (V1) ---



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/kirimi/client.rb', line 170

def generate_otp(device_id:, phone:, otp_length: nil, otp_type: nil, custom_otp_text: nil,
                 custom_otp_message: nil, enable_typing_effect: nil, typing_speed_ms: nil)
  body = { device_id: device_id, phone: phone }
  body[:otp_length]         = otp_length         if otp_length
  body[:otp_type]           = otp_type           if otp_type
  body[:customOtpText]      = custom_otp_text    if custom_otp_text
  body[:customOtpMessage]   = custom_otp_message if custom_otp_message
  body[:enableTypingEffect] = enable_typing_effect unless enable_typing_effect.nil?
  body[:typingSpeedMs]      = typing_speed_ms    if typing_speed_ms
  post('/v1/generate-otp', body)
end

#list_deposits(page: nil, limit: nil, status: nil) ⇒ Object



239
240
241
242
243
244
245
# File 'lib/kirimi/client.rb', line 239

def list_deposits(page: nil, limit: nil, status: nil)
  body = {}
  body[:page]   = page   if page
  body[:limit]  = limit  if limit
  body[:status] = status if status
  post('/v1/list-deposits', body)
end

#list_devices(page: nil, limit: nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/kirimi/client.rb', line 133

def list_devices(page: nil, limit: nil)
  body = {}
  body[:page]  = page  if page
  body[:limit] = limit if limit
  post('/v1/list-devices', body)
end

#list_packages ⇒ Object

--- Packages & Deposits ---



223
224
225
# File 'lib/kirimi/client.rb', line 223

def list_packages
  post('/v1/list-packages', {})
end

#otp_reverse_create(phone:, device_id:, app_name: nil, callback_url: nil, custom_message: nil, success_message: nil, failure_message: nil) ⇒ Object

--- OTP Reverse ---



206
207
208
209
210
211
212
213
214
215
# File 'lib/kirimi/client.rb', line 206

def otp_reverse_create(phone:, device_id:, app_name: nil, callback_url: nil,
                       custom_message: nil, success_message: nil, failure_message: nil)
  body = { phone: phone, device_id: device_id }
  body[:app_name]        = app_name        if app_name
  body[:callback_url]    = callback_url    if callback_url
  body[:custom_message]  = custom_message  if custom_message
  body[:success_message] = success_message if success_message
  body[:failure_message] = failure_message if failure_message
  post('/v2/otp-reverse/create', body)
end

#otp_reverse_status(token:) ⇒ Object



217
218
219
# File 'lib/kirimi/client.rb', line 217

def otp_reverse_status(token:)
  post('/v2/otp-reverse/status', { token: token })
end

#renew_device(device_id:, package_id:, voucher_code: nil) ⇒ Object



127
128
129
130
131
# File 'lib/kirimi/client.rb', line 127

def renew_device(device_id:, package_id:, voucher_code: nil)
  body = { device_id: device_id, package_id: package_id }
  body[:voucher_code] = voucher_code if voucher_code
  post('/v1/renew-device', body)
end

#save_contact(nama:, nomor:, device_id: nil) ⇒ Object

--- Contacts ---



156
157
158
159
160
# File 'lib/kirimi/client.rb', line 156

def save_contact(nama:, nomor:, device_id: nil)
  body = { nama: nama, nomor: nomor }
  body[:device_id] = device_id if device_id
  post('/v1/save-contact', body)
end

#save_contacts_bulk(contacts:, device_id: nil) ⇒ Object



162
163
164
165
166
# File 'lib/kirimi/client.rb', line 162

def save_contacts_bulk(contacts:, device_id: nil)
  body = { contacts: contacts }
  body[:device_id] = device_id if device_id
  post('/v1/save-contacts-bulk', body)
end

#send_message(device_id:, receiver:, message:, media_url: nil, file_name: nil, enable_typing_effect: nil, typing_speed_ms: nil, quoted_message_id: nil) ⇒ Object

--- WhatsApp Unofficial ---



22
23
24
25
26
27
28
29
30
31
# File 'lib/kirimi/client.rb', line 22

def send_message(device_id:, receiver:, message:, media_url: nil, file_name: nil,
                 enable_typing_effect: nil, typing_speed_ms: nil, quoted_message_id: nil)
  body = { device_id: device_id, receiver: receiver, message: message }
  body[:media_url]           = media_url           if media_url
  body[:fileName]            = file_name           if file_name
  body[:enableTypingEffect]  = enable_typing_effect unless enable_typing_effect.nil?
  body[:typingSpeedMs]       = typing_speed_ms     if typing_speed_ms
  body[:quotedMessageId]     = quoted_message_id   if quoted_message_id
  post('/v1/send-message', body)
end

#send_message_fast(device_id:, receiver:, message:, media_url: nil, file_name: nil, quoted_message_id: nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/kirimi/client.rb', line 51

def send_message_fast(device_id:, receiver:, message:, media_url: nil, file_name: nil,
                      quoted_message_id: nil)
  body = { device_id: device_id, receiver: receiver, message: message }
  body[:media_url]       = media_url         if media_url
  body[:fileName]        = file_name         if file_name
  body[:quotedMessageId] = quoted_message_id if quoted_message_id
  post('/v1/send-message-fast', body)
end

#send_message_file(device_id:, receiver:, file:, file_name: nil, message: nil, caption: nil, quoted_message_id: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kirimi/client.rb', line 33

def send_message_file(device_id:, receiver:, file:, file_name: nil, message: nil, caption: nil,
                      quoted_message_id: nil)
  file_io = file.is_a?(String) ? File.open(file, 'rb') : file
  fields = {
    'user_code' => @user_code,
    'secret'    => @secret,
    'device_id' => device_id,
    'receiver'  => receiver
  }
  fields['fileName']        = file_name        if file_name
  fields['message']         = message          if message
  fields['caption']         = caption          if caption
  fields['quotedMessageId'] = quoted_message_id if quoted_message_id
  post_multipart('/v1/send-message-file', fields, file_io, file_name || 'file')
ensure
  file_io&.close if file.is_a?(String)
end

#send_otp_v2(phone:, method: nil, app_name: nil, device_id: nil, waba_id: nil, template_name: nil, custom_message: nil) ⇒ Object

--- OTP (V2) ---



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kirimi/client.rb', line 188

def send_otp_v2(phone:, method: nil, app_name: nil, device_id: nil, waba_id: nil,
                template_name: nil, custom_message: nil)
  body = { phone: phone }
  body[:method]         = method         if method
  body[:app_name]       = app_name       if app_name
  body[:device_id]      = device_id      if device_id
  body[:waba_id]        = waba_id        if waba_id
  body[:template_name]  = template_name  if template_name
  body[:custom_message] = custom_message if custom_message
  post('/v2/otp/send', body)
end

#send_waba_message(waba_id:, to:, template_name:, variables: nil, header: nil, buttons: nil) ⇒ Object

--- WABA (Cloud API) ---



84
85
86
87
88
89
90
# File 'lib/kirimi/client.rb', line 84

def send_waba_message(waba_id:, to:, template_name:, variables: nil, header: nil, buttons: nil)
  body = { waba_id: waba_id, to: to, template_name: template_name }
  body[:variables] = variables if variables
  body[:header]    = header    if header
  body[:buttons]   = buttons   if buttons
  post('/v1/waba/send-message', body)
end

#user_info ⇒ Object

--- User ---



150
151
152
# File 'lib/kirimi/client.rb', line 150

def 
  post('/v1/user-info', {})
end

#validate_otp(device_id:, phone:, otp:) ⇒ Object



182
183
184
# File 'lib/kirimi/client.rb', line 182

def validate_otp(device_id:, phone:, otp:)
  post('/v1/validate-otp', { device_id: device_id, phone: phone, otp: otp })
end

#verify_otp_v2(phone:, otp_code:) ⇒ Object



200
201
202
# File 'lib/kirimi/client.rb', line 200

def verify_otp_v2(phone:, otp_code:)
  post('/v2/otp/verify', { phone: phone, otp_code: otp_code })
end

#waba_conversations(limit: nil, page: nil) ⇒ Object



96
97
98
99
100
101
# File 'lib/kirimi/client.rb', line 96

def waba_conversations(limit: nil, page: nil)
  body = {}
  body[:limit] = limit if limit
  body[:page]  = page  if page
  post('/v1/waba/conversations', body)
end

#waba_reply(waba_id:, to:, message:) ⇒ Object



92
93
94
# File 'lib/kirimi/client.rb', line 92

def waba_reply(waba_id:, to:, message:)
  post('/v1/waba/messages/reply', { waba_id: waba_id, to: to, message: message })
end

#waba_send_otp(waba_id:, to:, template_name:) ⇒ Object



107
108
109
# File 'lib/kirimi/client.rb', line 107

def waba_send_otp(waba_id:, to:, template_name:)
  post('/v1/waba/send-otp', { waba_id: waba_id, to: to, template_name: template_name })
end

#waba_templates_sync(waba_id:) ⇒ Object



103
104
105
# File 'lib/kirimi/client.rb', line 103

def waba_templates_sync(waba_id:)
  post('/v1/waba/templates/sync', { waba_id: waba_id })
end

#waba_verify_otp(waba_id:, to:, otp_code:) ⇒ Object



111
112
113
# File 'lib/kirimi/client.rb', line 111

def waba_verify_otp(waba_id:, to:, otp_code:)
  post('/v1/waba/verify-otp', { waba_id: waba_id, to: to, otp_code: otp_code })
end