ProgramBuddy - Helper Bot to create project (rest-api)
Tags: node, aws, python
Published: 2022-10-23 17:17:23

Send messages in WhatsApp and get your project started right away.

[GitHub Repo](https://github.com/imprakashraghu/aws-programbuddy-bot)

### Overview 🤖
The main focus is about stop doing the work which does not require much thinking and following the procedures would get the work done. Here the ideology is to work with rest apis using javascript. How great it would be when we just ask a person to create a project with standard CRUD apis and then give it to us in a way we can add more functions to it.

### What I used to built 🛠
- Amazon Lex
- Amazon Lambda
- Amazon S3 Bucket
- Amazon Dynamo DB
- Amazon Simple Email As Service
- Twilio

{% embed https://player.vimeo.com/video/763094342?h=4bd846b3a4&amp;badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479 %}

### The Bot

Creating the required Intents for project creation, capturing the model information and exporting the project. This bot is focusing mostly on a nodejs express project with mongodb as database.

Amazon Lex is used for the bot creation and training, with four intents as mentioned which is connected to Amazon Lambda triggered at the moment of fulfilment of each intent. Slots are used to get the required information on each intent.

Intent 01: (ProjectCreation)
Slots (Name, EmailAddress, Framework, ProjectName). These information are used as an high level for the project where used to create all configuration files.

![Intent 01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jaw8jqheytdb56e5oz8x.png)

Intent 02: (CapturingModel)
Slots (ModelName, ModelProperties) These information are used to create as much models we need (reusable intent)

![Intent 02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu2rjkbzpzpefr9c7kvq.png)

Intent 03: (ExportingProject)
Used to export the files and folders based on the information saved.

### Lambda Function ⚡️

It plays a major role here to process all the data from the chatbot and have a copy in the Amazon Dynamo DB to make use later. Python environment is used to extract and process and create necessary files and folders and save it to Amazon S3 bucket.

[View Lambda Function](https://raw.githubusercontent.com/imprakashraghu/aws-programbuddy-bot/main/lambda.py)

### ProjectCreation Intent Extraction

```py
if intent_name == 'ProjectCreation':
        payload = {
            "sessionId": event["sessionId"],
            "name": get_slot(event, 'Name'),
            "email": get_slot(event, 'EmailAddress'),
            "project_name": get_slot(event, 'ProjectName'),
            "framework": get_slot(event, 'Framework')
        }
        # save to dynamodb
        projectTable.put_item(Item=payload)
```

### CapturingModel Intent Extraction

```py
if intent_name == 'CapturingModel':
        properties = get_slot(event, 'ModelProperties')
        name = get_slot(event, 'ModelName')
        if properties is not None:
            props_array = properties.split(',')
            sessionUser = projectTable.get_item(Key={'sessionId':event['sessionId']})
            # save to dynamodb
            schemaTable.put_item(Item={
                'sessionId':event['sessionId'],
                'model_name':name,
                'properties':props_array,
                'username':sessionUser['Item']['name']
            })
```

### ExportingProject Intent Extraction

```py
if intent_name == 'ExportProject':
        sessionId = event['sessionId']
        projectInformation = projectTable.get_item(Key={'sessionId':sessionId})
        models = schemaTable.scan(
            FilterExpression='sessionId = :sId',
            ExpressionAttributeValues={":sId": sessionId}
        )
        # creating package json file
        buildPackageFile(projectInformation['Item'])
        # creating db and configuration files and folders
        createRequiredFiles(projectInformation['Item'])
        # creating router files
        createRouterFiles(projectInformation['Item'], models['Items'])
        # creating app.js file
        createMainFile(projectInformation['Item'], models['Items'])
        # zip the files and folder
        url = createZipFileStream('program-buddy', projectInformation['Item']['name']+"/"+projectInformation["Item"]['sessionId'], projectInformation['Item']['project_name'], True)
        sendEmail(projectInformation['Item'], url)

```

### Storing the Data 📦

Dynamo DB is used to store intermediate data which is used along during exporting the project.

```json
# Project Information
{
 sessionId: "BOT_SESSION_ID",
 name: "USERNAME",
 email: "USER_EMAIL",
 project_name: "PROJECT_NAME",
 framework: "PROJECT_FRAMEWORK_NAME"
}
# Schema Information
{
 sessionId: "BOT_SESSION_ID",
 username: "USERNAME",
 properties: ["Item1", "Item2", "Item3"],
 model_name: "SCHEMA_NAME"
}
```

### Exporting the Project

Export intent is triggered and required files and folder for the project are created in lambda and then stored in s3 bucket. These files are then zipped and sent to the recipient email address using Amazon SES.
This email contains the S3 pre-signed URL to download the zipped project files.

[Download Example Exported Project](https://github.com/imprakashraghu/aws-programbuddy-bot/blob/main/example-project-creation.zip?raw=true)


![Project File Sent to email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7ldy555rvpcp42ygn8d.png)