AttributeError: module 'google.cloud.dialogflow' has no attribute 'types'

AttributeError: module 'google.cloud.dialogflow' has no attribute 'types'

我正在构建一个电报机器人并在其中使用 Dialogflow,我收到以下错误:

2021-11-19 23:26:46,936 - __main__ - ERROR - Update '{'message': {'entities': [], 
'delete_chat_photo': False, 'text': 'hi', 'date': 1637344606, 'new_chat_members': [], 
'channel_chat_created': False, 'message_id': 93, 'photo': [], 'group_chat_created': 
False, 'supergroup_chat_created': False, 'new_chat_photo': [], 'caption_entities': [], 
'chat': {'id': 902424541, 'type': 'private', 'first_name': 'Akriti'}, 
'from': {'first_name': 'Akriti', 'id': 902424541, 'is_bot': False, 'language_code': 'en'}
}, 'update_id': 624230278}' caused error 'module 'google.cloud.dialogflow' has no 
attribute 'types''

Dialogflow 属性“types”似乎存在一些问题,但我不知道我做错了什么。

这是我使用它的代码:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "client.json"

from google.cloud import dialogflow
dialogflow_session_client = dialogflow.SessionsClient()
PROJECT_ID = "get-informed-ufnl"

from gnewsclient import gnewsclient

client = gnewsclient.NewsClient()


def detect_intent_from_text(text, session_id, language_code='en'):
    session = dialogflow_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 = dialogflow_session_client.detect_intent(session=session, query_input=query_input)
    return response.query_result



def get_reply(query, chat_id):
    response = detect_intent_from_text(query, chat_id)

    if response.intent.display_name == 'get_news':
        return "get_news", dict(response.parameters)
    else:
        return "small_talk", response.fulfillment_text


def fetch_news(parameters):
    client.language = parameters.get('language')
    client.location = parameters.get('geo-country')
    client.topic = parameters.get('topic')

    return client.get_news()[:5]


topics_keyboard = [
    ['Top Stories', 'World', 'Nation'],
    ['Business', 'Technology', 'Entertainment'],
    ['Sports', 'Science', 'Health']
]

我想通了,问题出在导入语句上。正确的模块名称应该是:

import google.cloud.dialogflow_v2 as dialogflow

我建议停用您当前的错误处理程序或使用类似于 this example 的错误处理程序,这样您就可以看到异常的完整回溯 :)


免责声明:我目前是 python-telegram-bot

的维护者