Giter VIP home page Giter VIP logo

africastalking-node.js's Introduction

Africa's Talking Node.js SDK

NPM

The wrapper provides convenient access to the Africa's Talking API from applications written for Node.js.

Documentation

Take a look at the API docs here.

Install

You can install the package from npm by running:

$ npm install --save africastalking

Usage

The package needs to be configured with your app 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

const credentials = {
    apiKey: 'YOUR_API_KEY',         // use your sandbox app API key for development in the test environment
    username: 'YOUR_USERNAME',      // use 'sandbox' for development in the test environment
};
const Africastalking = require('africastalking')(credentials);

// Initialize a service e.g. SMS
const sms = Africastalking.SMS

// Use the service
const options = {
    to: ['+254711XXXYYY', '+254733YYYZZZ'],
    message: "I'm a lumberjack and its ok, I work all night and sleep all day"
}

// Send message and capture the response or error
sms.send(options)
    .then( response => {
        console.log(response);
    })
    .catch( error => {
        console.log(error);
    });

See example for more usage examples.

Initialization

Initialize the SDK as a requirement by doing require('africastalking')(options). After initialization, you can get instances of offered services as follows:

Services

All methods are asynchronous

All phone numbers use the international format. e.g. +234xxxxxxxx.

ApplicationService

  • fetchApplicationData(): Get app information. e.g. balance

For more information, please read https://developers.africastalking.com/docs/application

AirtimeService

  • airtime.send({ recipients }): Send airtime to a bunch of phone numbers.

  • recipients: An array of objects containing the following keys:

    • phoneNumber: Recipient of airtime. REQUIRED.
    • currencyCode: 3-digit ISO format currency code. REQUIRED.
    • amount: Amount to charge. REQUIRED.
  • 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, setting maxNumRetry=4 means the transaction will be retried every 60seconds for the next 4 hours.OPTIONAL.

  • Example:

    airtime.send({
        recipients: [
            {
                phoneNumber: '+xxxxxxxxxxxx',
                currencyCode: 'KES',
                amount: 90
            },
            {
                phoneNumber: '+xxxxxxxxxxxx',
                currencyCode: 'KES',
                amount: 897
            }
        ],
        maxNumRetry: 3, // Will retry the transaction every 60seconds for the next 3 hours.
    });

For more information, please read https://developers.africastalking.com/docs/airtime/sending

SmsService

  • Send a message to one recipient.

        send({
          to: '+xxxxxxxxxxxx',
          from: 'XYZ LTD',
          message: 'Hello world',
          enqueue: true,
        });
  • Send a message to multiple recipients.

        send({
          to: ['+xxxxxxxxxxxx','+yyyyyyyyyyyy','+zzzzzzzzzzzz'],
          from: 'XYZ LTD',
          message: 'Hello world',
          enqueue: true,
        });
  • Send different messages to different recipients.

        send([
          {
            to: ['+aaaaaaaaaaaa','+bbbbbbbbbbbb','+cccccccccccc'],
            from: 'XYZ LTD',
            message: 'Congratulations team! You have won it!',
            enqueue: true,
          },
          {
            to: '+xxxxxxxxxxxx',
            from: 'XYZ LTD',
            message: 'Congratulations coach! Your team has won!',
            enqueue: true,
          }
        ]);
    • to: Recipient(s) phone number. REQUIRED
    • from: Shortcode or alphanumeric ID that is registered with Africa's Talking account
    • message: SMS content. REQUIRED
    • enqueue: Set to true if you would like to deliver as many messages to the API without waiting for an acknowledgement from telcos.
  • sendPremium({ to, from, message, enqueue, keyword, linkId, retryDurationInHours }): Send premium SMS

    • send() parameters plus:
    • keyword: You premium product keyword
    • linkId: We forward the linkId to your application when the user send a message to your service
    • retryDurationInHours: It specifies the number of hours your subscription message should be retried in case it's not delivered to the subscriber
  • fetchMessages({ lastReceivedId }): Manually retrieve your messages

    • lastReceivedId: "This is the id of the message that you last processed". Defaults to 0
  • fetchSubscription({ shortCode, keyword, lastReceivedId }): Fetch your premium subscription data

    • shortCode: This is the premium short code mapped to your account. REQUIRED
    • keyword: A premium keyword under the above short code and mapped to your account. REQUIRED
    • lastReceivedId: "This is the id of the message that you last processed". Defaults to 0
  • createSubscription({ shortCode, keyword, phoneNumber }): Create a premium subscription

    • shortCode: This is the premium short code mapped to your account. REQUIRED
    • keyword: A premium keyword under the above short code and mapped to your account. REQUIRED
    • phoneNumber: The phone number to be subscribed. REQUIRED

For more information on:

VoiceService

  • voice.call({ callFrom, callTo }): Initiate a phone call

    • callFrom: Your Africa's Talking issued virtual phone number. REQUIRED
    • callTo: Comma-separated string of phone numbers to call. REQUIRED
    • clientRequestId: Additional information that can be used to tag the call in your callback URL.
  • voice.fetchQuedCalls({ phoneNumber }): Get queued calls

    • phoneNumber: Your Africa's Talking issued virtual phone number. REQUIRED
  • voice.uploadMediaFile({ phoneNumber, url }): Upload voice media file

    • phoneNumber: Your Africa's Talking issued virtual phone number. REQUIRED
    • url: URL to your media file. REQUIRED

Helpers that will construct proper xml to send back to Africa's Taking API when it comes POSTing.

  • Say, Play, GetDigits, Dial, Record, Enqueue, Dequeue, Conference, Redirect, Reject

Remember to send back an HTTP 200.

For more information, please read https://developers.africastalking.com/docs/voice/overview and issue #15

TokenService

  • generateAuthToken(): Generate an auth token to use for authentication instead of an API key.

USSD

For more information, please read https://developers.africastalking.com/docs/ussd/overview

Development

Run all tests:

$ npm install
$ npm test

or on Windows...

$ npm install
$ npm run test-windows

Issues

If you find a bug, please file an issue on our issue tracker on GitHub.

africastalking-node.js's People

Contributors

aksalj avatar calvinkarundu avatar daggieblanqx avatar ianjuma avatar kevokevoh avatar tawn33y avatar habbes avatar pbombo avatar dependabot[bot] avatar mbuguanewton avatar cooleraid avatar crispinkoech avatar gachez avatar favourori avatar mayomi1 avatar alpha1202 avatar roboflank avatar ndrymes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.