Wednesday, 28 September 2011

Getting Ready For John Hegley

Went to the Manchester Museum today with the boys. We were taking a look at the Living Worlds exhibit in preparation for them going to see John Hegley next week. I wish I could go along.

Friday, 23 September 2011

Manchester Jelly

Today I spent the day working from Manchester Jelly. It was quite an interesting day. I definitely felt the outsider working for "the man". I wasn't quite sure how much we were suppose to be chatting about things. The conversation came and went. Sometimes there were conversations about WordPress, VAT returns, charge out rates. Sometimes there were Trappist like silences. I think that if there becomes a core of people who turn up reasonably regularly then some common shared background will emerge and conversation is likely to flow more smoothly. Oh, and I did manage to have a reasonably productive day!

Wednesday, 21 September 2011

Make Vibrobot

#2 son and I have just finshing making the Make Magazine Vibrobot. We used a metal shoe polish tin as we couldn't find a metal sweets tin. The shoe polish tin was circular so the cable ties wouldn't hold the motor in place. #2 son solved the problem "use duct tape". I have taught him well!

Tuesday, 20 September 2011

foursquare

I've been playing with foursquare recently. It is quite good fun, but I have no friends. If you are on foursquare and you know me, you can add me as a friend.

Monday, 19 September 2011

Tooth Fairy Arrives

Son #2's first two teeth both fell out on the same day yesterday. The tooth fairy came over night. Given #1 son's common reading of Wikipedia as the Source of Truth I hope he stays off the topic.

Sunday, 18 September 2011

Minecraft Mod Manager

Son #1 has got hugely into Minecraft recently. As a result son #2 is now interested in it also, but that's a different story. I've been roped in as the tech support to install all the "mods". Installing mods involves unpacking and repacking the contents of the minecraft.jar file and moving resources about in your minecraft folder. This is too tricky for a nine-year-old to do reliably at the moment. I've started writing a python script to automate the process. I had a quick goolge to see if I could find someone solving this problem already but didn't find anything.

Saturday, 30 April 2011

Send and Receive SMS with Python

I was thinking about writing a text-based game similar to the classic Fighting Fantasy books, but using SMS as the delivery mechanism. To do that I would need a way to send and receive SMS text messages to and from a server. As a challenge to see how that would work I wanted to write a prototype that would look like this:

CPU>>User: "Hello! What is your name?"
User>>CPU: "Fred"
CPU>>User: "Nice to meet you Fred"

The program itself is obviously easy. The problem is how to do the input and output over SMS. On to Google to see if I could find anything. Second link down on my first search I found the Active State code recipe 'Send and receive SMS messages using TextMagic'. It had a nice looking python package available. Reading the TextMagic website it seems that the only way to get started is to buy 200 SMS credits for £20. Hmm a little pricey as a barrier to entry.
I remember using Clickatell for this sort of thing ages ago. I took a look back at their site. The reality-check came in pretty quickly as Clickatell was going to cost minimum of €100 to set up and then €25 per month to run. So, back to TextMagic. And then I found out how to sign up for a free trial account!
With the free trial set up the next job was to set up the python environment. I use virtualenv to keep my python installation clean. To get started therefore created a new virtualenv.

$ virtualenv sms
$ source sms/bin/activate

I then installed the PyTextMagicSMS package.
$ sms/bin/easy_install PyTextMagicSMS
The first job was to try the sample sending program:

import textmagic.client
client = textmagic.client.TextMagicClient('your_username', 'your_api_password')
result = client.send("Hello, World!", "1234567890")

...which worked a treat. Obviously change the method parameters to match your settings.
So onwards to reading the Text Magic documentation to find out how to receive a reply. There are two ways of receiving a reply. Either you can poll your in-box to see what messages you have received, or you can set a callback URL to be notified of the response. The callback URL is clearly the way to go for anything of any complexity, but just to get started we will poll the in-box.

import textmagic.client
client = textmagic.client.TextMagicClient('name', 'pw')

received_messages = client.receive(0)
messages_info = received_messages['messages']
print('%d messages in in-box' % len(messages_info))
if len(messages_info) > 0:
first_message = messages_info[0]
print("Message from: %s" % first_message['from'])
print(first_message['text'])
client.delete_reply(first_message['message_id'])


Okay, so we're getting close to a solution. I'll have one program that sends the hello part of the conversation, a second program that checks the in-box for replies and then sends the final message. Here's the sending one...

import sys
import textmagic.client


client = textmagic.client.TextMagicClient('username', 'password')
no = sys.argv[1]

result = client.send("Hello! What is your name?", no)


...and here's the receiving one...

import textmagic.client
client = textmagic.client.TextMagicClient('username', 'password')

received_messages = client.receive(0)
messages_info = received_messages['messages']
print('%d messages in in-box' % len(messages_info))
if len(messages_info) > 0:
first_message = messages_info[0]
from_no = first_message['from']
name = first_message['text']
print("Message from: %s" % from_no)
print(name)

client.send("Nice to meet you %s!" % name, from_no)
print("I have sent a reply")
client.delete_reply(first_message['message_id'])


All this needs is a main loop round it to keep polling for input and we're done.