I am trying to integrate two APIs
Python Code;
import africastalking
import requests
import json
# Initialize SDK
username = "sandbox" # use 'sandbox' for development in the test environment
# use your sandbox app API key for development in the test environment
api_key = "auth_key"
africastalking.initialize(username, api_key)
# Initialize a service e.g. SMS
sms = africastalking.SMS
url = "https://nay-bokani.sparkmeter.cloud/api/v0/sms/outgoing"
payload = json.dumps({
"mark_delivered": False
})
headers = {
'Content-Type': 'application/json',
'Authentication-Token': 'auth_key'
}
response = requests.request("GET", url, headers=headers, data=payload)
# print()
json_response = response.json()
if json_response['status'] == 'success':
if json_response["messages"][0]["timestamp"] == max("timestamp"):
for s in range(len(json_response['messages'])):
response = sms.send(json_response['messages']['text'], [json_response['messages']['phone_number']])
print(response)
print(response.text)
json response
{
"error": null,
"messages": [
{
"id": "46186176-ba91-4072-a93e-0fc089dea750",
"phone_number": "+2348000000000",
"text": "You've just been credited with N10.00 worth of energy",
"timestamp": "2021-06-09T10:14:10.795818"
},
{
"id": "6709099c-22c8-4df5-88e1-24284d8ff61f",
"phone_number": "+2348111111111",
"text": "You've just been credited with N500.00 worth of energy",
"timestamp": "2021-06-09T10:32:11.885605"
}
],
"status": "success"
}
I need to be able to extract the phone number and text and use it to send an auto sms using the sms API. I tried to use parse using the recent timestamp, but that will be a problem when multiple json data is returned.
import json
#cannot use the real sms.send function so define a dummy one
def dummy_sms_send(txt, phone):
print(txt, phone)
json_response = """
{
"error": null,
"messages": [
{
"id": "46186176-ba91-4072-a93e-0fc089dea750",
"phone_number": "+2348000000000",
"text": "You've just been credited with N10.00 worth of energy",
"timestamp": "2021-06-09T10:14:10.795818"
},
{
"id": "6709099c-22c8-4df5-88e1-24284d8ff61f",
"phone_number": "+2348111111111",
"text": "You've just been credited with N500.00 worth of energy",
"timestamp": "2021-06-09T10:32:11.885605"
}
],
"status": "success"
}
"""
decoded_response = json.loads(json_response)
if decoded_response["status"] == "success":
for msg in decoded_response["messages"]:
dummy_sms_send(msg["text"], msg["phone_number"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.