Saturday, April 18, 2009

the hard way to get SMS messages in app

My epiphany today was that SMS would be the easiest way to capture expenses like gas and groceries. That's funny, seriously. I decided to see how difficult it would be to get SMS messages into an app. Since I recently set up a Skype account with SkypeIn, I figured I'd see if SkypeIn could accept SMS. In short, it doesn't appear to (still).

The Skype APIs are brilliantly trivial so within a couple minutes of downloading the Python version of the API I had this script running to send my real phone a test SMS message.


import Skype4Py

def OnAttach(status):
print 'API attachment status: ' + skype.Convert.AttachmentStatusToText(status)

if status == Skype4Py.apiAttachAvailable:
skype.Attach()

skype = Skype4Py.Skype()
skype.OnAttachmentStatus = OnAttach

print 'Connecting to Skype...'
skype.Attach()

message = skype.CreateSms(Skype4Py.smsMessageTypeOutgoing, '+15555551234')
message.Body = "Hello from sendSms.py"
message.Send()


That works - I set a real phone number where the 5555551234 is and the real phone gets the SMS instantly. Since I was sure the API was functioning correctly, I tried reading SMS messages. I could see SMS messages so from a real phone I sent an SMS to my SkypeIn number.

Neither Skype (the GUI client) nor the API could see the incoming SMS so I did a little reading and found that landlines are used for SkypeIn so incoming SMS is not an option. I read that there is a company that provides free SMS routing to Skype accounts. The format is 'skype account name message'. Skype then receives the SMS as an Instant Message. I tried it out by sending 'skype mrtidy test another from iPhone' and did get the Instant Message.


import Skype4Py

skype = Skype4Py.Skype()
skype.Attach()

for sms in skype.Smss:
print sms.Type + ' SMS: ' + sms.Body

for chat in skype.Chats:
print chat.Type + ' Chat'
for message in chat.Messages:
print 'Message: ' + message.Body


Using the code above, I can now see that I have received the SMS message:

OUTGOING SMS: short message from python
OUTGOING SMS: Hello from sendSms.py
OUTGOING SMS:
OUTGOING SMS: test with the 1 in it
OUTGOING SMS:
OUTGOING SMS: testing sms from skype
DIALOG Chat
Message: "test another from iPhone [ SMS to Skype message from +15555551234 ]"
Message:
Message:
MULTICHAT Chat


I'm pretty bummed about SkypeIn being landline and having no real option for receiving SMS. I thought about my fundamental problem and I could set up a private twitter account specifically for tracking the expenses that we make while out but I'd really prefer SMS or email. The SMS attraction was that it is just 2 fields which makes sense; email has a subject field which is unnecessary for this task.

1 comment:

MrTidy OTR said...

Thank you :)