Search code examples
pythonchatbotwit.ai

Python integration with Wit.ai


I am new to Wit.ai and I am trying to make a chatbot. This is the chatbot straight from the quickstart. What am I doing wrong?

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import sys
from wit import Wit

if len(sys.argv) != 2:
    print('usage: python ' + sys.argv[0] + ' <wit-token>')
    exit(1)
access_token = sys.argv[1]

client = Wit(access_token=access_token)
client.interactive()

def set_temperature(temp):
    print('hi')

def get_temperature():
    print('bye')

I would like to execute the functions set_temperature and get_temperature.

python3 wit_test.py IG3OBYOAQJJPDCQFNLPVVXDCM6TS5ZPN
> make the temperature 45                                                       
{'_text': 'make the temperature 45', 'entities': {'intent': [{'confidence': 0.986478544826, 'value': 'set_temperature'}]}, 'WARNING': 'DEPRECATED', 'msg_id': '1CyIOWaEfapF9bbh0'}
> what is the temperature                                                       
{'_text': 'what is the temperature', 'entities': {'intent': [{'confidence': 0.98791564105294, 'value': 'get_temperature'}]}, 'WARNING': 'DEPRECATED', 'msg_id': '1qPNFDlSmECpUN8UG'}

Instead of {'_text': 'make the temperature 45', 'entities': {'intent': [{'confidence': 0.986478544826, 'value': 'set_temperature'}]}, 'WARNING': 'DEPRECATED', 'msg_id': '1CyIOWaEfapF9bbh0'} I'd like hi to be printed. Thanks!


Solution

  • If I understood correctly what you want:

    def interactive(self, handle_message=None, context=None) I would say you can put your desired messages when starting the interactive conversation with your bot

    
    client.interactive(handle_message = "I will handle the temperature")
    
    

    If you want to customise your message using some functions , you can do something like this:

    
    def set_temperature(temp, msg):
        return "{0} I am setting the temperature to {1}".format(msg,temp)
    
    client.interactive(handle_message = set_temperature("hi",25))