【问题标题】:Calling action function wit api in python在python中使用api调用动作函数
【发布时间】:2017-01-17 08:35:48
【问题描述】:

我想获得任何商品的股票价格。我有一个函数getPrice,它返回商品的价格。我正在尝试使用wit ai。 这是我尝试过的。

from wit import Wit

def getPrice(request):
    #statements
    return price


def send(request, response):
    print request['text']
    print('Sending to user...', response['text'])

actions = {
    'send': send,
    'getPrice':getPrice
}

client = Wit(access_token="<token>", actions=actions)

resp = client.message('what is the stock price of xyz')
print('Result: ' + str(resp))

我得到的结果是:

Result: {
u 'entities': {
    u 'search_query': [{
        u 'suggested': True,
        u 'confidence': 0.578445451443443,
        u 'type': u 'value',
        u 'value': u 'stock of xyz'
    }],
    u 'item': [{
        u 'confidence': 0.9613219630126943,
        u 'value': u 'xyz'
    }]
},
u 'msg_id': u '5ac1a450-7c5d-3190-8666-5d88a1884f94',
u '_text': u 'what is the stock of xyz?'
}

我希望机器人显示价格。如何调用这个动作函数?

【问题讨论】:

    标签: python bots wit.ai wit-ai


    【解决方案1】:

    你可以的

    print resp['entities']['item'][0]['value']
    

    【讨论】:

    • TypeError: list indices must be integers, not str
    • 另外,xyz 不是结果。这是我想要获得的股票。
    • 错误已修复,但未返回价格。它显示了我要查找的库存的`项目名称`。
    【解决方案2】:

    代替:

    print('Result: ' + str(resp))
    

    尝试:

    stock = resp['entities']['item'][0]['value']
    print('Price: {}'.format(getPrice(stock)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2011-12-18
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多