From dfab08b877f63eadb24346c780c711c639ef2c6d Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Thu, 2 Apr 2020 22:14:13 -0500 Subject: [PATCH] Webhook to listen and download MMS content to files --- .gitignore | 4 ++++ mms-download.py | 21 +++++++++++++++++++++ requirements.txt | 15 +++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 .gitignore create mode 100644 mms-download.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..728cf4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +lib/ +lib64 +pyvenv.cfg diff --git a/mms-download.py b/mms-download.py new file mode 100644 index 0000000..352c861 --- /dev/null +++ b/mms-download.py @@ -0,0 +1,21 @@ +import requests +from flask import Flask, request +from twilio.twiml.messaging_response import MessagingResponse + +app = Flask(__name__) + +@app.route("/mms", methods=['POST']) +def sms_reply(): + """Respond to incoming multimedia messages with a confirmed download text.""" + if request.form['NumMedia'] is not None: + urls = [request.form['MediaUrl' + str(idx)] for idx in range(int(request.form['NumMedia']))] + photos = {url.split("/")[-1]: requests.get(url) for url in urls} + for identifier, photo in photos.items(): + with open(identifier + '.jpg', 'wb') as f: + f.write(photo.content) + resp = MessagingResponse() + resp.message("Upload received!") + return str(resp) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=9092, debug=True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..827e7e1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +certifi==2019.11.28 +chardet==3.0.4 +Click==7.0 +Flask==1.1.1 +idna==2.8 +itsdangerous==1.1.0 +Jinja2==2.11.0 +MarkupSafe==1.1.1 +PyJWT==1.7.1 +pytz==2019.3 +requests==2.22.0 +six==1.14.0 +twilio==6.35.3 +urllib3==1.25.8 +Werkzeug==0.16.1 -- 2.20.1