2017-01-17 76 views
1

我想獲取任何項目的股票價格。我有一個功能getPrice它返回該項目的價格。我正在嘗試使用wit ai。 這是我試過的。在python中調用動作函數api

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?' 
} 

我想博特顯示的價格。我怎樣才能調用這個動作函數?

回答

0

你可以做

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

它給'類型錯誤:列表索引必須是整數,而不是str' – HunterrJ

+0

也' xyz'不是結果。這是我想要獲得的股票。 – HunterrJ

+0

對不起,沒有看到它是一個列表。已更新。 –

0

相反的:

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

嘗試:

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