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
$ pip3 install africastalking # python 3.8.x
OR
$ python3 -m pip install africastalking # python 3.8.x
Usage
The package needs to be configured with your app username and API key, which you can get from the dashboard. Note that all functions accept an optional timeout argument, but have a set default.
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, timeout=(3, 6))
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:
- SMS:
africastalking.SMS - Airtime:
africastalking.Airtime - Voice:
africastalking.Voice - Token:
africastalking.Token - Application:
africastalking.Application - Mobile Data:
africastalking.MobileData - Insights:
africastalking.Insights - Whatsapp:
africastalking.Whatsapp
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 keysphone_number: Recipient of airtimeamount: Amount to send with currency e.g100currency_code: 3-digit ISO format currency code (e.gKES,USD,UGXetc).
-
-
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, settingmax_num_retry=4means 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.REQUIREDrecipients: An array of phone numbers.REQUIREDsender_id: Shortcode or alphanumeric ID that is registered with your Africa's Talking account.enqueue: Set totrueif 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 SMSmessage: SMS content.REQUIREDshort_code: Your premium product shortCode.REQUIREDrecipients: An array of phone numbers.REQUIREDkeyword: Your premium product keyword.link_id: We forward thelinkIdto your application when a user sends a message to your onDemand serviceretry_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 messageslast_received_id: This is the id of the message you last processed. Defaults to0
-
create_safaricom_subscription(short_code: str, keyword: str, phone_number: str, request_id: str, redirect_url: str, source_ip: str, user_agent: str): Create a premium Safaricom subscriptionshort_code: Premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and is also mapped to your account.REQUIREDrequest_id: Request id associated with the request.redirect_url: URL that the user will be redirected to after they have confirmed their subscription.phone_number: PhoneNumber to be subscribed.source_ip: IP address the subscribing party is originating from.user_agent: String stating which browser was used to access the content.
Voice
-
call(callFrom: str, callTo: [str]): Initiate a phone callcallFrom: Phone number on Africa's Talking (in international format).REQUIREDcallTo: An array of phone numbers that you wish to dial (in international format).REQUIRED
-
fetch_queued_calls(phone_number: str): Get queued callsphone_number: Phone number mapped to your Africa's Talking account (in international format).REQUIRED
-
upload_media_file(phone_number: str, url: str): Upload voice media filephone_number: phone number mapped to your Africa's Talking account (in international format).REQUIREDurl: The url of the file to upload. Should start withhttp(s)://.REQUIRED
Token
generate_auth_token(): Generate an auth token to use for authentication instead of an API key.
MobileData
-
send(product_name: str, recipients: dict): Send mobile data to customers.product_name: Payment product on Africa's Talking.REQUIREDrecipients: A list of recipients. Each recipient has:phoneNumber: Customer phone number (in international format).REQUIREDquantity: Mobile data amount.REQUIREDunit: Mobile data unit. Can either beMBorGB.REQUIREDvalidity: How long the mobile data is valid for. Must be one ofDay,WeekandMonth.REQUIREDmetadata: 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.
Insights
check_sim_swap_state(phone_numbers: [str]): Check the sim swap state of a given [array of ] phone number(s).
Whatsapp
-
send(body: dict, wa_number: str, phone_number: str): Send a whatsapp message to a given phone number.wa_number: The number being used to send the message that is associated with the account.REQUIREDphone_number: The number that is to receive the message.REQUIREDbody: The message to be sent. The message has a combination of the following:message: The message to be sent to the client.OPTIONALmediaType: The type of message being sent Can be one ofImage,Video,AudioorVoice.OPTIONALurl: The hosted URL of what is being sent.OPTIONALcaption: The caption associated with an image or video that is being sent.OPTIONALaction: A dictionary with a list of actions.OPTIONALbody: A dictionary containing what is being sent with the interactive button or list.OPTIONALheader: A dictionary containing what header is being sent with the interactive button or list.OPTIONALfooter: A dictionary containing what footer is being sent with the interactive button or list.OPTIONAL
-
send_template(component: dict, wa_number: str, name: str, language: str, category: str): Send a Whatsapp template for your future messages.wa_number: The Whatsapp phone number that will be used to send the messages associated with the template.REQUIREDname: The name of the template. This must be unique.REQUIREDlanguage: The language code associated with the template.REQUIREDcategory: The category associated with the template.REQUIREDcomponent: A complex type containing the values that will be in the template. It can contain the following types:header: The header of the template to be sent.OPTIONALbody: The type of message being sent in the body of the template.OPTIONALfooter: The footer of the template to be sent.OPTIONALbuttons: A list of buttons to be sent in the template.OPTIONAL
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
$ python3 -m unittest
Issues
If you find a bug, please file an issue on our issue tracker on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file africastalking-2.0.2.tar.gz.
File metadata
- Download URL: africastalking-2.0.2.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a09185a692dbba5a0f894c19f8d03454474fbd24922bc8e5b977f693bf89565e
|
|
| MD5 |
cc88616aaf3f619bfeb862851dc36059
|
|
| BLAKE2b-256 |
4aca3772eccf9f89f18a5dbe4dcb3611b3ef2da8b9e6a5e361e4b1d477046014
|
Provenance
The following attestation bundles were made for africastalking-2.0.2.tar.gz:
Publisher:
python-publish.yml on AfricasTalkingLtd/africastalking-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
africastalking-2.0.2.tar.gz -
Subject digest:
a09185a692dbba5a0f894c19f8d03454474fbd24922bc8e5b977f693bf89565e - Sigstore transparency entry: 804463065
- Sigstore integration time:
-
Permalink:
AfricasTalkingLtd/africastalking-python@a4146e04c92b4f63587ce1a1dc0a7f9417ffe50a -
Branch / Tag:
refs/tags/v2.0.2 - Owner: https://github.com/AfricasTalkingLtd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a4146e04c92b4f63587ce1a1dc0a7f9417ffe50a -
Trigger Event:
release
-
Statement type:
File details
Details for the file africastalking-2.0.2-py3-none-any.whl.
File metadata
- Download URL: africastalking-2.0.2-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e44240112070dec8527af9f7b0ae30773f1c52933017132bef12922bd396085a
|
|
| MD5 |
f6dce40e07ed78ce3964fe59960832e6
|
|
| BLAKE2b-256 |
5d9086cf872b7952fa4e1d6bf1e94cf5bcb38a5920388e202b21d653cab274fe
|
Provenance
The following attestation bundles were made for africastalking-2.0.2-py3-none-any.whl:
Publisher:
python-publish.yml on AfricasTalkingLtd/africastalking-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
africastalking-2.0.2-py3-none-any.whl -
Subject digest:
e44240112070dec8527af9f7b0ae30773f1c52933017132bef12922bd396085a - Sigstore transparency entry: 804463085
- Sigstore integration time:
-
Permalink:
AfricasTalkingLtd/africastalking-python@a4146e04c92b4f63587ce1a1dc0a7f9417ffe50a -
Branch / Tag:
refs/tags/v2.0.2 - Owner: https://github.com/AfricasTalkingLtd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a4146e04c92b4f63587ce1a1dc0a7f9417ffe50a -
Trigger Event:
release
-
Statement type: