Build a Serverless Gym App with ChatGPT, Twilio and WhatsApp
Tags: serverless, go, aws, programming
Published: 2023-04-03 17:05:42

We have recently started hosting virtual workshops in our [Discord community](http://discord.tailwarden.com/) called “Wardens Assembly”. These monthly events cover a variety of tech topics. The first event was about building a Serverless gym app that sends a workout plan to your WhatsApp number using ChatGPT. In case you missed the event, you can watch it completely here:

{% embed https://youtu.be/z2JaX-2Cn2w %}

In case you want to cut straight to the chase, this tutorial is for you as it covers only the key parts of building a Serverless app, including:

-   Building a Serverless app using Golang & AWS Lambda
-   Scheduling Lambda with cron expression triggers
-   Integrating ChatGPT 4 with AWS Lambda
-   Sending WhatsApp messages via the Twilio SDK
-   Streamlining deployment with GitHub Actions & AWS SAM

Before jumping into the code, the diagram below summarizes the architecture we’re going to build by end of this tutorial:

![Application’s architecture](https://miro.medium.com/v2/resize:fit:700/0*NPMNELL9W7C70Dqt.png)

The goal is to create a Lambda function in Go, which will communicate with ChatGPT. This function will send a prompt to ChatGPT API and then use Twilio to send a workout plan to our WhatsApp number at a specific schedule determined by an EventBridge Rule. Moreover, we will leverage AWS SAM and GitHub Actions to automate the infrastructure build and deployment, as well as the CI/CD pipeline.

> You can find all the source code used in this tutorial on [GitHub](https://github.com/tailwarden/virtual-workshops).

## Building Lambda Function

To get started, from your terminal create a main.go file, initialize a go project, and install the AWS Lambda package with the following commands:

`go mod init workout-generator
go get github.com/aws/aws-lambda-go`

Next, declare the handler function in _main.go_. The main function calls the lambda _handler_ by calling the _lambda.Start_ method_._

![Lambda handler](https://miro.medium.com/v2/resize:fit:700/0*PL8m296LjZKpyozc.png)

Next, to integrate with OpenAI, you will need to download the OpenAI Go wrapper library:

`go get github.com/sashabaranov/go-openai`

Then, update the handler, and create an OpenAI client by passing your OpenAI token. Next, start a chat using the _CreateChatCompletion_ method and pass a prompt, and _setGPT3Dot5Turbo_ as the target model (which is the underlying name for ChatGPT). The library also supports other models, such as ChatGPT, GPT-4, DALL·E 2, and Whisper.

![ChatGPT Go integration](https://miro.medium.com/v2/resize:fit:700/0*UFHxtpP34eyroMzj.png)

The _OPENAI\_TOKEN_ value can be generated from the [OpenAI platform](https://platform.openai.com/account/api-keys). Make sure to save the key in a safe place, as it will only be shown to you once.

To send the workout plan to our WhatsApp number, we need to integrate Twilio. To do so, you will need to sign up for a Twilio account, sign in to your existing account, and activate the Twilio Sandbox for WhatsApp. Follow the steps below:

-   Sign up for a [free Twilio account](https://www.twilio.com/try-twilio).
-   Activate the Twilio [Sandbox for WhatsApp](https://www.twilio.com/console/sms/whatsapp/sandbox).
-   Select a number from the available sandbox numbers to activate your sandbox.
-   Send join _<your sandbox keyword>_ to your Sandbox number in WhatsApp to join your Sandbox.

After sending the message, Twilio should reply with a confirmation message, as shown in the screenshot below:

![Twilio Sandbox confirmation](https://miro.medium.com/v2/resize:fit:700/0*jwWzm4qs9SpYqkdF.png)

Now to integrate with our app, install the Twilio Go package with the following command:

`go get https://github.com/twilio/twilio-go`

Next, add the following code snippet to the Lambda handler. It creates a Twilio client by passing the credentials as environment variables. The Account SID and Auth Token can be found [here](http://twilio.com/console) and should be set as the values for the environment variables _TWILIO\_USERNAME_ and _TWILIO\_PASSWORD_ respectively. Finally, it uses the _CreateMessage_ method to send the workout plan generated by ChatGPT.

![Sending message with Twilio SDK](https://miro.medium.com/v2/resize:fit:700/0*2TXcHsUQsiJR9mOh.png)

That’s it. Our function handler is ready to be deployed to AWS Lambda!

## Deploying Serverless Stack with SAM

For the deployment part, we’re going to use AWS Serverless Application Model (SAM). Once you’ve installed the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html), create a template.yml that declares a Lambda function called “WorkoutGenerator”, including its source code location, handler function, runtime environment, memory size, and timeout duration.

![SAM template](https://miro.medium.com/v2/resize:fit:700/0*cR-3I-f9Vohb-OCJ.png)

The template also defines a set of environment variables for the function. These variables are resolved using the AWS Systems Manager Parameter Store, which is a service that stores secure strings and parameters. The variables specified in this template include the OpenAI token, Twilio account credentials, and phone numbers for sending and receiving WhatsApp messages.

![Lambda variables stored in Parameter Store](https://miro.medium.com/v2/resize:fit:700/0*AhNMH2wxZYHKZKHf.png)

Next, use the AWS SAM CLI to build the application and prepare for deployment by running the _sam build_ command. Finally, run the _sam deploy — guided_ command to deploy the AWS resources by provisioning an AWS CloudFormation stack:

![The sam deploy command’s output](https://miro.medium.com/v2/resize:fit:700/0*pcxljtqoU1Fbh091.png)

The Lambda function is now deployed and running in the AWS Cloud! You can test it out, by triggering the Lambda function manually from the AWS Console. A WhatsApp message should be received with a workout plan generated by ChatGPT as follows:

![Workout plan generated by ChatGPT](https://miro.medium.com/v2/resize:fit:700/0*rX-HZtmBu8Xa6kMP.png)

However, our goal is to have our workout plan generated automatically, ideally before we begin our gym workout. To achieve this, we need to trigger our Lambda function at a specific schedule. We can use an EventBridge rule to define a cron expression that invokes our function every weekday at 7 PM. Update the SAM template below and run it again:

![Triggering Lambda function with a cron job](https://miro.medium.com/v2/resize:fit:700/0*S8clxAjd6sVEECIJ.png)

If you head back to the Lambda dashboard, you should see an EventBridge Rule trigger, as shown in the screenshot below:

![EventBridge integration](https://miro.medium.com/v2/resize:fit:700/0*lHHOrfFXl6g2tybI.png)

With our application being completed, let’s build a CI/CD pipeline to automate the deployment process through GitHub Actions.

## Defining a CI/CD Pipeline with GitHub Actions

Once your source code is pushed to a remote GitHub repository, create a _release.yml_ file under the _.github/workflows_ folder with the following steps:

-   Check out the repository under _$GITHUB\_WORKSPACE_, so the workflow can access it.
-   Set up Python environment and install SAM CLI.
-   Configure AWS credentials from secrets. Make sure to add _AWS\_ACCESS\_KEY\_ID_ and _AWS\_SECRET\_ACCESS\_KEY_ as a secret under the GitHub repository.
-   Run the _sam build_ and _sam deploy_ commands in a non-interactive mode.

![CI/CD pipeline](https://miro.medium.com/v2/resize:fit:700/0*FwPOp-m32F1UPiAZ.png)

Once the pipeline is defined, push the changes to the remote repository.

We can test out the pipeline by improving the ChatGPT prompt to generate a workout plan with only weightlifting exercises. Push the changes, the pipeline will be triggered and new changes will be deployed to AWS as shown below:

![GitHub Actions pipeline’s output](https://miro.medium.com/v2/resize:fit:700/0*BWizl77yrIxSoTj4.png)

> To improve the workout plan, provide your age, gender, weight, height, and one rep max in the ChatGPT prompt.

Congratulations! You have successfully built a personal gym workout generator. We would love to see your implementation. Create a similar Serverless app that integrates with ChatGPT and share it on Twitter to enter the competition. Tag [Tailwarden on Twitter](https://twitter.com/tailwarden) and add the hashtag #WardensAssemblyChallenge. The three winners will be chosen on Friday, April 28th.