尝试/除了检查字符串并在Python和Twilio中执行该功能

时间:2017-12-05 01:39:27

标签: python python-3.x twilio

我正在尝试解析正文消息,然后返回用户请求的值。我无法弄清楚如何存储5个字符的邮政编码,所以我决定尝试使用该功能。基本上,我希望用户发送文本,例如" SenEmail"然后被提示输入他们的邮政编码并返回数据(我目前只让它返回API调用URL以简化)。但是,它不能正常工作,我一直收到错误,说明我的缩进已经关闭或者语法无效=' SenWeb':"当它工作时,它返回所有3个API调用链接,而不仅仅是与之关联的链接。所以SenPhone将返回(发送所有信息的3个文本而不仅仅是1个),SenPhone,SenWeb和SenEmail,而不仅仅是SenPhone。你可能会说,我对python很新。谢谢您的考虑。

import urllib.request as urlr
import urllib.parse as url
import urllib.error as urle
import json
import os
from flask import Flask, request, redirect
from twilio import twiml
from twilio.twiml.messaging_response import MessagingResponse


app = Flask(__name__)
@app.route('/sms', methods=['GET', 'POST'])

def sms_reply():
    r = MessagingResponse()
    body = request.values.get('Body', None)
    if body == 'SenPhone':
        r.message("Welcome to PollText! Reply with Zip for Senators Phone Num's.")
    try: 
        if len(body) == 5:
            url = 'https://www.googleapis.com/civicinfo/v2/representatives?address='
            z_c = str(body)
            key = '&key=xxxxxxxxxxxxxxxxxxx'
            serviceurl = url+z_c+key
            address = serviceurl
            r.message(str(serviceurl + "Phone"))
        else:
            r.message("SenPhone, SenWeb, SenEmail")


    elif body == 'SenWeb':
        r.message("Welcome to PollText! Reply with Zip for Senators Website.")
        try:
            if len(body) == 5:
                url = 'https://www.googleapis.com/civicinfo/v2/representatives?address='
                z_c = str(body)
                key = '&key=xxxxxxxxxxxxxxxxxxx'
                serviceurl = url+z_c+key
                address = serviceurl
                r.message(str(serviceurl + "Website"))
            else:
                r.message("SenPhone, SenWeb, SenEmail")


    elif body == "SenEmail":
        r.message("Welcome to PollText! Reply with Zip for Senators Email's.")
        try:
            if len(body) == 5:
                url = 'https://www.googleapis.com/civicinfo/v2/representatives?address='
                z_c = str(body)
                key = '&key=xxxxxxxxxxxxxxxxxxx'
                serviceurl = url+z_c+key
                address = serviceurl
                r.message(str(serviceurl + "Email"))

        except:
            r.message('Sorry, that was not a valid input message.')

    return str(r)

if __name__ == "__main__":
    app.run(debug=True)

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

我将您的代码弹出到Python validator,它还告诉我elif错误。

然而,我稍微查了一下文件,发现在你的if下面,它看起来像这样:

if body == 'SenPhone':
    r.message("Welcome to PollText! Reply with Zip for Senators Phone Num's.")
try: 
    if len(body) == 5:

所以,问题是try缩进并结束了条件。我想如果你缩进,那么你的语法错误就会消失,你可以继续从那里调试其余的。