【发布时间】:2022-01-15 17:24:05
【问题描述】:
希望一切都好
我正在为 whatsapp 制作一个提醒机器人。在我打了一夜和今天几乎一整天的头之后,我决定寻求帮助。
有一件事,这些文档对于对话流来说真的没用,而且我在 StackOverflow 上找到的大多数文档链接现在都重定向了。
我一直在尝试设置时区,因为如果我不添加时区对话流假定它是我在那里设置的 (GMT+2)。
注意:我打开了 beta API 箭头键。
假设我用 ngrok 将它发布到我的烧瓶应用程序中。
{
"number":"258xx",
"timestamp":1642262883085,
"text":"Ljgl"
}
以前,我是在计算时间差并将其存储到数据库中。然后我会玩很多数学游戏,我设法得到正确的时差时间。唯一的问题是:
如果有人在 10:36 (UTC+1) 向我发送消息,而我的时间是 11:35 (UTC+2),则 dialogflow 会假定时间是第二天的 10:35。所以这意味着,我需要通过消息传递时区,以 10:36 为例,以“欧洲/华沙”为例,所以现在它仍然指的是今天而不是第二天。
所以我现在一直在尝试直接发送到对话流。我在firestore中有用户的时区,我检索了它,例如。 '非洲/马普托'。
所以检查后,您需要添加时区作为查询参数。
GOOGLE_APPLICATION_CREDENTIALS = "something.json"
DIALOGFLOW_PROJECT_ID = 'project-id'
DIALOGFLOW_LANGUAGE_CODE = 'en-GB'
def add_timezone():
resp = request.get_json(force=True)
print(resp)
whatsapp_to = resp['number']
SESSION_ID = f'appname',{whatsapp_to}'
phone_number = "+" + whatsapp_to
text_to_be_analyzed = resp['text']
session_client = dialogflow.SessionsClient(credentials=GOOGLE_APPLICATION_CREDENTIALS) #took me a while to figure this out thanks to another post on StackOverflow -> nothing else worked...
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
query_input = dialogflow.types.QueryInput(text=text_input) #not used in v2beta1 i think
#get timezone here (removed code as you don't have access to db)
timezone = "Europe/Warsaw"
# query_params = dialogflow.types.QueryParameters.time_zone(timezone) #tried never worked
payload = {
"session": session,
'query_input':
{
"text": text_input
},
'query_params':
{
"time_zone": timezone
}
}
response = session_client.detect_intent(payload)
它在响应时卡住了。过了一会儿它会抛出这个错误:
[2022-01-15 19:21:15,194] ERROR in app: Exception on /timezone [POST]
Traceback (most recent call last):
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 67, in error_remapped_callable
return callable_(*args, **kwargs)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\grpc\_channel.py", line 946, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\grpc\_channel.py", line 849, in _end_unary_response_blocking
raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'"
debug_error_string = "{"created":"@1642267275.196000000","description":"Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'","file":"src/core/lib/security/credentials/plugin/plugin_credentials.cc","file_line":92,"grpc_status":14}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\retry.py", line 189, in retry_target
return target()
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 69, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\flask\app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\remindme.py", line 70, in add_timezone
response = session_client.detect_intent(payload)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\cloud\dialogflow_v2beta1\services\sessions\client.py", line 545, in detect_intent
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 145, in __call__
return wrapped_func(*args, **kwargs)
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\retry.py", line 286, in retry_wrapped_func
return retry_target(
File "C:\Users\Lenovo X260\PycharmProjects\whatsapp_bot\venv\lib\site-packages\google\api_core\retry.py", line 204, in retry_target
six.raise_from(
File "<string>", line 3, in raise_from
google.api_core.exceptions.RetryError: Deadline of 220.0s exceeded while calling target function, last exception: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
127.0.0.1 - - [15/Jan/2022 19:21:15] "POST /timezone HTTP/1.1" 500 -
感谢您的帮助:)
编辑 1: 我又试了一次,这次我尝试了新的东西:
timezone = profile['time_zone']
session_client = dialogflow.SessionsClient(credentials=GOOGLE_APPLICATION_CREDENTIALS)
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
query_input = dialogflow.types.QueryInput(text=text_input)
query_params = dialogflow.types.QueryParameters(timeZone=timezone)
response = session_client.detect_intent(session=session, query_input=query_input, query_params=query_params)
但是错误:
ValueError: Unknown field for QueryParameters: timeZone
见这里:https://cloud.google.com/dialogflow/es/docs/reference/rest/v2beta1/QueryParameters
文档有字段 timeZone?那我该怎么办呢?
{
"timeZone": string,
...
编辑2:所以它是python所以我猜测并将timeZone设置为time_zone并且错误消失了。现在它显示:
timezone = profile['time_zone']
session_client = dialogflow.SessionsClient(credentials=GOOGLE_APPLICATION_CREDENTIALS)
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
query_input = dialogflow.types.QueryInput(text=text_input)
query_params = dialogflow.types.QueryParameters(time_zone=timezone)
response = session_client.detect_intent(session=session, query_input=query_input, query_params=query_params)
错误:
TypeError: detect_intent() got an unexpected keyword argument 'query_params'
好消息是 -> 我现在可以使用查询参数并弄清楚如何向它们添加时区。现在的问题在于如何真正实现它的意图。
编辑 4:
凭证没有奏效。所以我用它作为环境变量。
所以现在至少没有 query_params 我不再得到错误:
503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
修复:
GOOGLE_APPLICATION_CREDENTIALS = "secret.json"
DIALOGFLOW_PROJECT_ID = 'project-id'
DIALOGFLOW_LANGUAGE_CODE = 'en-GB'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = GOOGLE_APPLICATION_CREDENTIALS
然后在客户端我不再传递凭据。
session_client = dialogflow.SessionsClient()
编辑 5: 现在一切正常,但似乎没有读取 query_params。
timezone = 'America/New_York'
payload = {
"session": session,
"query_params": {
"time_zone": timezone
},
"query_input": {
"text": {
"text": text_to_be_analyzed,
"language_code": "en-GB"
}
}
}
response = session_client.detect_intent(payload)
我的时间是 GMT+2,所以说晚上 7 点(结束)会在第二天晚上 7 点(对话流)。但是,纽约仍然不是晚上 7 点,所以它应该给我与今天相同的日期。这意味着它仍然假设我的时区。
有什么建议吗?
【问题讨论】:
标签: python flask dialogflow-es