Coder Social home page Coder Social logo

jniles / africastalking-node.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from africastalkingltd/africastalking-node.js

0.0 3.0 0.0 53 KB

Official AfricasTalking node.js API wrapper

License: GNU General Public License v3.0

JavaScript 100.00%

africastalking-node.js's Introduction

africastalking-node.js

Official AfricasTalking node.js API wrapper

Install

$ npm install --save africastalking

Initialization

var options = {
    apiKey: 'YOUR_API_KEY',
    username: 'YOUR_USERNAME',
    format: 'json' // or xml
};
var AfricasTalking = require('africastalking')(options);
// ...

Important: If you register a callback URL with the API, always remember to acknowledge the receipt of any data it sends by responding with an HTTP 200; e.g. res.status(200); for express.

SMS

var sms = AfricasTalking.SMS;
// all methods return a promise
sms.send(opts)
    .then(success)
    .catch(error);
  • send(options): Send a message. options contains:

    • to: A single recipient or an array of recipients. REQUIRED
    • from: Shortcode or alphanumeric ID that is registered with Africa's Talking account.
    • message: SMS content. REQUIRED
  • sendBulk(options): Send bulk SMS. In addition to paramaters of send(), we would have:

    • enqueue: "[...] would like deliver as many messages to the API before waiting for an Ack from the Telcos."
  • sendPremium(options): Send premium SMS. In addition to paramaters of send(), we would have:

    • keyword: Value is a premium keyword REQUIRED
    • linkId: "[...] We forward the linkId to your application when the user send a message to your service" REQUIRED
    • retryDurationInHours: "It specifies the number of hours your subscription message should be retried in case it's not delivered to the subscriber"

You can register a callback URL with us and we will forward any messages that are sent to your account the moment they arrive. Read more

  • fetchMessages(options): Manually retrieve your messages.

    • lastReceivedId: "This is the id of the message that you last processed". Defaults to 0. REQUIRED

If you have subscription products on your premium SMS short codes, you will need to configure a callback URL that we will invoke to notify you when users subscribe or unsubscribe from your products. Read more

  • createSubscription(options):

    • shortCode: "This is a premium short code mapped to your account". REQUIRED
    • keyword: "Value is a premium keyword under the above short code and mapped to your account". REQUIRED
    • phoneNumber: "The phoneNumber to be subscribed" REQUIRED
  • fetchSubscription(options):

    • shortCode: "This is a premium short code mapped to your account". REQUIRED
    • keyword: "Value is a premium keyword under the above short code and mapped to your account". REQUIRED
    • lastReceivedId: "ID of the subscription you believe to be your last." Defaults to 0

Processing USSD requests using our API is very easy once your account is set up. In particular, you will need to:

  • Register a service code with us.
  • Register a URL that we can call whenever we get a request from a client coming into our system.

Once you register your callback URL, any requests that we receive belonging to you will trigger a callback that sends the request data to that page using HTTP POST. Read more.

If you are using connect-like frameworks (express), you could use the middleware AfricasTalking.USSD(handler):

handler(params, next): Process USSD request and call next() when done.

  • params: contains the following user data sent by Africa's Talking servers: sessionId, serviceCode, phoneNumber and text.
  • next(args): args must contain the following:
    • response: Text to display on user's device. REQUIRED
    • endSession: Boolean to decide whether to END session or to CONtinue it. REQUIRED
// example (express)

app.post('/natoil-ussd', new AfricasTalking.USSD((params, next) => {
    var endSession = false;
    var message = '';
    
    var session = sessions.get(params.sessionId);
    var user = db.getUserByPhone(params.phoneNumber);

    if (params.text === '') {
        message = "Welcome to Nat Oil \n";
        message += "1: For account info \n";
        message += "2: For lost gas cylinder";

    } else if (params.text === '1') {
        message = user.getInfo();
        endSession = true;

    } else if (params.text === '2') {
        message = "Enter 1 for recovery \n";
        message += "Enter 2 for lost and found";
        endSession = true;

    } else {
        message = "Invalid option";
        endSession = true;
    }

    next({
        response: message, 
        endSession: endSession
    });
}));

Voice

var voice = AfricasTalking.VOICE;
  • Helpers that will construct proper xml to send back to Africa's Taking API when it comes POSTing. Read more
    • Say, Play, GetDigits, Dial, Record, Enqueue, Dequeue, Conference, Redirect, Reject
  • Initiate a call
  • Fetch call queue
  • Media upload - any url to Play will be cached by default.
  • Remember to send back an HTTP 200.
  voice.call({
    callFrom: '+2547XXXXXXXX', // AT virtual number
    callTo: from_ 
  })
  .then(function(s) {
    // persist call Info
    console.log(s);
  })
  .catch(function(error) {
    console.log(error);
  });
  voice.getNumQueuedCalls({ 
    phoneNumbers: destinationNumber 
  })
  .then(function(s) {
    // call queue
    console.log(s);
  })
  .catch(function(error) {
    console.log(error);
  });

Handle call **TODO Build helpers

check issue #15

Airtime

var airtime = AfricasTalking.AIRTIME;
  • airtime.send(options): Send airtime

    • recipients: An array of the following
      • phoneNumber: Recipient of airtime
      • amount: Amount sent. >= 10 && <= 10K
   airtime.send(options)
    .then(success)
    .catch(error);

Account

AfricasTalking.fetchAccount()
    .then(success)
    .catch(error);
  • fetchAccount(): Fetch account info; i.e. balance

Mobile Money (mpesa) - check out

var payment = AfricasTalking.PAYMENT;

payment.checkOut({
  phoneNumber  : '+2547XXXXXXXX',
  productName  : 'your_productName',
  currencyCode : 'KES',
  metadata     : { id: uuid.v4() },
  amount       : 100
})
.then(function(s) {
    // persist payment status
    console.log(s);
})
.catch(function(error) {
    console.log(error);
});

B2B (mpesa)

???

B2C (mpesa)

???

africastalking-node.js's People

Contributors

ianjuma avatar aksalj avatar habbes avatar calvinkarundu avatar roboflank avatar

Watchers

James Cloos avatar Jonathan Niles avatar  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.