如何使用对话流 python api 在 shell(最好是 repl.it)中制作文本机器人?

How do I make a text bot in a shell (preferably repl.it) using the dialogflow python api?

我正在尝试在我的 repl.it shell 中制作一个简单的聊天机器人,但它出错了,说是 "Could not automatically determine credentials.",我已经查看了很多次,但不能'不知道该怎么做。此外,我可能还有其他问题,但我无法判断,因为我无法克服此错误。 如果有人可以帮助我解决这个问题,或者如果您有一个我可以效仿的例子,那就太好了 https://repl.it/@RyandaKing/ThoughtfulWorrisomeQuadrant

import dialogflow
from google.api_core.exceptions import InvalidArgument

DIALOGFLOW_PROJECT_ID = 'newagent-1-rhjebl'
DIALOGFLOW_LANGUAGE_CODE = 'en-US'
GOOGLE_APPLICATION_CREDENTIALS = 'newagent-1-rhjebl-29ae80f7e64d.json'
SESSION_ID = '110497386060607202274'
text_to_be_analyzed = "Hello"
session_client = dialogflow.SessionsClient()
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)
try:
    response = session_client.detect_intent(session=session, query_input=query_input)
except InvalidArgument:
    raise
print("Query text:", response.query_result.query_text)
print("Detected intent:", response.query_result.intent.display_name)
print("Detected intent confidence:", response.query_result.intent_detection_confidence)
print("Fulfillment text:", response.query_result.fulfillment_text)

原因是您对.json中包含的私钥使用不当。如 here 所述,您应该定义值为 .json 密钥文件路径的环境变量。

假设 .py.json 文件在同一目录中 运行 就像这样。

$ cd path/to/app
$ env 'GOOGLE_APPLICATION_CREDENTIALS=newagent-1-rhjebl-29ae80f7e64d.json' python3 app.py