npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hubtel-ussd

v1.1.0

Published

An unofficial nodejs wrapper for processing hubtel ussd requests

Downloads

21

Readme

Hubtel USSD

Based on Hubtel's USSD API

About

This package helps you to build maintainable and scalable USSD applications by breaking down your "long switch" statement USSD application into modules

Getting Started

Check out this link to know how you can acquire a USSD code from Hubtel.

Installing

Install the package from npm

npm install --save hubtel-ussd

Usage

Check the example folder for an example using expressjs

  //app.js 
const Hubtel = require('hubtel-ussd');
const HubtelUSSD = new Hubtel();
const HubtelRequest = Hubtel.Request;

//setup CORS
const cors = require('cors');

app.use(cors());

//route which serves as the endpoint to your ussd application
app.post('/', async(req, res) => {
  // we use underscorejs to convert the request body to an array and use the spread operator
  let request = new HubtelRequest(... _(req.body).toArray());
  
  /**
    * Pass the instance of the HubtelRequest (request) as the first parameter to HubtelUSSD.process
    * create a folder for our sequences
    * and require the seqeunces into an array as the second parameter for HubtelUSSD.process
    **/
    let ussdResponse = await HubtelUSSD.process(request,[
        require('./my_sequence_dir/sequence1'),
        require('./my_sequence_dir/sequence2'),
        require('./my_sequence_dir/sequence3')
    ]);
    res.json(ussdResponse);
});
});

Done :blush:

Api

ResponseTypes is an object with two properties response and release for indicating the type of response in a sequence.

  const hubtel = require('hubtel-ussd');
  const ResponseTypes =hubtel.ResponseTypes;
  
  ResponseTypes.response  // 'Response'
  ResponseTypes.release // 'Release'

HubtelUSSD is a class with one method process which takes two parameters a Request (an instance of HubtelUSSDReqeust) and an array of sequence;

  const Hubtel = require('hubtel-ussd');
  const HubtelUSSD = new Hubtel();
  const HubtelRequest = Hubtel.Request;
  
  let request = new HubtelRequest(/** populate properties **/);
  let response = await HubtelUSSD.process(request,[/** an array of sequence **/);
  //response is an instance of HubtelUSSDResponse
  

All seqeunces must have a handle method and must return an instance of HubtelUSSDResponse (Response). The package automagically injects a ussdRequest parameter into the handle method of each sequence. This ussdRequest is an instace of HubtelUSSDRequest which grants access to all properties and methods in the request.

//sequence1.js
const hubtel = require('hubtel-ussd');
const Response = hubtel.Response;
const ResponseTypes = hubtel.ResponseTypes;

class Sequence1{
    /*
     * A ussdRequest object which is an instance of HubtelUSSDRequest
     *  this gives us access to methods such as {mobile,sessionId,serviceCode,type} etc 
     **/
    async handle(ussdRequest){
        //ussdRequest.mobile --- returns the mobile number of the user performing the request. etc
        return new Response('Welcome to Freebie Service.\n1. Free Food\n2. Free Drink\n3. Free Airtime',ResponseTypes.response);
    }
}

module.exports = Sequence1;

Visit Hubtel's USSD Api documentation page for the list of methods on the request and response classes

Running the tests

npm test

Authors

License

This project is licensed under the MIT License