python-2.7 - httplib.ResponseNotReady 与 api.ai

标签 python-2.7 httplib dialogflow-es

我正在尝试使用 api.ai 制作自己的聊天机器人。

第一次很顺利,但第二次我收到此错误:

   Traceback (most recent call last):
  File "/root/Documents/Projects/Darlene/core/api.py", line 34, in <module>
    main()
  File "/root/Documents/Projects/Darlene/core/api.py", line 28, in main
    speech, action = b.handle(cmd)
  File "/root/Documents/Projects/Darlene/core/api.py", line 15, in handle
    response = self.request.getresponse().read()
  File "/usr/local/lib/python2.7/dist-packages/apiai/requests/request.py", line 133, in getresponse
    return self._connection.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1123, in getresponse
    raise ResponseNotReady()
httplib.ResponseNotReady

看起来 api 等有问题。但我不确定。 这是我的代码:

import apiai
import json



class Bot(object):
    def __init__(self, client_token='<clientToken>'):

        self.AI = apiai.ApiAI(client_token)
        self.request = self.AI.text_request()
        self.request.lang = 'en'

    def handle(self, text):
        self.request.query = text
        response = self.request.getresponse().read()
        speech = str(json.loads(response)['result']['fulfillment']['speech'])
        action = str(json.loads(response)['result']['action'])
        if action is not '':
            return speech, action
        else:
            return speech, None


def main():
    b = Bot()
    while True:
        cmd = raw_input('me; ')
        speech, action = b.handle(cmd)
        print speech
        if action is not None:
            print 'action'

if __name__ == '__main__':
    main()

有人知道解决这个问题的方法吗?

最佳答案

请不要重复使用请求。只是为每个阶段创建新的请求。同样这样:

def handle(self, text):
    self.request = self.AI.text_request()
    self.request.lang = 'en'

    self.request.query = text
    response = self.request.getresponse().read()
    speech = str(json.loads(response)['result']['fulfillment']['speech'])
    action = str(json.loads(response)['result']['action'])
    if action is not '':
        return speech, action
    else:
        return speech, None

关于python-2.7 - httplib.ResponseNotReady 与 api.ai,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184882/

相关文章:

python - Tkinter:使用鼠标绘制矩形

python - Django/httplib : transmitting request. raw_post_data 与 httplib

python - 在 Python 中使用 httplib 无法获得成功的响应

python - Dialogflow v2 错误 “MalformedResponse ' Final_response' 必须设置”

Python 自动点击脚本

python - Django Tastypie 记录级授权

python - 将 mysql 连接到 django

python - 如何使用 Py2Neo 修复 Linux 上的 IncompleteRead 错误

node.js - Dialogflow v2 使用 Firebase Cloud Function 返回 Firestore 数据库数据

java - HTTP 错误代码 415 - JAVA 中的 JSON 数组