使用Cloud Functions,Firestore和Python SDK进行Dialoflow时应在哪里编写数据库查询

时间:2018-08-02 17:37:19

标签: python google-cloud-firestore dialogflow

我在Dialogflow中创建了一个聊天机器人,现在将其集成到我的Python环境(PyCharm)中,我已将Python连接到Google云平台(这是我的Cloud Functions,Cloud Firestore和Dialogflow可访问性所在的位置),一切都连接良好,我知道这一点是因为当我键入内容时,聊天机器人会以默认的intent进行响应。我还尝试过写入数据库以创建内容,如图here(有效)

但是根据this的问题,我希望能够在触发意图后查询数据库,因此我的代码必须稍作更改;现在是这样的:

import os, json
import sys
import dialogflow
from dialogflow_v2beta1 import *
import firebase_admin
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
from firebase_admin import firestore
from firebase_admin import credentials
import requests.packages.urllib3
from Tkinter import *
from dialogflow_v2beta1 import agents_client
import Tkinter as tk
result = None
window = Tk()

def Response():
    #no need to use global here
    result = myText.get()
    displayText.configure(state='normal')
    displayText.insert(END, "User:"+ result + '\n')
    displayText.configure(state='disabled')

#Creating the GUI
myText = tk.StringVar()
window.resizable(False, False)
window.title("Chatbot")
window.geometry('400x400')
User_Input = tk.Entry(window, textvariable=myText, width=50).place(x=20, y=350)
subButton = tk.Button(window, text="Send", command=Response).place(x =350, y=350)
displayText = Text(window, height=20, width=40)
displayText.pack()
scroll = Scrollbar(window, command=displayText).pack(side=RIGHT)
window.mainloop()

#Initialize the firebase admin SDK
cred = credentials.Certificate('./file.json')
default_app = firebase_admin.initialize_app(cred)
db = firestore.client()

def getCourse():
    doc_ref = db.collection(u"Course_Information").document(u"CourseTypes")
    try:
        doc = doc_ref.get()
        return 'Document data: {}'.format(doc.to_dict())
    except google.cloud.exceptions.NotFound:
        return 'Not found'

def detect_intent_text(project_id, session_id, text, language_code):
    GOOGLE_APPLICATION_CREDENTIALS=".chat-8.json"
    session_client = dialogflow.SessionsClient(GOOGLE_APPLICATION_CREDENTIALS)

    session = session_client.session_path(project_id, session_id)

    text_input = dialogflow.types.TextInput(
        text=text, language_code=language_code)

    query_input = dialogflow.types.QueryInput(text=text_input)

    response = session_client.detect_intent(
        session=session, query_input=query_input)

queryText = [myText.get()]

res = detect_intent_text('chat-8', 'session-test', queryText, 'en')
intentName = res['query_result']['intent']['display_name']

if intentName == 'CourseEnquiry':
    reponse = getCourse()
    print json.dumps({
        'fulfillmentText': reponse,
    })
elif intentName == 'Greetings':
    print "Yo"

detect_intent_texts("chat-8","abc", queryText,"en-us")

现在我运行时开始显示错误(不再获得漫游器的响应_;

Traceback (most recent call last):
  File "C:/Users/Ashley/PycharmProjects/Chatbot/venv/Chatbot.py", line 59, in <module>
    res = detect_intent_text('chat-8', 'session-test', queryText, 'en')
  File "C:/Users/Ashley/PycharmProjects/Chatbot/venv/Chatbot.py", line 45, in detect_intent_text
    session_client = dialogflow.SessionsClient(GOOGLE_APPLICATION_CREDENTIALS)
  File "C:\Users\Ashley\PycharmProjects\Chatbot\venv\lib\site-packages\dialogflow_v2\gapic\sessions_client.py", line 109, in __init__
    self.sessions_stub = (session_pb2.SessionsStub(channel))
  File "C:\Users\Ashley\PycharmProjects\Chatbot\venv\lib\site-packages\dialogflow_v2\proto\session_pb2.py", line 1248, in __init__
    self.DetectIntent = channel.unary_unary(
AttributeError: 'str' object has no attribute 'unary_unary'

我认为这与后端设置不正确有关, 我的Dialogflow webhook选项链接到cloud Functions中提供的URL,但是我在哪里写数据库查询代码?在代码中,就像我上面在getCourse()函数中还是在云函数中一样?

0 个答案:

没有答案