1

I want to get the stock price of any item. I have a function getPrice which returns the price of the item. I am trying to use wit ai. This is what I have tried.

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))

and the result I am getting is:

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

I want the Bot to display the price. How can I call this action function?

Hunterr
  • 553
  • 1
  • 8
  • 28

2 Answers2

0

You can do

print resp['entities']['item'][0]['value']
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
0

Instead of:

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

try:

stock = resp['entities']['item'][0]['value']
print('Price: {}'.format(getPrice(stock)))
sid8491
  • 6,622
  • 6
  • 38
  • 64