예제 #1
0
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']
        }
    }]
예제 #2
0
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)
예제 #3
0
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']
    }
예제 #4
0
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
예제 #5
0
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']
    }
예제 #6
0
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']
        }
    }]
예제 #7
0
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'
예제 #8
0
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!']
        }
    }]
예제 #9
0
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")
예제 #10
0
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
예제 #11
0
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()
예제 #12
0
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)