from flask import *
from twilio import twiml
from twilio.rest import TwilioRestClient
from flask import render_template
import os
#Pull in configuration from system environment variables
TWILIO_ACCOUNT_SID = os.environ.get('Axxxxxx')
TWILIO_AUTH_TOKEN = os.environ.get('Sxxxxxxxxx')
TWILIO_NUMBER = os.environ.get('xxxxxxx')
# create an authenticated client that can make requests to Twilio for your
# account.
#client = TwilioRestClient(account='Axxxxx', token'sxxxxxxxx')
#create a flask web app
app = Flask(__name__)
client = TwilioRestClient(account='Axxxxx', token='Sxxxxx')
@app.route('/')
def homepage():
return render_template('index.html')
#Handling a post request to send text messages.
@app.route('/message', methods=['POST', 'GET'])
def message():
# Send a text message to the number provided
if request.method == 'POST':
message = client.sms.messages.create(to=request.form['Phone_number'],
from_=TWILIO_NUMBER,
body=request.form['body'])
return render_template('message.html')
if __name__ == '__main__':
# Note that in production, you would want to disable debugging
app.run(debug=True)
我正在使用烧瓶。当我输入数字和短信时,它给了我这个错误
Method Not Allowed
The method is not allowed for the requested URL.
答案 0 :(得分:1)
您发布到错误的端点。您的表单应如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Send an SMS</title>
</head>
<body>
<form action="/message" method="POST">
Cell phone number: <input name="phone_number" type="text" />
Text in here: <input name="body" type="text" />
<button type="submit">Send Text</button>
</form>
</script>
</body>
</html>
(上图,action
已从/
更改为/message
。)
注意:如果这是一个贯穿flask.render_template
的模板,则应更改
<form action="/message" method="POST">
到
<form action="{{ url_for('message') }}" method="POST">
这是一种更可持续的方法,可以在烧瓶中使用网址,如果您需要更改价值,它会减少您的开销。