Python 我犯了这个错误';TypeError:只能将str(而不是“NoneType”)连接到str';

Python 我犯了这个错误';TypeError:只能将str(而不是“NoneType”)连接到str';,python,python-3.x,string,python-requests,twilio,Python,Python 3.x,String,Python Requests,Twilio,我试图实现的是,用户将首先向bot提问。假设用户想要找到最近的货币兑换机,他/她会键入“我需要找到货币兑换机。然后机器人会回答“请提供位置”。一旦用户提供了坐标,机器人就会回复附近所有的货币兑换商 from flask import Flask, request import requests from twilio.twiml.messaging_response import MessagingResponse app = Flask(__name__) @app.route('/s

我试图实现的是,用户将首先向bot提问。假设用户想要找到最近的货币兑换机,他/她会键入“我需要找到货币兑换机。然后机器人会回答“请提供位置”。一旦用户提供了坐标,机器人就会回复附近所有的货币兑换商

from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse


app = Flask(__name__)


@app.route('/sms', methods=['POST'])
def bot():

    incoming_msg = request.values.get('Body', '').lower()
    resp = MessagingResponse()
    msg = resp.message()

    if 'moneychanger' in incoming_msg:
        search1 = 'Please provide the location please'
        msg.body(search1)

        message_latitude = request.values.get('Latitude', None)
        message_longitude = request.values.get('Longitude', None)

        responded = True

        if message_latitude == None:
            location = '%20' + message_latitude + '%2C' + message_longitude 
            responded = False


            url = f'https://tih-api.stb.gov.sg/money-changer/v1?location={location}&radius=2000'

            r = requests.get(url)
            if r.status_code == 200:
                data = r.json()
                search = data['data'][0]['name']
            else:
                search = 'I could not retrieve a quote at this time, sorry.'
            msg.body(search)
            responded = True

    return str(resp)

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

这里是Twilio开发者福音传道者

我相信您正在使用,基于您使用的位置参数

这里的问题是,您试图在同一webhook请求中回复并接收更多信息。但是,文本消息(其中包含“moneychanger”)将以不同的请求发送到带有位置消息的请求。因此,您需要在应用程序中存储一些状态,表明您的用户当前正在寻找货币兑换商

下面是一个示例,用于存储传入消息,然后询问位置,如果有消息,请将其与消息放在一起并作出响应:

从烧瓶导入烧瓶,请求,会话
导入请求
从twilio.twiml.messaging\u响应导入MessagingResponse
app=烧瓶(名称)
#将密钥设置为一些随机字节。请保守秘密!
#不要使用这些字节,因为它们在文档中。
app.secret_key=b“u5”y2L“F4Q8z\n\xec]/”
@app.route('/sms',methods=['POST'])
def bot():
传入的\u msg=request.values.get('Body','').lower()
resp=消息响应()
msg=响应消息()
message_latitude=request.values.get('latitude',None)
message_longitude=request.values.get('longitude',None)
如果传入消息中有“moneychanger”:
#我们在找一个货币兑换商,问一下位置
响应='请提供位置'
会话['message']=传入消息
elif消息经纬度和消息经度和会话中的“消息”和会话中的“货币兑换商”[“消息”]
#我们已经知道了位置,之前的消息要求
#货币兑换商。
位置=“%20”+消息纬度+“%2C”+消息经度
url=f'https://tih-api.stb.gov.sg/money-changer/v1?location={location}&radius=2000'
r=请求。获取(url)
如果r.status_code==200:
data=r.json()
响应=数据['data'][0]['name']
其他:
response='对不起,我现在无法检索报价单。'
#我们已经处理完原始消息,现在可以取消设置。
会话['message']=无
其他:
#在这种情况下,要么您有一条不包含
#“moneychanger”或您在请求中有纬度和经度,但
#没有会话['message']。您可能想在此处执行其他操作,但是
#我还不知道是什么。
回答='我不确定你在找什么。'
消息正文(应答)
返回str(resp)
如果名称=“\uuuuu main\uuuuuuuu”:
app.run(debug=True)
您可能还希望对此进行扩展,以便在收到请求(“moneychanger”)之前收到位置为的消息时,您可以在会话中存储位置,然后询问用户正在查找什么


让我知道这是否有帮助。

因为
message\u latitude
message\u latitude
或两者都是
None
。请提供有关您如何提出请求的更多详细信息。@racraman,正如您所看到的,它是写在烧瓶中的,显然是写在烧瓶中的python@JohmEayne正如你们所看到的,它写在烧瓶里,所以显而易见sly它是用python编写的,那么为什么它被标记为Java和JavaScript?@Narishma这是所有的细节错误在哪一行?