def fetch_sms(self):
        gateway = AfricasTalkingGateway(self.APP_USERNAME, self.API_KEY)
        try:
            # Our gateway will return 10 messages at a time back to you, starting with
            # what you currently believe is the lastReceivedId. Specify 0 for the first
            # time you access the gateway, and the ID of the last message we sent you
            # on subsequent results
            lastReceivedId = 0

            while True:
                messages = gateway.fetchMessages(lastReceivedId)
                if len(messages) == 0:
                    print 'No sms messages in your inbox.'
                    break
                for message in messages:
                    print 'from = %s; to = %s; date = %s; text = %s; linkId = %s;' % (
                        message['from'], message['to'], message['date'],
                        message['text'], message['linKId'])
                    lastReceivedId = message['id']

        except AfricasTalkingGatewayException as e:
            print 'Encountered an error while fetching messages: %s' % str(e)
apikey   = 'MyApikey'

# Create a new instance of our awesome gateway class
gateway = AfricasTalkingGateway(username, apikey)

# Any gateway errors will be captured by our custom Exception class below, 
# so wrap the call in a try-catch block
try:
	# Our gateway will return 10 messages at a time back to you, starting with
	# what you currently believe is the lastReceivedId. Specify 0 for the first
	# time you access the gateway, and the ID of the last message we sent you
	# on subsequent results
	lastReceivedId = 0;
    
	while True:
		messages = gateway.fetchMessages(lastReceivedId)
        
		for message in messages:
			print 'from=%s;to=%s;date=%s;text=%s;linkId=%s;' % (message['from'],
			                                                    message['to'],
			                                                    message['date'],
			                                                    message['text'],
			                                                    message['linKId']
			                                                   )
			lastReceivedId = message['id']
	if len(messages) == 0:
		break

            
except AfricasTalkingGatewayException, e:
	print 'Encountered an error while fetching messages: %s' % str(e)