Я создаю чат-бота на основе SMS с использованием Python, Twilio, Flask, IBM Watson и Google App Engine.Он отлично работает в течение нескольких минут, но затем неизбежно отправляет внутреннюю ошибку ответа.
Я попытался отредактировать файл yaml, поэтому срок его действия по умолчанию составляет 1 день.Это ничего не делает.
Редактировать: В настоящее время используется автоматическое масштабирование, но я пытался перейти с автоматического на базовое масштабирование и изменить min_instances на 1.
Вот журнал:
2019-07-06 08:57:09 default[20190706t180659]
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/env/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/env/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/env/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/env/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/srv/main.py", line 81, in incoming_sms
r = Watson(b)
File "/srv/main.py", line 42, in Watson
input = message_input
File "/srv/ibm_watson/assistant_v2.py", line 244, in message
accept_json=True)
File "/srv/ibm_cloud_sdk_core/base_service.py", line 358, in request
raise ApiException(response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: NotFound: session id 2900da9f-dd77-480a-a939-1a5b060b3f82 for agent instance 6656a86c-ad0e-4463-8344-5f7fdcb4a6fe, Code: 404 , X-global-transaction-id: 5a3699f1cd4e3409e9f89f4fcd87735f
Код для ввода данных в Twilio, передачи их через Watson и ответа.
from flask import Flask, request, make_response
from twilio.twiml.messaging_response import MessagingResponse
import os
from twilio import twiml
import ibm_watson
# Set up Assistant service.
service = ibm_watson.AssistantV2(
iam_apikey = 'xxx', # replace with API key
version = '2019-02-28',
url = 'https://gateway-syd.watsonplatform.net/assistant/api')
assistant_id = 'xxx' #Under Assistant Settings
# Create session.
session_id = service.create_session(
assistant_id = assistant_id
).get_result()['session_id']
def Watson(b):
# Initialize with empty value to start the conversation.
message_input = {
'message_type:': 'text',
'text': str(b)
}
# Main input/output loop
while message_input['text'] != 'quitt':
# Send message to assistant.
response = service.message(
assistant_id,
session_id,
input = message_input
).get_result()
# If an intent was detected, print it to the console.
if response['output']['intents']:
print('Detected intent: #' + response['output']['intents'][0]['intent'])
# Print the output from dialog, if any. Supports only a single
# text response.
if response['output']['generic']:
if response['output']['generic'][0]['response_type'] == 'text':
return(response['output']['generic'][0]['text'])
# Prompt for next round of input.
message_input = {
'text': str(b)
}
# We're done, so we delete the session.
service.delete_session(
assistant_id = assistant_id,
session_id = session_id
)
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
#Get the message the user sent our Twilio number
body = request.values.get('Body', None)
b = str(body)
b = b.lower()
resp = MessagingResponse()
r = Watson(b)
resp.message(r)
# response.append(r)
return str(resp)
if __name__ == "__main__":
app.run(debug=True)