コード例 #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
ファイル: app.py プロジェクト: wags69a/dialogflow-fulfillment
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)