Workaround for getting received SMS’ sender number in PyS60
It’s fairly simple in PyS60 to get the sender’s details whenever a new SMS is received. However, if the sender has an entry in your address book these details translate to just the contact name. That way, if the contact has multiple numbers you have no idea which number was used to send the text. Here’s an ugly little workaround for this issue:
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
def message_received(id, box): box.sms_messages() sender = box.address(id) if sender != '+' or sender[1:].isdigit() == False: # Try to find the sender's number from log. logged_sms = logs.sms(mode = 'in') # To confirm that the log entry is the same one we're concerned # about, do some checks. if logged_sms['subject'] == sms_text[:64]: number = logged_sms['number'] contacts_db = contacts.open() found_contact = contacts_db.find(number) if found_contact: title_match = True for token in sender.split(): if found_contact.title.find(token) == -1: title_match = False if title_match: sender = number print "SMS Received from: ", sender
You can use this little snippet of code to reply back to the
sender even when there’s a matching entry in the contacts database. This in turn opens up a whole new range of interesting ideas for auto-responding applications.