def handler(agent: WebhookClient) -> None: """ Handle the webhook request. This handler sends a text message along with a quick replies message back to Dialogflow, which uses the messages to build the final response to the user. """ agent.add('How are you feeling today?') agent.add(QuickReplies(quick_replies=['Happy :)', 'Sad :(']))
def handler2(agent: WebhookClient) -> None: con = Context(agent.context, agent.session) city = con.get("delivered")["parameters"]["geo-city"] resources = con.get("delivered")["parameters"]["resources"] pk_id = con.get("delivered")["parameters"]["id"] feed = con.get("delivered")["parameters"]["feedback"] sup = Supplier.objects.get(pk=pk_id) sup.status = feed sup.save() print(city) print(resources) if not feed == "helpful": dic = helpdata(city, resources) if dic["isany"]: con.get("delivered")["parameters"]["id"] = dic['id'] agent.add( "Here is the detail we have found, might be helpful for you") agent.add(dic['name'] + '\n' + dic['number'] + '\n' + dic["email"] + '\n' + dic["des"] + '\n' + "last updated " + dic['updated']) agent.add( " please give us you feedback on this supplier \n say helpful if it is useful,\n out of stock,\n say not answering or unresponsive if not replying,\n say fraud or scam,\n say wrong or invalid if it is wrong number" ) else: con.delete("delivered") else: agent.add("thanks for information, I am happy to help you") con.delete("delivered")
def test_handler_intent_map(webhook_request): agent = WebhookClient(webhook_request) handler = { 'Default Welcome Intent': lambda agent: agent.add('Hello!'), 'Default Fallback Intent': lambda agent: agent.add('What was that?'), } agent.handle_request(handler) assert agent.response['fulfillmentMessages'] == [{ 'text': { 'text': ['Hello!'] } }]
def quick_summary(queryResult: dict, agent: WebhookClient, session: str) -> WebhookClient: information = queryResult["parameters"]["information"] print("quick summary :", information) try: if (wikipedia.API_URL.split(".")[0].split("/")[-1] != sessions_lang[session]): wikipedia.set_lang(sessions_lang[session][0]) response = wikipedia.summary(information, sentences=1).replace( " ()", '').replace("()", '') if len(response) > 4500: response = response[:4500] + " [...]" agent.add(response) except Exception: agent.add("Désolé, mais je suis incapable de te dire ce qu'est '" + information + "'") return agent
def intent_switch(intent_name: str, queryResult: dict, agent: WebhookClient, session: str) -> str: switcher = { "get_summary": get_summary, "how_are_you": how_are_you, "quick_summary": quick_summary, "set_language": set_language, "which_language": which_language, } func = switcher.get(intent_name) if (func != None): return func(queryResult, agent, session) print("Unknown intent : " + intent_name) agent.add( "Toute mes excuses, mais mon serveur à eu un léger soucis à comprendre ton intention :(" ) return agent
def set_language(queryResult: dict, agent: WebhookClient, session: str) -> WebhookClient: language = queryResult["parameters"]["language"].lower() print("set_language", language) language_changed = False if ((language == "fr") or (language == "français") or (language == "french")): sessions_lang[session][0] = "fr" language_changed = True elif ((language == "en") or (language == "anglais") or (language == "english")): sessions_lang[session][0] = "en" language_changed = True if (language_changed): agent.add("Et hop ! Tes renseignements seront maintenant en " + language) else: agent.add( "Désolé mais je ne suis pas en mesure de te trouver des renseignements dans cette langue :(." ) return agent
def how_are_you(queryResult: dict, agent: WebhookClient, session: str) -> WebhookClient: agent.add(str(classifier(queryResult.get("queryText")))) return agent
def which_language(queryResult: dict, agent: WebhookClient, session: str) -> WebhookClient: agent.add( "Je peux récupérer des informations en français ou en anglais si tu veux !" ) return agent