class MessageBird::Client

Attributes

access_key[R]
conversation_client[R]
http_client[R]
voice_client[R]

Public Class Methods

new(access_key = nil, http_client = nil, conversation_client = nil, voice_client = nil) click to toggle source
# File lib/messagebird/client.rb, line 50
def initialize(access_key = nil, http_client = nil, conversation_client = nil, voice_client = nil)
  @access_key = access_key || ENV['MESSAGEBIRD_ACCESS_KEY']
  @http_client = http_client || HttpClient.new(@access_key)
  @conversation_client = conversation_client || ConversationClient.new(@access_key)
  @number_client = http_client || NumberClient.new(@access_key)
  @voice_client = voice_client || VoiceClient.new(@access_key)
end

Public Instance Methods

balance() click to toggle source

Retrieve your balance.

# File lib/messagebird/client.rb, line 187
def balance
  Balance.new(request(:get, 'balance'))
end
call_create(source, destination, call_flow = {}, webhook = {}, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 299
def call_create(source, destination, call_flow = {}, webhook = {}, params = {})
  params = params.merge(callFlow: call_flow.to_json) unless call_flow.empty?
  params = params.merge(webhook: webhook.to_json) unless webhook.empty?

  Voice::Call.new(voice_request(:post, 'calls', params.merge(source: source, destination: destination)))
end
call_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 314
def call_delete(id)
  voice_request(:delete, "calls/#{id}")
end
call_flow_create(title, steps, default, record, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 460
def call_flow_create(title, steps, default, record, params = {})
  params = params.merge(
    title: title,
    steps: steps,
    default: default,
    record: record
  )
  CallFlow.new(voice_request(:post, 'call-flows', params))
end
call_flow_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 478
def call_flow_delete(id)
  voice_request(:delete, "call-flows/#{id}")
end
call_flow_list(per_page = CallFlowList::PER_PAGE, page = CallFlowList::CURRENT_PAGE) click to toggle source
# File lib/messagebird/client.rb, line 474
def call_flow_list(per_page = CallFlowList::PER_PAGE, page = CallFlowList::CURRENT_PAGE)
  CallFlowList.new(CallFlow, voice_request(:get, "call-flows?perPage=#{per_page}&page=#{page}"))
end
call_flow_view(id) click to toggle source
# File lib/messagebird/client.rb, line 470
def call_flow_view(id)
  CallFlow.new(voice_request(:get, "call-flows/#{id}"))
end
call_leg_list(call_id, per_page = Voice::List::PER_PAGE, current_page = Voice::List::CURRENT_PAGE) click to toggle source
# File lib/messagebird/client.rb, line 318
def call_leg_list(call_id, per_page = Voice::List::PER_PAGE, current_page = Voice::List::CURRENT_PAGE)
  Voice::List.new(Voice::CallLeg, voice_request(:get, "calls/#{call_id}/legs?perPage=#{per_page}&currentPage=#{current_page}"))
end
call_leg_recording_delete(call_id, leg_id, recording_id) click to toggle source
# File lib/messagebird/client.rb, line 330
def call_leg_recording_delete(call_id, leg_id, recording_id)
  voice_request(:delete, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}")
end
call_leg_recording_download(recording_uri, &block) click to toggle source
# File lib/messagebird/client.rb, line 334
def call_leg_recording_download(recording_uri, &block)
  @voice_client.request_block(:get, recording_uri, {}, &block)
end
call_leg_recording_list(call_id, leg_id) click to toggle source
# File lib/messagebird/client.rb, line 326
def call_leg_recording_list(call_id, leg_id)
  Voice::List.new(Voice::CallLegRecording, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings"))
end
call_leg_recording_view(call_id, leg_id, recording_id) click to toggle source
# File lib/messagebird/client.rb, line 322
def call_leg_recording_view(call_id, leg_id, recording_id)
  Voice::CallLegRecording.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}"))
end
call_list(per_page = Voice::List::PER_PAGE, page = Voice::List::CURRENT_PAGE) click to toggle source
# File lib/messagebird/client.rb, line 306
def call_list(per_page = Voice::List::PER_PAGE, page = Voice::List::CURRENT_PAGE)
  Voice::List.new(Voice::Call, voice_request(:get, "calls?perPage=#{per_page}&currentPage=#{page}"))
end
call_view(id) click to toggle source
# File lib/messagebird/client.rb, line 310
def call_view(id)
  Voice::Call.new(voice_request(:get, "calls/#{id}"))
end
contact(id) click to toggle source
# File lib/messagebird/client.rb, line 374
def contact(id)
  Contact.new(request(:get, "contacts/#{id}"))
end
contact_create(phone_number, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 366
def contact_create(phone_number, params = {})
  Contact.new(request(
                :post,
                'contacts',
                params.merge(msisdn: phone_number.to_s)
  ))
end
contact_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 378
def contact_delete(id)
  request(:delete, "contacts/#{id}")
end
contact_list(limit = 0, offset = 0) click to toggle source
# File lib/messagebird/client.rb, line 386
def contact_list(limit = 0, offset = 0)
  List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}"))
end
contact_update(id, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 382
def contact_update(id, params = {})
  request(:patch, "contacts/#{id}", params)
end
conversation(id) click to toggle source
# File lib/messagebird/client.rb, line 131
def conversation(id)
  Conversation.new(conversation_request(:get, "conversations/#{id}"))
end
conversation_list(limit = -1, offset = -1) click to toggle source
# File lib/messagebird/client.rb, line 118
def conversation_list(limit = -1, offset = -1)
  query = '?'
  if limit != -1
    query += "limit=#{limit}&"
  end

  if offset != -1
    query += "offset=#{offset}"
  end

  List.new(Conversation, conversation_request(:get, "conversations#{query}"))
end
conversation_message(id) click to toggle source
# File lib/messagebird/client.rb, line 156
def conversation_message(id)
  ConversationMessage.new(conversation_request(:get, "messages/#{id}"))
end
conversation_messages_list(id, limit = -1, offset = -1) click to toggle source
# File lib/messagebird/client.rb, line 143
def conversation_messages_list(id, limit = -1, offset = -1)
  query = '?'
  if limit != -1
    query += "limit=#{limit}&"
  end

  if offset != -1
    query += "offset=#{offset}"
  end

  List.new(ConversationMessage, conversation_request(:get, "conversations/#{id}/messages#{query}"))
end
conversation_reply(id, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 139
def conversation_reply(id, params = {})
  ConversationMessage.new(conversation_request(:post, "conversations/#{id}/messages", params))
end
conversation_request(method, path, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 58
def conversation_request(method, path, params = {})
  response_body = @conversation_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end
conversation_update(id, status) click to toggle source
# File lib/messagebird/client.rb, line 135
def conversation_update(id, status)
  Conversation.new(conversation_request(:patch, "conversations/#{id}", status: status))
end
conversation_webhook(id) click to toggle source
# File lib/messagebird/client.rb, line 178
def conversation_webhook(id)
  ConversationWebhook.new(conversation_request(:get, "webhooks/#{id}"))
end
conversation_webhook_create(channel_id, url, events = []) click to toggle source
# File lib/messagebird/client.rb, line 160
def conversation_webhook_create(channel_id, url, events = [])
  ConversationWebhook.new(conversation_request(
                            :post,
                            'webhooks',
                            channel_id: channel_id,
                            url: url,
                            events: events
  ))
end
conversation_webhook_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 182
def conversation_webhook_delete(id)
  conversation_request(:delete, "webhooks/#{id}")
end
conversation_webhook_update(id, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 174
def conversation_webhook_update(id, params = {})
  ConversationWebhook.new(conversation_request(:patch, "webhooks/#{id}", params))
end
conversation_webhooks_list(limit = 0, offset = 0) click to toggle source
# File lib/messagebird/client.rb, line 170
def conversation_webhooks_list(limit = 0, offset = 0)
  List.new(ConversationWebhook, conversation_request(:get, "webhooks?limit=#{limit}&offset=#{offset}"))
end
group(id) click to toggle source
# File lib/messagebird/client.rb, line 390
def group(id)
  Group.new(request(:get, "groups/#{id}"))
end
group_add_contacts(group_id, contact_ids) click to toggle source
# File lib/messagebird/client.rb, line 410
def group_add_contacts(group_id, contact_ids)
  # We expect an array, but we can handle a string ID as well...
  contact_ids = [contact_ids] if contact_ids.is_a? String

  query = add_contacts_query(contact_ids)

  request(:get, "groups/#{group_id}?#{query}")
end
group_create(name) click to toggle source
# File lib/messagebird/client.rb, line 394
def group_create(name)
  Group.new(request(:post, 'groups', name: name))
end
group_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 398
def group_delete(id)
  request(:delete, "groups/#{id}")
end
group_delete_contact(group_id, contact_id) click to toggle source
# File lib/messagebird/client.rb, line 419
def group_delete_contact(group_id, contact_id)
  request(:delete, "groups/#{group_id}/contacts/#{contact_id}")
end
group_list(limit = 0, offset = 0) click to toggle source
# File lib/messagebird/client.rb, line 402
def group_list(limit = 0, offset = 0)
  List.new(Group, request(:get, "groups?limit=#{limit}&offset=#{offset}"))
end
group_update(id, name) click to toggle source
# File lib/messagebird/client.rb, line 406
def group_update(id, name)
  request(:patch, "groups/#{id}", name: name)
end
hlr(id) click to toggle source

Retrieve the information of specific HLR.

# File lib/messagebird/client.rb, line 192
def hlr(id)
  HLR.new(request(:get, "hlr/#{id}"))
end
hlr_create(msisdn, reference) click to toggle source

Create a new HLR.

# File lib/messagebird/client.rb, line 197
def hlr_create(msisdn, reference)
  HLR.new(request(
            :post,
            'hlr',
            msisdn: msisdn,
            reference: reference
  ))
end
lookup(phone_number, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 354
def lookup(phone_number, params = {})
  Lookup.new(request(:get, "lookup/#{phone_number}", params))
end
lookup_hlr(phone_number, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 362
def lookup_hlr(phone_number, params = {})
  HLR.new(request(:get, "lookup/#{phone_number}/hlr", params))
end
lookup_hlr_create(phone_number, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 358
def lookup_hlr_create(phone_number, params = {})
  HLR.new(request(:post, "lookup/#{phone_number}/hlr", params))
end
message(id) click to toggle source

Retrieve the information of specific message.

# File lib/messagebird/client.rb, line 236
def message(id)
  Message.new(request(:get, "messages/#{id}"))
end
message_create(originator, recipients, body, params = {}) click to toggle source

Create a new message.

# File lib/messagebird/client.rb, line 249
def message_create(originator, recipients, body, params = {})
  # Convert an array of recipients to a comma-separated string.
  recipients = recipients.join(',') if recipients.is_a?(Array)

  Message.new(request(
                :post,
                'messages',
                params.merge(originator: originator.to_s,
                             body: body.to_s,
                             recipients: recipients)
  ))
end
message_list(filters = {}) click to toggle source

Retrieve messages with optional paging and status filter.

# File lib/messagebird/client.rb, line 241
def message_list(filters = {})
  params = { limit: 10, offset: 0 }.merge(filters).compact
  query = "messages?#{URI.encode_www_form(params)}"

  List.new(Message, request(:get, query))
end
number_cancel(number) click to toggle source

Cancel a number

# File lib/messagebird/client.rb, line 456
def number_cancel(number)
  number_request(:delete, "phone-numbers/#{number}")
end
number_fetch(number) click to toggle source

Fetch specific purchased number's details

# File lib/messagebird/client.rb, line 445
def number_fetch(number)
  Number.new(number_request(:get, "phone-numbers/#{number}"))
end
number_fetch_all(params = {}) click to toggle source

Fetch all purchaed numbers' details

# File lib/messagebird/client.rb, line 440
def number_fetch_all(params = {})
  List.new(Number, number_request(:get, add_querystring('phone-numbers', params), params))
end
number_purchase(number, country_code, billing_interval_months) click to toggle source

Purchase an avaiable number

# File lib/messagebird/client.rb, line 430
def number_purchase(number, country_code, billing_interval_months)
  params = {
    number: number,
    countryCode: country_code,
    billingIntervalMonths: billing_interval_months
  }
  Number.new(number_request(:post, 'phone-numbers', params))
end
number_request(method, path, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 65
def number_request(method, path, params = {})
  response_body = @number_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end
number_update(number, tags) click to toggle source

Update a number

# File lib/messagebird/client.rb, line 450
def number_update(number, tags)
  tags = [tags] if tags.is_a? String
  Number.new(number_request(:patch, "phone-numbers/#{number}", tags: tags))
end
parse_body(body) click to toggle source
# File lib/messagebird/client.rb, line 86
def parse_body(body)
  json = JSON.parse(body)

  # If the request returned errors, create Error objects and raise.
  if json.key?('errors')
    raise(ErrorException, json['errors'].map { |e| Error.new(e) })
  end

  json
end
request(method, path, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 79
def request(method, path, params = {})
  response_body = @http_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end
send_conversation_message(from, to, params = {}) click to toggle source
Conversations

Send a conversation message

# File lib/messagebird/client.rb, line 99
def send_conversation_message(from, to, params = {})
  ConversationMessage.new(conversation_request(
                            :post,
                            'send',
                            params.merge(from: from,
                                         to: to)
  ))
end
start_conversation(to, channel_id, params = {}) click to toggle source

Start a conversation

# File lib/messagebird/client.rb, line 109
def start_conversation(to, channel_id, params = {})
  Conversation.new(conversation_request(
                     :post,
                     'conversations/start',
                     params.merge(to: to,
                                  channel_id: channel_id)
  ))
end
verify(id) click to toggle source

Retrieve the information of specific Verify.

# File lib/messagebird/client.rb, line 207
def verify(id)
  Verify.new(request(:get, "verify/#{id}"))
end
verify_create(recipient, params = {}) click to toggle source

Generate a new One-Time-Password message.

# File lib/messagebird/client.rb, line 217
def verify_create(recipient, params = {})
  Verify.new(request(
               :post,
               'verify',
               params.merge(recipient: recipient)
  ))
end
verify_delete(id) click to toggle source

Delete a Verify, response is empty

# File lib/messagebird/client.rb, line 231
def verify_delete(id)
  request(:delete, "verify/#{id}")
end
verify_email_message(id) click to toggle source

Retrieve the information of specific Verify email message

# File lib/messagebird/client.rb, line 212
def verify_email_message(id)
  VerifyEmailMessage.new(request(:get, "verify/messages/email/#{id}"))
end
verify_token(id, token) click to toggle source

Verify the One-Time-Password.

# File lib/messagebird/client.rb, line 226
def verify_token(id, token)
  Verify.new(request(:get, "verify/#{id}?token=#{token}"))
end
voice_message(id) click to toggle source

Retrieve the information of a specific voice message.

# File lib/messagebird/client.rb, line 263
def voice_message(id)
  VoiceMessage.new(request(:get, "voicemessages/#{id}"))
end
voice_message_create(recipients, body, params = {}) click to toggle source

Create a new voice message.

# File lib/messagebird/client.rb, line 268
def voice_message_create(recipients, body, params = {})
  # Convert an array of recipients to a comma-separated string.
  recipients = recipients.join(',') if recipients.is_a?(Array)

  VoiceMessage.new(request(
                     :post,
                     'voicemessages',
                     params.merge(recipients: recipients, body: body.to_s)
  ))
end
voice_request(method, path, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 72
def voice_request(method, path, params = {})
  response_body = @voice_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end
voice_transcription_create(call_id, leg_id, recording_id, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 338
def voice_transcription_create(call_id, leg_id, recording_id, params = {})
  Voice::Transcription.new(voice_request(:post, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions", params))
end
voice_transcription_download(call_id, leg_id, recording_id, transcription_id, &block) click to toggle source
# File lib/messagebird/client.rb, line 346
def voice_transcription_download(call_id, leg_id, recording_id, transcription_id, &block)
  @voice_client.request_block(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}.txt", {}, &block)
end
voice_transcription_view(call_id, leg_id, recording_id, transcription_id) click to toggle source
# File lib/messagebird/client.rb, line 350
def voice_transcription_view(call_id, leg_id, recording_id, transcription_id)
  Voice::Transcription.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}"))
end
voice_transcriptions_list(call_id, leg_id, recording_id) click to toggle source
# File lib/messagebird/client.rb, line 342
def voice_transcriptions_list(call_id, leg_id, recording_id)
  Voice::List.new(Voice::Transcription, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions"))
end
voice_webhook(id) click to toggle source
# File lib/messagebird/client.rb, line 291
def voice_webhook(id)
  Voice::Webhook.new(voice_request(:get, "webhooks/#{id}"))
end
voice_webhook_create(url, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 279
def voice_webhook_create(url, params = {})
  Voice::Webhook.new(voice_request(:post, 'webhooks', params.merge(url: url)))
end
voice_webhook_delete(id) click to toggle source
# File lib/messagebird/client.rb, line 295
def voice_webhook_delete(id)
  voice_request(:delete, "webhooks/#{id}")
end
voice_webhook_update(id, params = {}) click to toggle source
# File lib/messagebird/client.rb, line 287
def voice_webhook_update(id, params = {})
  Voice::Webhook.new(voice_request(:put, "webhooks/#{id}", params))
end
voice_webhooks_list(per_page = VoiceList::PER_PAGE, page = VoiceList::CURRENT_PAGE) click to toggle source
# File lib/messagebird/client.rb, line 283
def voice_webhooks_list(per_page = VoiceList::PER_PAGE, page = VoiceList::CURRENT_PAGE)
  Voice::List.new(Voice::Webhook, voice_request(:get, "webhooks?perPage=#{per_page}&page=#{page}"))
end