Skip to main content

Official Africa's Talking Python SDK

Project description

africastalking-python

The SDK provides convenient access to the Africa's Talking APIs to python apps.

Documentation

Take a look at the API docs here.

Install

$ pip  install africastalking # python 2.7.x

OR

$ python -m pip install africastalking # python 2.7.x

OR

$ pip3 install africastalking # python 3.6.x

OR

$ python3 -m pip install africastalking # python 3.6.x

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

# import package
import africastalking


# Initialize SDK
username = "YOUR_USERNAME"    # use 'sandbox' for development in the test environment
api_key = "YOUR_API_KEY"      # use your sandbox app API key for development in the test environment
africastalking.initialize(username, api_key)


# Initialize a service e.g. SMS
sms = africastalking.SMS


# Use the service synchronously
response = sms.send("Hello Message!", ["+2547xxxxxx"])
print(response)

# Or use it asynchronously
def on_finish(error, response):
    if error is not None:
        raise error
    print(response)

sms.send("Hello Message!", ["+2547xxxxxx"], callback=on_finish)    

See example for more usage examples.

Initialization

Initialize the SDK by calling africastalking.initialize(username, api_key). After initialization, you can get instances of offered services as follows:

Application

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

Airtime

  • send(recipients: [dict]): Send airtime

    • recipients: Contains an array of arrays containing the following keys

      • phone_number: Recipient of airtime
      • amount: Amount to send with currency e.g 100
      • currency_code: 3-digit ISO format currency code (e.g KES, USD, UGX etc).
  • max_num_retry: 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 60 seconds. For example, setting max_num_retry=4 means the transaction will be retried every 60 seconds for the next 4 hours. OPTIONAL.

Sms

  • send(message: str, recipients: [str], sender_id: str = None, enqueue: bool = False): Send a message.

    • message: SMS content. REQUIRED
    • recipients: An array of phone numbers. REQUIRED
    • sender_id: Shortcode or alphanumeric ID that is registered with your Africa's Talking account.
    • enqueue: Set to true if you would like to deliver as many messages to the API without waiting for an acknowledgement from telcos.
  • send_premium(message: str, short_code: str, recipients: [str], link_id: [str] = None, retry_duration_in_hours [int] = None): Send a premium SMS

    • message: SMS content. REQUIRED
    • short_code: Your premium product shortCode. REQUIRED
    • recipients: An array of phone numbers. REQUIRED
    • keyword: Your premium product keyword.
    • link_id: We forward the linkId to your application when a user sends a message to your onDemand service
    • retry_duration_in_hours: This specifies the number of hours your subscription message should be retried in case it's not delivered to the subscriber
  • fetch_messages(last_received_id: int = 0): Fetch your messages

    • last_received_id: This is the id of the message you last processed. Defaults to 0
  • create_subscription(short_code: str, keyword: str, phone_number: str): Create a premium subscription

    • short_code: Premium short code mapped to your account. REQUIRED
    • keyword: Premium keyword under the above short code and is also mapped to your account. REQUIRED
    • phone_number: PhoneNumber to be subscribed REQUIRED
  • fetch_subscriptions(short_code: str, keyword: str, last_received_id: int = 0): Fetch your premium subscription data

    • short_code: Premium short code mapped to your account. REQUIRED
    • keyword: Premium keyword under the above short code and mapped to your account. REQUIRED
    • last_received_id: ID of the subscription you believe to be your last. Defaults to 0
  • delete_subscription(short_code: str, keyword: str, phone_number: str): Delete a phone number from a premium subscription

    • short_code: Premium short code mapped to your account. REQUIRED
    • keyword: Premium keyword under the above short code and is also mapped to your account. REQUIRED
    • phone_number: PhoneNumber to be subscribed REQUIRED

MobileData

  • send(product_name: str, recipients: dict): Send mobile data to customers.

    • product_name: Payment product on Africa's Talking. REQUIRED
    • recipients: A list of recipients. Each recipient has:
      • phoneNumber: Customer phone number (in international format). REQUIRED
      • quantity: Mobile data amount. REQUIRED
      • unit: Mobile data unit. Can either be MB or GB. REQUIRED
      • validity: How long the mobile data is valid for. Must be one of Day, Week and Month. REQUIRED
      • isPromoBundle: This is an optional field that can be eithertrue and false. OPTIONAL
      • metadata: Additional data to associate with the transaction. OPTIONAL
  • find_transaction(transaction_id: str): Find a mobile data transaction.

  • fetch_wallet_balance(): Fetch a mobile data product balance.

Token

  • generate_auth_token(): 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

Development

$ git clone https://github.com/AfricasTalkingLtd/africastalking-python.git
$ cd africastalking-python
$ touch .env

Make sure your .env file has the following content then run python -m unittest discover -v

# AT API
USERNAME=sandbox
API_KEY=some_key

Issues

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

africastalking-1.2.7.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

africastalking-1.2.7-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file africastalking-1.2.7.tar.gz.

File metadata

  • Download URL: africastalking-1.2.7.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for africastalking-1.2.7.tar.gz
Algorithm Hash digest
SHA256 fd3a2368755b5b7c5f5718b75231a4f6151013e6f4818b141428958be32a8637
MD5 3518ee557fa0d24ad9e86796923790fd
BLAKE2b-256 e7fe3d6d7bb61ef6c7e793b6798c0f654be7703aa68e0e749946804d08915098

See more details on using hashes here.

File details

Details for the file africastalking-1.2.7-py3-none-any.whl.

File metadata

  • Download URL: africastalking-1.2.7-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for africastalking-1.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8fbb5fe0b3d0330dd29ae8f12df90830a56e6823153d7d57c970ec4811c5dae9
MD5 f56ab33ccc14fffffa263f146b50f7fd
BLAKE2b-256 7fdb564f8a631f5368ba29e17e301c60d00fb2a1f10e851109237d985058e46a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page