2017-01-17 6 views
1

私はどの項目の株価も取得したいと思います。私は商品の価格を返す関数getPriceを持っています。 wit aiを使用しようとしています。 これは私が試みたものです。私は取得していますPythonでアクション関数wit 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

:リストインデックスは整数でなければなりません、ないも – HunterrJ

+0

をstr'、'試し

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

をxyz'は結果ではありません。それは私が手に入れようとしている株式です。 – HunterrJ

+0

申し訳ありませんが、リストではありませんでした。更新しました。 –

0

の代わりに行うことができます。それは `TypeError例外を与えて

stock = resp['entities']['item'][0]['value'] 
print('Price: {}'.format(getPrice(stock))) 
関連する問題