Webhook client#

class WebhookClient(request)[source]#

Bases: object

A client class for handling webhook requests from Dialogflow.

This class allows to dinamically manipulate contexts and create responses to be sent back to Dialogflow (which will validate the response and send it back to the end-user).

Parameters

request (dict) – The webhook request object (WebhookRequest) from Dialogflow.

Raises

TypeError – If the request is not a dictionary.

Return type

None

See also

For more information about the webhook request object, see the WebhookRequest section in Dialogflow’s API reference.

query#

The original query sent by the end-user.

Type

str

intent#

The intent triggered by Dialogflow.

Type

str

action#

The action defined for the intent.

Type

str

context#

An API class for handling input and output contexts.

Type

Context

contexts#

The array of input contexts.

Type

list(dict)

parameters#

The intent parameters extracted by Dialogflow.

Type

dict

console_messages#

The response messages defined for the intent.

Type

list(RichResponse)

original_request#

The original request object from detectIntent/query.

Type

str

request_source#

The source of the request.

Type

str

locale#

The language code or locale of the original request.

Type

str

session#

The session id of the conversation.

Type

str

add(responses)[source]#

Add response messages to be sent back to Dialogflow.

Examples

Adding a simple text response as a string:

>>> agent.add('Hi! How can I help you?')

Adding multiple rich responses one at a time:

>>> agent.add(Text('How are you feeling today?'))
>>> agent.add(QuickReplies(quick_replies=['Happy :)', 'Sad :(']))

Adding multiple rich responses at once:

>>> responses = [
...     Text('How are you feeling today?'),
...     QuickReplies(quick_replies=['Happy :)', 'Sad :('])
... ]
>>> agent.add(responses)
Parameters

responses (str, RichResponse, list(str, RichResponse)) – A single response message or a list of response messages.

Return type

None

property followup_event: Optional[Dict[str, Any]]#

The followup event to be triggered by the response.

Examples

Accessing the followup_event attribute:

>>> agent.followup_event
None

Assigning an event name to the followup_event attribute:

>>> agent.followup_event = 'WELCOME'
>>> agent.followup_event
{'name': 'WELCOME', 'languageCode': 'en-US'}

Assigning an event dictionary to the followup_event attribute:

>>> agent.followup_event = {'name': 'GOODBYE', 'languageCode': 'en-US'}
>>> agent.followup_event
{'name': 'GOODBYE', 'languageCode': 'en-US'}
Raises

TypeError – If the event is not a string or a dictionary.

Type

dict, optional

handle_request(handler)[source]#

Handle the webhook request using a handler or a mapping of handlers.

In order to manipulate the conversation programatically, the handler function must receive an instance of WebhookClient as a parameter. Then, inside the function, WebhookClient’s attributes and methods can be used to access and manipulate the webhook request attributes and generate the webhook response.

Alternatively, this method can receive a mapping of handler functions for each intent.

Note

If a mapping of handler functions is provided, the name of the corresponding intent must be written exactly as it is in Dialogflow.

Finally, once the request has been handled, the generated webhook response can be accessed via the response attribute.

Examples

Creating a simple handler function that sends a text and a collection of quick reply buttons to the end-user (the response is independent of the triggered intent):

>>> def handler(agent: WebhookClient) -> None:
...     agent.add('How are you feeling today?')
...     agent.add(QuickReplies(quick_replies=['Happy :)', 'Sad :(']))

Creating a mapping of handler functions for different intents:

>>> def welcome_handler(agent):
...     agent.add('Hi!')
...     agent.add('How can I help you?')
...
>>> def fallback_handler(agent):
...     agent.add('Sorry, I missed what you said.')
...     agent.add('Can you say that again?')
...
>>> handler = {
...     'Default Welcome Intent': welcome_handler,
...     'Default Fallback Intent': fallback_handler,
... }
Parameters

handler (callable, dict(str, callable)) – The handler function or a mapping of intents to handler functions.

Raises

TypeError – If the handler is not a function or a map of functions.

Returns

The output from the handler function (if any).

Return type

any, optional

property response: Dict[str, Any]#

The generated webhook response object (WebhookResponse).

See also

For more information about the webhook response object, see the WebhookResponse section in Dialogflow’s API reference.

Type

dict