import numpy as np from .root import app import json from .helpers import extract_entities_from_type from chatbot.middleware import latest_intent as l_t from chatbot.middleware.hotelHelper import hotelList from chatbot.middleware.restaurantHelper import getRestaurant from chatbot.middleware.firebaseHelper import firebaseHelper firebase = firebaseHelper() NOT_A_CITY = "Sorry😕...This city is not in India." + "~" + "Let me help you with this🙂" + "~" + "Some popular cities in India are : Lucknow, Goa, Jodhpur, etc." + "~" + "Please choose another city." NOT_A_SPOT = "Sorry😕...This spot is not in our list." + "~" + "Let me help you with this🙂" + "~" + "Some popular spots in India are : Tarsar Marsar, Nandi Hills, , Spiti Valley etc." + "~" + "Please choose another spot." @app.handle(intent='get_spotinfo', has_entity='spot_name') def get_spot_info(request, responder): try: spot_name = "None" if request.entities[0]["type"] == "spot_name": spot_name = request.entities[0]["value"][0]["cname"] else: spot_name = request.entities[1]["value"][0]["cname"] data = app.question_answerer.get(index='spot_data', spot_name=spot_name) trivia = "" try: trivia = data[0]["trivia"] except KeyError: trivia = "Trivia is empty now" image_URL = data[0]["image_URL"]
def __init__(self, name, app_path, nlp=None): """ Args: name (str): The name of the server. app_path (str): The path of the MindMeld application. nlp (NaturalLanguageProcessor): MindMeld NLP component, will try to load from app path if None. """ self.firebase = firebaseHelper() self.app = Flask(name) if not nlp: self.nlp = NaturalLanguageProcessor(app_path) self.nlp.load() else: self.nlp = nlp self.conv = Conversation(nlp=self.nlp, app_path=app_path) self.logger = logging.getLogger(__name__) self.url = None @self.app.route("/", methods=["POST"]) def handle_message(): # pylint: disable=unused-variable # print(request.values) # Getting number from which message came id = request.values.get('From', '') id = id.split('+')[1] # print(request.values) #uncomment this to dif deeper exist = self.firebase.existID(id) if not exist: result = self.firebase.createID(id) incoming_msg = request.values.get('Body', '').lower() location = { 'Latitude': request.values.get('Latitude', ''), 'Longitude': request.values.get('Longitude', '') } if request.values.get('Latitude', '') and request.values.get('Longitude', ''): intent = l_t.getIntent() print(intent) result = self.firebase.setCurrLocation(location, id) resp = MessagingResponse() msg = resp.message() params = dict(dynamic_resource=dict(id=id)) if intent == 'loc_for_source': incoming_msg = "source for location" elif intent == 'loc_for_hotel': incoming_msg = "location for hotel" elif intent == 'loc_for_food': incoming_msg = "location for food" else: incoming_msg = "general location" try: response_text = self.conv.say(incoming_msg, params=params)[0] messages = response_text.split("~") for msg in messages: if msg: sendMessage(msg, id) except IndexError: msg.body("Didn't understand. sorry") else: resp = MessagingResponse() msg = resp.message() # Used to send dynamic id of the user making query params = None if nth.getTarget() == None : params = dict(dynamic_resource =dict(id=id)) #Used to send dynamic id of the user making query else: params = dict(dynamic_resource =dict(id=id),target_dialogue_state=nth.getTarget()) try: response_text = self.conv.say(incoming_msg, params=params)[0] messages = response_text.split("~") for msg in messages: if msg: sendMessage(msg, id) except IndexError: msg.body("Didn't understand. sorry") return str(resp) def sendMessage(msg, number): # Change the from whatsapp number with your twilio account number valid=validators.url(msg) if valid : self.url = msg else: if self.url: client.messages.create(body=msg, from_="whatsapp:+14155238886", to="whatsapp:+"+str(number), media_url=[self.url]) self.url = None else: client.messages.create(body=msg, from_="whatsapp:+14155238886", to="whatsapp:+"+str(number))