AfricasTalking SDK
Provides convenient access to the Africa's Talking API from applications written in ruby.
Documentation
Take a look at the API docs here.
Installation
Add this line to your application's Gemfile:
gem "africastalking-ruby"
And then execute:
$ bundle
Usage
The SDK needs to be instantiated using your username and API key, which you can get from the dashboard.
You can use this SDK for either production or sandbox apps. For sandbox, the app username is ALWAYS
sandbox
require "AfricasTalking"
username = 'YOUR_USERNAME' # use 'sandbox' for development in the test environment
apiKey = 'YOUR_API_KEY' # use your sandbox app API key for development in the test environment
@AT=AfricasTalking::Initialize.new(username, apiKey)
You can now make API calls using the @AT object
Token
token = @AT.token
Create authentication Token
token.generateAuthToken phoneNumber
Airtime
Send airtime to phone numbers
airtime = @AT.airtime
airtime.send
optionsrecipients: Contains an hash of arrays containing the following keysphoneNumber: Recipient of airtimeREQUIREDcurrency:3-digit ISO format currency code .REQUIREDamount: Amount sent>= 10 && <= 10Kwith currency e.gKES 100REQUIRED
maxNumRetry: This allows you to specify the maximum number of retries in case of failed airtime deliveries due to various reasons such as telco unavailability. The default retry period is 8 hours and retries occur every 60seconds. For example, settingmaxNumRetry=4means the transaction will be retried every 60seconds for the next 4 hours.OPTIONAL.
Sms
sms = @AT.sms
Send Sms
sms.send
optionsmessage: SMS content.REQUIREDto: A single recipient or a comma separated string of recipients.REQUIREDfrom: Shortcode or alphanumeric ID that is registered with Africa's Talking account.OPTIONALenqueue: Set totrueif you would like to deliver as many messages to the API without waiting for an acknowledgement from telcos.OPTIONALbulkSMSMode: This parameter will be used by the Mobile Service Provider to determine who gets billed for a message sent using a Mobile-Terminated ShortCode. The default value is 1 (which means that the sender (The AfricasTalking account being used ) gets charged).OPTIONALretryDurationInHours: t specifies the number of hours your subscription message should be retried in case it's not delivered to the subscriber.OPTIONAL
Send Premium SMS
sms.sendPremium
optionsmessage: SMS content.REQUIREDkeyword: The keyword to be used for a premium service.REQUIREDlinkId: This parameter is used for premium services to send OnDemand messages. We forward the linkId to your application when the user send a message to your service..REQUIREDto: A single recipient or a comma separated string of recipients.REQUIREDfrom: Shortcode or alphanumeric ID that is registered with Africa's Talking account.OPTIONALenqueue: Set totrueif you would like to deliver as many messages to the API without waiting for an acknowledgement from telcos.OPTIONALbulkSMSMode: This parameter will be used by the Mobile Service Provider to determine who gets billed for a message sent using a Mobile-Terminated ShortCode. The default value is 1 (which means that the sender (The AfricasTalking account being used ) gets charged).OPTIONALretryDurationInHours: t specifies the number of hours your subscription message should be retried in case it's not delivered to the subscriber.OPTIONAL
Fetch Messsages
sms.fetchMessages
optionslastReceivedId: This is the id of the message that you last processed. The default is 0OPTIONAL
Create subscription
sms.createSubcription
optionsshortCode: This is a premium short code mapped to your account.REQUIREDkeyword: Value is a premium keyword under the above short code and mapped to your account.REQUIREDphoneNumber: The phoneNumber to be subscribedREQUIREDcheckoutToken: This is a token used to validate the subscription requestREQUIRED
Fetch Subscription
sms.fetchSubscriptions
optionsshortCode: This is a premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and mapped to your account.REQUIREDlastReceivedId: ID of the subscription you believe to be your last. Defaults to0
Delete Subscription
sms.deleteSubscriptions
optionsshortCode: This is a premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and mapped to your account.REQUIREDphoneNumber: PhoneNumber to be unsubscribedREQUIRED
Voice
voice = @AT.voice
Making a call
voice.call
optionsto: A single recipient or an array of recipients.REQUIRED- array of recipients contains ['2XXXXXXXX', '2XXXXXXXX']
from: Shortcode or alphanumeric ID that is registered with Africa's Talking account.REQUIREDclientRequestId: String sent to your Events Callback URL that can be used to tag the call.OPTIONAL
Fetch queued calls
voice.fetchQueuedCalls
optionsphoneNumber: is phone number mapped to your AfricasTalking account.REQUIRED
Upload media file
voice.uploadMediaFile
optionsurl: The url of the file to upload. Don't forget to start with http://REQUIREDphoneNumber: is phone number mapped to your AfricasTalking account.REQUIRED
Account
account = @AT.account
Fetch User data
account.fetchUserData
Mobile Data
Send mobile data to phone numbers
mobiledata = @AT.mobiledata
mobiledata.send
optionsproductName: This is the application's product name.REQUIREDrecipients: An array of objects containing the following keys:phoneNumber: Recipient of the mobile data.REQUIREDquantity: a numeric value for the amount of mobile data. It is based on the available mobile data package(see "Bundle Package" column of mobile data pricing).REQUIREDunit: The units for the specified data quantity, the format is:MBorGB. It is based on the available mobile data package(see "Bundle Package" column of mobile data pricing).REQUIREDvalidity: The period of the data bundle’s validity this can beDay,Week,BiWeek,Month, orQuarterly. It is based on the available mobile data package (see "Validity" column of mobile data pricing).REQUIREDmetadata: A JSON object of any metadata that you would like us to associate with the request.OPTIONAL
Find Transaction
mobiledata.findTransaction
- Find a mobile data transaction
optionstransactionId: ID of trancation to findREQUIRED
Fetch Wallet Balance
mobiledata.fetchWalletBalance
- Fetch a mobile data product balance
For more information, please read the https://developers.africastalking.com/docs/data/overview
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/AfricasTalkingLtd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.