> For the complete documentation index, see [llms.txt](https://den.gitbook.io/developerplaybook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://den.gitbook.io/developerplaybook/services-api/chatbots.md).

# Chatbots

## Architecture Decisions

* [ADR-0005](https://github.com/denseidel/developer-playbook/blob/master/docs/adr/0005-use-dialogflow-for-chatbots.md) - Use Dialogflow for chatbots

## Dialogflow

### [Getting Started](https://dialogflow.com/docs/getting-started/basics)

### Implement the intent

Guide: [Google Documenation](https://dialogflow.com/docs/getting-started/building-your-first-agent)

#### Model your Dialog

Here is the [plantuml activity diagram](http://plantuml.com/activity-diagram-beta)

!["Manage BMI" Dialog](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LE-8HWwU8IrOBl6E4wE%2F-LE-8gyvDIGZmWiaBaYu%2Fmanage-bmi-dialog.svg?alt=media\&token=83cf5738-bc66-4be9-9cf3-3a3599bbb4bb)

[Create a "Default Welcome Intent"](https://dialogflow.com/docs/getting-started/basic-fulfillment-conversation#editing_the_welcome_intent) with answers: "*Hi I am your personal trainer, let me advice you on your health. I can currently calculate your BMI, store the BMI and advices you on your BMI."*

#### Implement a "mange\_bmi" intent&#x20;

After creating the intent add training phrases and activate webhook fulfilment (this is required to check if allready a user exists for this profile):

![](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LE-Aj59-dilUMqwvI2b%2F-LE-B2jC6D_RlVGCWXtf%2Fmanage_bmi_training_phrases.png?alt=media\&token=a38f4bcf-b536-4ea2-94ce-d03282f083c3)

![](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LE-Aj59-dilUMqwvI2b%2F-LE-BNo8b1Vvc8f8v58Z%2Fmanage_bmi_activate_webhook_fulfillment.png?alt=media\&token=2588b3f1-6476-48b1-bfa1-84c6c13e0b6e)

Do the same for the update\_bmi intent (training phrase: My hight is 177 cm, I weigh 80 kg and my birthday is 19.12.1987). How to handle partial dates (<https://miningbusinessdata.com/dialogflow-tutorial-setting-context-from-your-inline-webhook-using-contextout/>)

### Implement the fulfilment&#x20;

#### Using Firebase

To start it makes sense to implement all fulfillments in one webhook on firebases. Later for more complex logic it is adviced to move this logic to it's own webhooks that are then called from the fulfillment webhook. For more details see the later section.&#x20;

Setup Firebase (<https://den.gitbook.io/developerplaybook/services-api/firebase>)

```bash
mkdir fulfillment-router
firebase login
firebase init
```

Guide: [Google Documentation](https://developers.google.com/actions/dialogflow/fulfillment)

* *"we highly recommend that you use the Node.js client library to process requests and return responses"*

#### Using a Webhook with AWS Lambda

First build a webhook with a fullfilment router

<https://medium.com/@pallavtrivedi03/how-to-make-a-webhook-for-dialogflow-fulfillment-d02835cc50bf>

### Test a chatbot - manually & unit test

### Connect the Chatbot to Facebook Messanger

### Integrate fulfilment with REST Python AWS Lambda (e.g. "analyse my bmi trend")&#x20;

{% embed url="<https://dialogflow.com/docs/sdks>" %}

{% embed url="<http://dialogflow-python-client-v2.readthedocs.io/en/latest/?#using-dialogflow>" %}

Sample: <https://github.com/miningbusinessdata/apiai-python-webhook/blob/master/app.py>
