Skip to content
Browse files

ALPHA release.

  • Loading branch information...
1 parent 7f6fda2 commit e507fc81bac884c0af7d9952b7d41c6d45a3a17c @orangethirty committed Feb 27, 2013
Showing with 65 additions and 71 deletions.
  1. +2 −2 README.md
  2. +1 −10 carriers.json
  3. +6 −4 config.json
  4. +7 −6 test.py
  5. +7 −0 utils.py
  6. +1 −0 web2txt.log
  7. +41 −49 web2txt.py
View
4 README.md
@@ -1,4 +1,4 @@
-**Note: in development. API does work as is, but lacks logging and proper docs. **
+*Docs are in the Wiki.*
web2txt
=========
@@ -12,7 +12,7 @@ service. This allows you to run your own texting API with no extra cost.
This uses the option carriers give to send text messages through email.
You set it up on your server and have it listen at a given port.
-Then you make a POST request to /text with the text data as JSON (example in docs).
+Then you make a POST request to /text with the text data as JSON (example in docs (Wiki)).
The API takes care of the rest. If the message is sent succesfully you receieve a code 200 from the API.
View
11 carriers.json
@@ -1,10 +1 @@
-{"tmobile" : "@tmomail.net",
- "verizon" : "@vtext.com",
- "att" : "@txt.att.net",
- "att_enterprise_paging" : "@page.att.net",
- "claro_puerto_rico" : "@vtexto.com",
- "google_voice" : "@txt.voice.google.com",
- "sprint" : "@messaging.sprintpcs.com",
- "tracfone" : "@mmst5.tracfone.com",
- "virgin_mobile" : "@vmobl.com"
-}
+{"" : ""}
View
10 config.json
@@ -1,7 +1,9 @@
-{"username" : "your_username",
- "password" : "your_password",
- "from" : "FROM WHO?",
- "smtp_address" : "smtp.FOO.com:PORT"}
+{"username" : "",
+ "password" : "",
+ "from" : "",
+ "smtp_address" : "",
+ "api_key" : ""
+}
View
13 test.py
@@ -3,12 +3,13 @@
import json
import requests
-url = '' #url where your API lives.
-carrier = '' #note: check list of supported carriers in carriers.json
-number = '' #number with area code
-message = '' #note that SMS has a size limit.
-payload = {'carrier' : carrier, 'number' : number, 'msg' : message}
+url = '' #url where your API lives.
+carrier = '' #note: check list of supported carriers in carriers.json
+number = '' #number with area code
+message = '' #note that SMS has a size limit.
+api_key = '' #security key
+payload = {'carrier' : carrier, 'number' : number, 'msg' : message, 'api_key' : api_key}
headers = {'content-type' : 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
-print r
+print r.text
View
7 utils.py
@@ -1,4 +1,5 @@
import json
+from time import strftime
def load_config():
@@ -17,3 +18,9 @@ def load_carriers():
return carriers
+def logging(log):
+ """simple logging function. logging string as parameter."""
+ with open('web2txt.log', 'a') as f:
+ debug_log = "web2txt debug message: {0} * {1} \n".format(strftime("%Y-%m-%d %H:%M:%S"), log)
+ f.write(debug_log)
+ f.close
@jackric
jackric added a note Mar 1, 2013

You don't need the f.close() here - when the with block is finished, the file is closed automatically. It's one of the cool things about context managers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
1 web2txt.log
@@ -0,0 +1 @@
+
View
90 web2txt.py
@@ -1,25 +1,17 @@
-"""
-Note: code is tested and working, but unfinished.
+#web2txt
+#License: GNU GPL
+#copyright 2013 orangethirty@gmail.com
-TODO:
-Clean it up. Update readme. Write docs. :)
-Add error reporting to syslog.
-
-TEST logging code!
-
-
-"""
-
-import logging
-import logging.handlers
import smtplib
from flask import Flask, request, jsonify, Response, json
-from utils import load_config, load_carriers
+from utils import load_config, load_carriers, logging
#flask setup
app = Flask(__name__)
-PORT = 5000 #port number you will be listening at. change to fit your needs. note: not used currently
+
+#port number you will be listening at. change to fit your needs. note: not used currently
+PORT = 5000
@app.route("/")
@@ -31,68 +23,68 @@ def index():
@app.route("/text", methods=['POST'])
def send_text():
"""Sends the txt message from data passed through POST."""
-
- #logging
- my_logger = logging.getLogger('web2txt logger')
- my_logger.setLevel(logging.DEBUG)
- handler = logging.handlers.SysLogHandler(address = '/var/log')
- my_logger.addHandler(handler)
-
+
if request.method == 'POST':
if request.headers['Content-Type'] == 'application/json':
#converts json to python dict
data = request.json
#get list of carriers from carriers.json
+ config = load_config()
carriers = load_carriers()
- if data['carrier'] in carriers:
- #prepare the message
- number = data['number']
- msg = data['msg']
- to = "{0}{1}".format(number, carrier)
- CONFIG = load_config()
- FROM = CONFIG['from']
- #sends the actual message
- mail = smtplib.SMTP(CONFIG['smtp_address'])
- mail.starttls()
- mail.login(CONFIG['username'], CONFIG['password'])
- mail.sendmail(FROM, to, msg)
- mail.quit()
- #prepare the json response to your app.
- log = "Message: '{0}' was sent succesfuly sent to '{1}'.".format(msg, to)
- resp = {"response" : log}
- response = Response(json.dumps(resp), status=200, mimetype='application/json')
- return response
+ #authenticate request
+ if data['api_key'] == config['api_key']:
+
+ if data['carrier'] in carriers:
+ #prepare the message
+ carrier_choice = data['carrier']
+ carrier = carriers[carrier_choice]
+ number = data['number']
+ msg = data['msg']
+ to = "{0}{1}".format(number, carrier)
+ sender = config['from']
+ #sends the actual message
+ mail = smtplib.SMTP(config['smtp_address'])
+ mail.starttls()
+ mail.login(config['username'], config['password'])
+ mail.sendmail(sender, to, msg)
+ mail.quit()
+ #prepare the json response.
+ log = "Message: '{0}' was sent succesfuly sent to '{1}'.".format(msg, to)
+ logging(log)
+ resp = {"response" : log}
+ response = Response(json.dumps(resp), status=200, mimetype='application/json')
+ return response
#if the carrier is not supported or found in the carriers list.
else:
log = "Carrier not supported."
- my_logger.debug(log)
+ #log to web2txt.log file
+ logging(log)
resp = {"response" : log}
response = Response(json.dumps(resp), status=404, mimetype='application/json')
return response
#if the content type is not json
else:
- log = "Wrong request content-type. API only support JSON."
- my_logger.debug(log)
+ log = "Wrong request content-type. API only support JSON"
+ #log to web2txt.log file
+ logging(log)
resp = {"response" : log}
response = Response(json.dumps(resp), status=415, mimetype='application/json')
return response
#if the request is not a POST. note that flask handles this but included anyways.
else:
log = "Method Not Allowed. The method GET is not allowed for the requested URL."
- my_logger.debug(log)
+ #log to web2txt.log file
+ logging(log)
resp = {"response" : log}
response = Response(json.dumps(resp), status=405, mimetype='application/json')
return response
if __name__ == "__main__":
- #if you need to debug, replace the line below with: app.run(debug=True)
- app.run()
-
-
-
+ #if you need to debug, replace the line below with: app.run(port=PORT, debug=True)
+ app.run(port=PORT)

0 comments on commit e507fc8

Please sign in to comment.
Something went wrong with that request. Please try again.