Question

Trouble with Python reserved word "from" as API attribute

  • 28 October 2020
  • 1 reply
  • 129 views

I want to create a list of phone numbers from my message list using this Python script but am hung up on the 'from' attribute since it is a Python reserved word. Not sure what I am doing wrong. This script results in no numbers.

resp = platform.get('/restapi/v1.0/account/~/extension/~/message-store').json()

messList=resp.records

for i in messList:
    
    try:
      fromMess=getattr(i,'from')
      fromMess=fromMess.phoneNumber

    except:
      fromMess='no number'

    print(fromMess)



1 reply

Solved it this way

resp = platform.get('/restapi/v1.0/account/~/extension/~/message-store').json()

messList=resp.records

for i in messList:
    messID=i.id
    
    try:
      fromMess = platform.get('/restapi/v1.0/account/~/extension/~/message-store/'+str(messID))
      fromMess=fromMess.text()
      fromMess=json.loads(fromMess)
      whoMess=fromMess["from"]["phoneNumber"]
      
      print(whoMess)
            
    except:
      print("skipped message")


Reply