我正在尝试使用 python 中的 Flask-Ask 扩展为 Alexa 技能创建 Web 服务。该网络服务将允许用户查询本地 MySQL 数据库以查找停车信息并进行停车预订。
我一直在关注https://medium.com/spawn-ai/chat-bots-and-how-to-build-one-on-alexa-35772e429631上的指南
我正在使用以下 python 扩展:flask、flask_ask、MySQL。
这是我的网络服务的代码:
'''This is a Web Service for the Logan Parking skill'''
author__ = '*****'
import logging
from flaskext.mysql import MySQL
from flask import Flask, render_template
from flask_ask import Ask, statement, question
mysql = MySQL()
app = Flask(__name__)
app.config.from_object(__name__)
app.config['MYSQL_DATABASE_USER'] = '*****'
app.config['MYSQL_DATABASE_PASSWORD'] = '*****'
app.config['MYSQL_DATABASE_DB'] = '*****'
app.config['MYSQL_DATABASE_HOST'] = '*****'
mysql.init_app(app)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
#on launch of the parking skill, alexa tells the user "Welcome to the prototype Parking skill!"
@ask.launch
def launch():
welcome_message = 'Welcome to the prototype Parking Skill ! What can I do for you ?'
return question(welcome_message) \
.standard_card(title='Parking',
text='Welcome to the prototype Parking Skill ! ',
large_image_url='IMG')
#logic for the AvailableParking intent, which at this point tells the user which garages are closed/opened
@ask.intent('AvailableParking')
def available(occupancy):
#converts the inputted occupancy variable into all caps, to match the DB schema
occupancy = occupancy.upper()
#creates cursor variable that connects to DB
cursor = mysql.connect().cursor()
#connects to db and executes SQL query that displays all garages where the OCCUPANCY field matches the users slot input (OPEN OR CLOSED)
cursor.execute("SELECT GARAGE_NAME FROM GARAGES WHERE OCCUPANCY = %s", (occupancy,))
#stores all result rows from the query in the data variable
data = cursor.fetchall()
return statement(data)
#opens a server on localhost5000
if __name__ == '__main__':
app.run(debug=True, port=5000)
这是错误消息:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_ask/core.py", line 767, in _flask_view_func
result = self._map_intent_to_view_func(self.request.intent)()
File "/home/ngrok/ngrok/dbcon9", line 46, in available
return statement(data)
File "/usr/local/lib/python2.7/dist-packages/flask_ask/models.py", line 188, in __init__
super(statement, self).__init__(speech)
File "/usr/local/lib/python2.7/dist-packages/flask_ask/models.py", line 51, in __init__
'outputSpeech': _output_speech(speech)
File "/usr/local/lib/python2.7/dist-packages/flask_ask/models.py", line 402, in _output_speech
xmldoc = ElementTree.fromstring(speech)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
parser.feed(text)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1651, in feed
self._parser.Parse(data, 0)
TypeError: Parse() argument 1 must be string or read-only buffer, not tuple
{"context": {"System": {"apiAccessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjEifQ.eyJhdWQiOiJodHRwczovL2FwaS5hbWF6b25hbGV4YS5jb20iLCJpc3MiOiJBbGV4YVNraWxsS2l0Iiwic3ViIjoiYW16bjEuYXNrLnNraWxsLmY2NDViYTgxLTZmM2QtNDZlNi04MDM1LTI1MTBlYTg5ODNkYiIsImV4cCI6MTUzMDU1NzAyNSwiaWF0IjoxNTMwNTUzNDI1LCJuYmYiOjE1MzA1NTM0MjUsInByaXZhdGVDbGFpbXMiOnsiY29uc2VudFRva2VuIjpudWxsLCJkZXZpY2VJZCI6ImFtem4xLmFzay5kZXZpY2UuQUhFRzdPQ0RLN01TNkdFWkhYTUlTUlFXRkNNTktKVUxHTk1GTkdSVFlWSFk0WFZMSkVWUzRZTFFGSjVFSDVRR0YyREVYQTVVVjI0UzJHNFJNU0tYWFpHVjZFRDY0RktJWkpHVVQyWURVRlZZTzVLVUZLSENDUzVPWUdPTDI3NUU1QjRGRU1XTk5VRVAzUkxPSENFNzRUVTdLSDZBIiwidXNlcklkIjoiYW16bjEuYXNrLmFjY291bnQuQUg0T1FZSVBSQ1NRVEZaR1pIWkVRSFpCU1RISDI1VUE2MjYyNFY1WTdDWTdCSzZDWU9RWjdJN1BMV0s0Tzc0RFpVSzRBMkw0MlpQUjRQV1lFRlJIVzY2MlRPVlVEM1BCT1c0UkxRSjNFSFpVWllTTzM2VkM0NkMzRVdHT1ZPRFlVSVNWNzNPRVRHUkJEUkFTRjc0NEg3VFcyR0pYM01FN0kyWVNGQkFTNjNOUUFGNzNCVUJHUlRSSU5ZQ0tIM0dPWjZZVjNVS0tRM0xZMlFJIn19.J6mfOthVmALtgGPeYJLKSyq0EtrohslTxIWgB4g-jMTD5WHOlI869QWz7VRORHrGiLYJSM6kh980z-kTQ_LPLmPeLiQZrFKnMgMAnhKbJooa0xlsh_R4ILUb4Rgur-PHN1IX5SWBP1kfy0oEU95YtYcXJ0KlFD1O8zl9hZGxkL61-_1CuU9l8HGBVhkrO544PdgirSqmpfc7o1NMzEgkAfKmQRC-MpSe02KaWlNTj_ryzwQLYCLPFs39iOYWJzot56Fz3YrNhftIbCnG5c36I86nd0j8T1N0rTKyELg0mgs28wQmJSdQiPOLbhzFFcBtT6-7le7iUhG4bI1UBfHI0A", "apiEndpoint": "https://api.amazonalexa.com", "application": {"applicationId": "amzn1.ask.skill.f645ba81-6f3d-46e6-8035-2510ea8983db"}, "device": {"deviceId": "amzn1.ask.device.AHEG7OCDK7MS6GEZHXMISRQWFCMNKJULGNMFNGRTYVHY4XVLJEVS4YLQFJ5EH5QGF2DEXA5UV24S2G4RMSKXXZGV6ED64FKIZJGUT2YDUFVYO5KUFKHCCS5OYGOL275E5B4FEMWNNUEP3RLOHCE74TU7KH6A", "supportedInterfaces": {}}, "user": {"userId": "amzn1.ask.account.AH4OQYIPRCSQTFZGZHZEQHZBSTHH25UA62624V5Y7CY7BK6CYOQZ7I7PLWK4O74DZUK4A2L42ZPR4PWYEFRHW662TOVUD3PBOW4RLQJ3EHZUZYSO36VC46C3EWGOVODYUISV73OETGRBDRASF744H7TW2GJX3ME7I2YSFBAS63NQAF73BUBGRTRINYCKH3GOZ6YV3UKKQ3LY2QI"}}}, "request": {"error": {"message": "An exception occurred while dispatching the request to the skill.", "type": "INVALID_RESPONSE"}, "locale": "en-US", "reason": "ERROR", "requestId": "amzn1.echo-api.request.864a9333-cee5-409a-abbe-9bda1cbed895", "timestamp": "2018-07-02T17:43:45Z", "type": "SessionEndedRequest"}, "session": {"application": {"applicationId": "amzn1.ask.skill.f645ba81-6f3d-46e6-8035-2510ea8983db"}, "new": false, "sessionId": "amzn1.echo-api.session.462ff667-b8cf-4ede-b838-4834c767e66e", "user": {"userId": "amzn1.ask.account.AH4OQYIPRCSQTFZGZHZEQHZBSTHH25UA62624V5Y7CY7BK6CYOQZ7I7PLWK4O74DZUK4A2L42ZPR4PWYEFRHW662TOVUD3PBOW4RLQJ3EHZUZYSO36VC46C3EWGOVODYUISV73OETGRBDRASF744H7TW2GJX3ME7I2YSFBAS63NQAF73BUBGRTRINYCKH3GOZ6YV3UKKQ3LY2QI"}}, "version": "1.0"}
{}
127.0.0.1 - - [02/Jul/2018 17:43:45] "POST / HTTP/1.1" 200 -
特别是,这两行用于将查询结果返回给 Alexa。
#stores all result rows from the query in the data variable
data = cursor.fetchall()
return statement(data)
我一直在查看其他问题,发现 cursor.fetchall() 将查询结果作为元组返回。
有谁知道我将如何将查询结果转换为字符串,以便我可以将查询结果返回给 Alexa?