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[0] != '+' or sender[1:].isdigit() == False: # Try to find the sender's number from log. logged_sms = logs.sms(mode = 'in')[0] # 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[0].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.
hey line 23 what is sms_text supposed to refer to? and why only everything before the 64th digit? is that inbox.Inbox.content(msg_id) ?
thanks in advance
Comment by am — February 1, 2011 @ 4:36 am
The 64 characters limits is because the “subject” in of texts in logs are only 64 characters long so we can check only that portion. sms_text seems to be a bug but yes it is supposed to be the contents of given message id. I don’t have PyS60 myself or I would’ve checked that right now.
Comment by krkhan — February 1, 2011 @ 1:49 pm