-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
46 lines (40 loc) · 1.39 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from flask import Flask, request, render_template
import requests
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__, static_url_path="/static/")
@app.route("/")
def index():
return render_template("index.html", title="Welcome")
@app.route("/sms", methods=["POST"])
def incoming_sms():
sender = request.form.get("From")
message = request.form.get("Body")
media_url = request.form.get("MediaUrl0")
print(f"{sender} sent {message}")
if media_url:
r = requests.get(media_url)
content_type = r.headers["Content-Type"]
username = sender
if content_type == "image/jpeg":
filename = f"static/latest.jpg"
elif content_type == "image/png":
filename = f"static/latest.png"
elif content_type == "image/gif":
filename = f"static/latest.gif"
else:
filename = None
if filename:
if not os.path.exists(f"static"):
os.mkdir(f"static")
with open(filename, "wb") as f:
f.write(r.content)
print("Thank you! Your image was received.")
else:
print("The file that you submitted is not a supported image type.")
else:
print("Please send an image!")
resp = MessagingResponse()
return str(resp)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)