Class: AfricasTalking::Message

Inherits:
Base
  • Object
show all
Defined in:
lib/africas_talking/message.rb

Constant Summary

Constants inherited from Base

Base::BASE_URI

Instance Method Summary collapse

Methods inherited from Base

#api_error_messages, #build_messages_array, #get, #headers, #parse_api_errors, #parse_api_response, #post, #prepare_recipients, #process_api_response

Instance Method Details

#deliver(recipients, message, username) ⇒ Object


Specify the numbers that you want to send to in a comma-separated list Please ensure you include the country code (+254 for Kenya in this case) to = “+254711XXXYYY,+254733YYYZZZ”; And of course we want our recipients to know what we really do



10
11
12
# File 'lib/africas_talking/message.rb', line 10

def deliver(recipients, message, username)
  post('/version1/messaging', {username: username, message: message, to: prepare_recipients(recipients)})
end

#deliver_premium_messages(opts) ⇒ Object

POST /version1/messaging#premium_messages


Required fields to deliver a premium message through the API########

To send a premium message, build a HASH (as shown below) with the following attributes keeping in mind the defaults. 1.Specify your premium shortCode and keyword 2.Set keyword as None where not used (Mostly for onDemand services) 3.Set the bulkSMSMode flag to 0 so that the subscriber gets charged 4.Set the enqueue flag to 0 so that your message will not be queued or to 1 for many messages 5.Incase of an onDemand service, specify the link id. else set it to nil 6.linkId is received from the message sent by subscriber to your onDemand service 7.Specify retryDurationInHours: The numbers of hours our API should retry to send the message incase it doesn’t go through. opts=“0710335602”, message: “Hey there testing something awesome”, bulkSMSMode: 0, shortCode:“XXXXX”, enqueue: 0, keyword: nil, linkId: “messageLinkId”



57
58
59
60
61
62
# File 'lib/africas_talking/message.rb', line 57

def deliver_premium_messages(opts)
  post('/version1/messaging', {
      to: opts.fetch(:recipients), message: opts.fetch(:message),
      keyword: opts.fetch(:keyword, nil), enqueue: opts.fetch(:enqueue, 0), username: opts.fetch(:username),
      linkId: opts.fetch(:linkId, nil), retryDurationInHours: opts.fetch(:retryDurationInHours, 1)})
end

#deliver_with_shortcode(recipients, message, from, username) ⇒ Object

POST /version1/messaging


Specify your AfricasTalking shortCode or sender id sender = “shortCode or senderId”



20
21
22
# File 'lib/africas_talking/message.rb', line 20

def deliver_with_shortcode(recipients, message, from, username)
  post('/version1/messaging', {username: username, message: message, to: recipients, from: from})
end

#enqueue_messages(opts) ⇒ Object

POST /version1/messaging


sender = nil # bulkSMSMode # This should always be 1 for bulk messages # enqueue flag is used to queue messages incase you are sending a high volume. # The default value is 0. opts=1, bulkSMSMode: 1, sender: “shortCode/senderId” gateway = AfricasTalkingGateway.new(username, apikey) reports = gateway.sendMessage(to, message, sender, bulkSMSMode, enqueue)



35
36
37
38
39
# File 'lib/africas_talking/message.rb', line 35

def enqueue_messages(opts)
  post('/version1/messaging', {
    to: opts.fetch(:recipients), message: opts.fetch(:message, ""), sender: opts.fetch(:sender, nil),
    enqueue: opts.fetch(:enqueue,1), bulkSMSMode: opts.fetch(:bulkSMSMode, 1), username: opts.fetch(:username)})
end

#fetch_messages(username, last_received_id = 0) ⇒ Object

GET /?username=#'africas_talking_username'&lastReceivedId=#last_received_id The gateway will return 10 messages at a time back to you, starting with what you currently believe is the lastReceivedId. Specify 0 for the first time you access the gateway, and the ID of the last message we sent you on subsequent results



71
72
73
# File 'lib/africas_talking/message.rb', line 71

def fetch_messages(username, last_received_id=0)
	get("/version1/messaging?username=#{username}&lastReceivedId=#{last_received_id}")
end