繁体   English   中英

带twilio错误的烧瓶短信

[英]Flask SMS with twilio error

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.

您正在发布到错误的端点。 您的表单应如下所示:

<!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">

这是在flask中使用URL的更可持续的方法,如果您需要更改值,它将减少您的开销。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM