├── .gitignore ├── Procfile ├── requirements.txt ├── app.json ├── README.md ├── LICENSE └── question.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.gz 2 | env 3 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn question:app --log-file=- 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asjson==1.2.1 2 | Flask==0.10.1 3 | gunicorn==19.3.0 4 | itsdangerous==0.24 5 | Jinja2==2.8 6 | jsondict==1.2 7 | MarkupSafe==0.23 8 | requests==2.7.0 9 | shortuuid==0.4.2 10 | Werkzeug==0.10. -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "question", 3 | "description": "A Flask web server for quickly asking someone a question.", 4 | "repository": "https://github.com/blha303/question", 5 | "website": "https://github.com/blha303/question", 6 | "keywords": ["http", "rest", "API", "python", "flask"] 7 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Question 2 | ======== 3 | 4 | [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) 5 | 6 | A Flask web server for quickly asking someone a question. Uses [Airgram](http://www.airgramapp.com) for push notification functionality, so users need to make accounts there. 7 | 8 | Currently backend JSON api only. Feel free to implement your own frontend around the API using your own instance. 9 | 10 | The lines starting with "Question from" are sent first, at the same time. The user swipes or selects the relevant option, and a reply is sent to the user indicating their choice. In the below example, I used my own username for both. 11 | 12 | ![screenshot](http://i.imgur.com/MXTWZY8.png) 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Steven Smith (blha303) 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11 | -------------------------------------------------------------------------------- /question.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | #Copyright (c) 2015, Steven Smith (blha303) 3 | #All rights reserved. 4 | # 5 | #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 | # 7 | #1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | # 9 | #2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | # 11 | #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | 13 | #Updates at https://github.com/blha303/question 14 | 15 | from flask import Flask, request, jsonify, make_response 16 | import json, requests, logging, time, jsondict, gzip, shortuuid 17 | 18 | URL = "http://b303.me:7456" 19 | app = Flask(__name__) 20 | 21 | USERS = jsondict.JsonDict("users.json.gz", compress=True, autosave=True) 22 | PENDING = jsondict.JsonDict("pending.json.gz", compress=True, autosave=True) 23 | VERIFY = jsondict.JsonDict("verify.json.gz", compress=True, autosave=True) 24 | 25 | @app.errorhandler(Exception) 26 | def error_handler(e): 27 | try: 28 | code = e.code 29 | except AttributeError: 30 | code = 500 31 | return jsonify(error=code, text=str(e)), code 32 | 33 | def gen_html(header, body=""): 34 | return "

{}

{}

".format(header, body) 35 | 36 | def err_resp(json=True, **kwargs): 37 | response = jsonify(**kwargs) if json else make_response(**kwargs) 38 | response.status_code = kwargs["error"] if "error" in kwargs else 500 39 | return response 40 | 41 | def airgram_check(email, id, msg="Hi! You've been added to Question. Swipe here to verify"): 42 | resp = requests.post("https://api.airgramapp.com/1/send_as_guest", data={'email': email, 'msg': msg, "url": URL + "/verify/" + id}, verify=False).json() 43 | return resp["status"] != "error", resp["error_msg"] if "error_msg" in resp else None 44 | 45 | def airgram_send(**kwargs): 46 | resp = requests.post("https://api.airgramapp.com/1/send_as_guest", data=kwargs, verify=False).json() 47 | return resp["status"] != "error", resp["error_msg"] if "error_msg" in resp else None 48 | 49 | @app.route("/verify/") 50 | def verify_id(id): 51 | global VERIFY 52 | global USERS 53 | if id in VERIFY and VERIFY[id] in USERS: 54 | USERS[VERIFY[id]]["verified"] = True 55 | del VERIFY[id] 56 | return gen_html("VERIFIED", "Feel free to close this page now :)") 57 | else: 58 | return err_resp(json=False, error=404, text=gen_html("NOT VERIFIED", "ID not found. Maybe you've already verified?")) 59 | 60 | @app.route("/yes/") 61 | def yes(id): 62 | global PENDING 63 | global USERS 64 | if id in PENDING: 65 | airgram_send(email=USERS[PENDING[id]["from"]]["email"], 66 | msg="{to} replied Yes to: {text}".format(**PENDING[id])) 67 | return gen_html("REPLY SENT") 68 | else: 69 | return err_resp(json=False, error=404, text=gen_html("ALREADY REPLIED")) 70 | 71 | @app.route("/no/") 72 | def no(id): 73 | global PENDING 74 | global USERS 75 | if id in PENDING: 76 | airgram_send(email=USERS[PENDING[id]["from"]]["email"], 77 | msg="{to} replied No to: {text}".format(**PENDING[id])) 78 | return gen_html("REPLY SENT") 79 | else: 80 | return err_resp(json=False, error=404, text=gen_html("ALREADY REPLIED")) 81 | 82 | 83 | @app.route("/send/") 84 | def send_question(nick): 85 | global USERS 86 | global PENDING 87 | if nick and 'text' in request.args and 'from' in request.args: 88 | if not request.args['from'] in USERS or not USERS.get(request.args['from'], {}).get('verified', False): 89 | reverify(request.args['from']) 90 | return err_resp(error=404, text="Source user not found or not yet verified (verification msg sent if exists)") 91 | if not nick in USERS or not USERS.get(nick, {}).get('verified', False): 92 | return err_resp(error=404, text="Destination user not found or not yet verified") 93 | id = shortuuid.uuid() 94 | PENDING[id] = {'to': nick, 'from': request.args['from'], 'text': request.args['text'], 'ts': time.time()} 95 | first, _f = airgram_send(email=USERS[nick]["email"], msg="Question from {} : No | {}".format(request.args["from"], request.args["text"]), url=URL + "/no/" + id) 96 | second, _s = airgram_send(email=USERS[nick]["email"], msg="Question from {} : Yes | {}".format(request.args["from"], request.args["text"]), url=URL + "/yes/" + id) 97 | if first and second: 98 | return jsonify(status="ok") 99 | elif (first and not second) or (second and not first): 100 | _ = airgram_send(email=USERS[nick]["email"], msg="Oops! Can't send {} message. Please contact {} ASAP.".format("NO" if second else "YES", request.args['from'])) 101 | return err_resp(error=504, text="{} message didn't send, please contact {} directly (they may be contacting you also)".format("First" if second else "Second", nick)) 102 | else: 103 | return err_resp(error=504, text="Messages could not be sent, please notify blha303 at b3@blha303.com.au and contact {} directly at {}".format(nick, USERS[nick]["email"])) 104 | 105 | 106 | @app.route("/add/") 107 | def add_user(nick): 108 | global VERIFY 109 | global USERS 110 | if nick and nick in USERS: 111 | return err_resp(error=409, text="Username in use") 112 | if len(nick) > 20: 113 | return err_resp(error=400, text="Username is too long (>20)") 114 | elif 'email' in request.args: 115 | id = shortuuid.uuid() 116 | VERIFY[id] = nick 117 | exists, err = airgram_check(request.args['email'], id) 118 | if exists: 119 | USERS[nick] = {'email': request.args["email"], 'verified': False, 'reg': time.time()} 120 | return jsonify(status="ok") 121 | else: 122 | return err_resp(error=404, text="{} airgramapp.com".format(err)) 123 | else: 124 | return err_resp(error=400, text="Invalid or missing email address") 125 | 126 | @app.route("/reverify/") 127 | def reverify(nick): 128 | id = shortuuid.uuid() 129 | VERIFY[id] = nick 130 | if airgram_check(USERS[nick]["email"], id, msg="Please reverify your Airgram account for Question. Swipe here to verify"): 131 | return jsonify(status="ok") 132 | else: 133 | return err_resp(error=404, text="An error occured while verifying your Airgram account.") 134 | 135 | @app.route("/user/") 136 | def user_lookup(nick): 137 | if nick and nick in USERS: 138 | return jsonify({nick: USERS[nick]}) 139 | else: 140 | return err_resp(error=404, text="User not found") 141 | 142 | if __name__ == "__main__": 143 | app.run(port=7456, host="0.0.0.0") 144 | --------------------------------------------------------------------------------