def log(event, phone, message, begin=False, end=False, line_len=20): """ Logs the number and message. Optional args begin and end add some separators and spacing.""" phone = fixes.asciified(phone) message = fixes.asciified(message) word_len = len(event) + 2 #for two spaces left = (line_len - word_len) // 2 right = line_len - (left + word_len) if begin: logging.info(' ' + '*'*line_len) logging.info(" {L} {ev} {R} ".format( L=left*'-', ev=event, R=right*'-' )) logging.info(" " + phone) logging.info(" " + message) if end: logging.info('\n\n')
def application(environ, start_response): """ Main WSGI callable function. This function is called when the server receives a request. It expects the request to contain the "text" parameter, and an optional "phone" parameter. Calls get_reply and dispatches a reply SMS to the SMSGateway app server (via send_sms). Returns a Werkzeug Response containing the phone and reply.""" request = Request(environ) http_method = request.method # Our server receives an SMS using a GET Request. # I'd use POST if it was my choice, but the # creator of SMS Gateway chose GET :(. if http_method == 'GET': params = request.args phone = params.get('phone') user_message = params.get('text') # Workaround until we figure out Unicode: phone = fixes.asciified(phone) user_message = fixes.asciified(user_message) log("Received", phone, user_message, begin=True) reply_message = get_reply(user_message, phone) log("Sent", phone, reply_message, end=True) if __name__ != '__main__': send_sms(phone, reply_message) #response = Response('', mimetype='text/html') response = Response(reply_message, mimetype='text/html') return response(environ, start_response)