def test_add_list_of_texts(webhook_request): agent = WebhookClient(webhook_request) def handler(agent): agent.add(['this', 'is', 'a', 'list', 'of', 'texts']) agent.handle_request(handler) assert agent.response['fulfillmentMessages'] == [{ 'text': { 'text': ['this'] } }, { 'text': { 'text': ['is'] } }, { 'text': { 'text': ['a'] } }, { 'text': { 'text': ['list'] } }, { 'text': { 'text': ['of'] } }, { 'text': { 'text': ['texts'] } }]
def test_add_non_richresponse(webhook_request): agent = WebhookClient(webhook_request) def handler(agent): agent.add({'this': 'is not a RichResponse'}) with pytest.raises(TypeError): agent.handle_request(handler)
def test_assign_followup_event(webhook_request): agent = WebhookClient(webhook_request) def handler(agent): agent.followup_event = 'test_event' agent.handle_request(handler) assert agent.response['followupEventInput'] == { 'name': 'test_event', 'languageCode': webhook_request['queryResult']['languageCode'] }
def test_no_contexts(webhook_request): modified_webhook_request = webhook_request modified_webhook_request['queryResult']['outputContexts'] = [] agent = WebhookClient(modified_webhook_request) def handler(agent): pass agent.handle_request(handler) assert 'outputContexts' not in agent.response
def test_set_followup_event_by_dict(webhook_request): agent = WebhookClient(webhook_request) def handler(agent): with pytest.warns(DeprecationWarning): agent.set_followup_event({'name': 'test_event'}) agent.handle_request(handler) assert agent.response['followupEventInput'] == { 'name': 'test_event', 'languageCode': webhook_request['queryResult']['languageCode'] }
def test_add_text(webhook_request): agent = WebhookClient(webhook_request) def handler(agent): agent.add('this is a text') agent.handle_request(handler) assert agent.response['fulfillmentMessages'] == [{ 'text': { 'text': ['this is a text'] } }]
def test_with_request_source(webhook_request): modified_webhook_request = webhook_request modified_webhook_request['originalDetectIntentRequest']['source'] = \ 'PLATFORM_UNSPECIFIED' agent = WebhookClient(modified_webhook_request) def handler(agent): pass agent.handle_request(handler) assert agent.response['source'] == 'PLATFORM_UNSPECIFIED'
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 webhook(request: HttpRequest) -> HttpResponse: if request.method == 'POST': req = loads(request.body) # logger.info(f'Request headers:{dict(request.headers)}') # logger.info(f'Request body:{req}') agent = WebhookClient(req) if (agent.intent == "loop-back"): agent.handle_request(handler2) else: agent.handle_request(handler) # logger.info(f'Response body:{agent.response}') return JsonResponse(agent.response) return HttpResponse("hello")
def webhook() -> Dict: """Handle webhook requests from Dialogflow.""" # Get WebhookRequest object request_ = request.get_json(force=True) # Log request headers and body logger.info(f'Request headers: {dict(request.headers)}') logger.info(f'Request body: {request_}') # Handle request agent = WebhookClient(request_) agent.handle_request(handler) # Log WebhookResponse object logger.info(f'Response body: {agent.response}') return agent.response
def webhook(request: HttpRequest) -> HttpResponse: """Handle webhook requests from Dialogflow.""" if request.method == 'POST': # Get WebhookRequest object request_ = loads(request.body) # Log request headers and body logger.info(f'Request headers: {dict(request.headers)}') logger.info(f'Request body: {request_}') # Handle request agent = WebhookClient(request_) agent.handle_request(handler) # Log WebhookResponse object logger.info(f'Response body: {agent.response}') return JsonResponse(agent.response) return HttpResponse()
from dialogflow_fulfillment import QuickReplies, WebhookClient # Define a custom handler function 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 :('])) # Create an instance of the WebhookClient agent = WebhookClient(request) # noqa: F821 # Handle the request using the handler function agent.handle_request(handler)