Package: africastalking
africastalking-pythonThe SDK provides convenient access to the Africa's Talking APIs to python apps.DocumentationTake a look at theAPI docs here.Install$pipinstallafricastalking# python 2.7.xOR

$python-mpipinstallafricastalking# python 2.7.xOR

$pip3installafricastalking# python 3.6.xOR

$python3-mpipinstallafricastalking# python 3.6.xUsageThe package needs to be configured with your app username and API key, which you can get from thedashboard.You can use this SDK for either production or sandbox apps. For sandbox, the app username isALWAYSsandbox# import packageimportafricastalking# Initialize SDKusername="YOUR_USERNAME"# use 'sandbox' for development in the test environmentapi_key="YOUR_API_KEY"# use your sandbox app API key for development in the test environmentafricastalking.initialize(username,api_key)# Initialize a service e.g. SMSsms=africastalking.SMS# Use the service synchronouslyresponse=sms.send("Hello Message!",["+2547xxxxxx"])print(response)# Or use it asynchronouslydefon_finish(error,response):iferrorisnotNone:raiseerrorprint(response)sms.send("Hello Message!",["+2547xxxxxx"],callback=on_finish)Seeexamplefor more usage examples.InitializationInitialize the SDK by callingafricastalking.initialize(username, api_key). After initialization, you can get instances of offered services as follows:SMS:africastalking.SMSAirtime:africastalking.AirtimeVoice:africastalking.VoiceToken:africastalking.TokenApplication:africastalking.ApplicationMobileData:africastalking.MobileDataApplicationfetch_application_data(): Get app information. e.g balance.Airtimesend(recipients: [dict]): Send airtimerecipients: 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.Smssend(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 subscriberfetch_messages(last_received_id: int = 0): Fetch your messageslast_received_id: This is the id of the message you last processed. Defaults to0create_subscription(short_code: str, keyword: str, phone_number: str): Create a premium subscriptionshort_code: Premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and is also mapped to your account.REQUIREDphone_number: PhoneNumber to be subscribedREQUIREDfetch_subscriptions(short_code: str, keyword: str, last_received_id: int = 0): Fetch your premium subscription datashort_code: Premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and mapped to your account.REQUIREDlast_received_id: ID of the subscription you believe to be your last. Defaults to0delete_subscription(short_code: str, keyword: str, phone_number: str): Delete a phone number from a premium subscriptionshort_code: Premium short code mapped to your account.REQUIREDkeyword: Premium keyword under the above short code and is also mapped to your account.REQUIREDphone_number: PhoneNumber to be subscribedREQUIREDMobileDatasend(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.REQUIREDisPromoBundle: This is an optional field that can be eithertrueandfalse.OPTIONALmetadata: Additional data to associate with the transaction.OPTIONALfind_transaction(transaction_id: str): Find a mobile data transaction.fetch_wallet_balance(): Fetch a mobile data product balance.Tokengenerate_auth_token(): Generate an auth token to use for authentication instead of an API key.UssdFor more information, please readhttps://developers.africastalking.com/docs/ussdDevelopment$gitclonehttps://github.com/AfricasTalkingLtd/africastalking-python.git
$cdafricastalking-python
$touch.envMake sure your.envfile has the following content then runpython -m unittest discover -v# AT APIUSERNAME=sandboxAPI_KEY=some_keyIssuesIf you find a bug, please file an issue onour issue tracker on GitHub.