45
loading...
This website collects cookies to deliver better user experience
Once you've got the above sorted :
- Create a new directory and change into it.
mkdir Automated-Airtime-Disbursal
cd Automated-Airtime-Disbursal
python -m venv .
source bin/activate
pip install africastalking python-dotenv gspread
pip freeze > requirements.txt
Go to the Google Cloud Platform Console, click on New Project / Projects → New Project, on the GCP Toolbar.
Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
Select JSON key type and press “Create”.
{
"type": "service_account",
"project_id": "api-project-XXX",
"private_key_id": "2cd … ba4",
"private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "473 … hd.apps.googleusercontent.com",
...
}
airtime-credentials.json
. Go back to the Google Console, in our current project. Click on the credentials tab, copy theclient_email
gspread.exceptions.SpreadsheetNotFound
exception when trying to access this spreadsheet from your application or a script.
touch airtime_disbursal.py
# airtime_disbursal.py
import os
import africastalking as at
from dotenv import load_dotenv
import gspread
get_spreadsheet_data
that accepts spreadsheet name and index of required worksheet as arguments. The function will query for the specified spreadsheet and worksheet then return all the values except the column headers.def get_spreadsheet_data(sheet_name, worksheet_index):
#make a request to Google specifying the spreadsheet required
sheet = gc.open(sheet_name).get_worksheet(worksheet_index)
# by specifying the index we remove the column headers
return sheet.get_all_values()[1:]
sheet_index = 0
airtime_sheet_name = 'Contact Information (Responses)'
sheet_data = get_spreadsheet_data(airtime_sheet_name, sheet_index)
print(sheet_data)
.env
file in our working folder to hold all of our environment variables that we dont want to expose to everyone. Enter the following changing the placeholders with the proper credentials.touch .env
# .env
at_username=enter your at_username
at_api_key=enter your api_key
# Load our sensitive information using environment variables
load_dotenv()
# get the environment values from the .env file
at_username = os.getenv('at_username')
at_api_key = os.getenv('at_api_key')
# initialize africas talking using username and api key
at.initialize(at_username, at_api_key)
airtime = at.Airtime
account = at.Application
load_dotenv()
function to load our sensitive data from environment variables. We then assign variables to each environment value. We then instantiate Africas Talking client by passing our api_key
and at_username
variables. Finally, we assign the Airtime and Application classes to variables.def airtime_disbursal(number, airtime_amount: str, airtime_currency_code: str):
print(account.fetch_application_data())
try:
response = airtime.send(phone_number=number, amount=amount, currency_code=currency_code)
print(response)
except Exception as e:
print(f"Encountered an error while sending airtime. More error details below\n {e}")
airtime_disbursal
function that accepts 3 arguments: number (phone number of each recipient),fetch_application_data()
method. Finally we write a try-catch block to hold the logic for sending airtime. Here we pass our arguments as needed. Read the docs for more information.# Set The 3-Letter ISO currency code and the amount
amount = "5"
currency_code = "KES"
# Unpack the list of values
for item in sheet_data:
print(item[4])
mobile_number = item[4]
# for each number in the sheet send airtime top-up as specified.
airtime_disbursal(mobile_number, amount, currency_code)
print(account.fetch_application_data())
sheet_data
list. The last item in the list contains the required phone number. We proceed to assign it to a variable.airtime_disbursal
function on each value on the list. The function will run passing along a different phone number on each run, until the list is complete. We then query the balance of our account after the completion of the loop.heroku create --app automatic-airtime
. If you go on your app dashboard you'll see your new app.echo "python-3.9.2" > runtime.txt
heroku config:set at_api_key=api_key_here
heroku config:set at_username=Username_here
git init
heroku git:remote -a heroku create --app automatic-airtime
git add .
git commit -am 'Deploy airtime disbursal script'
git push heroku master