Build a Serverless Gym App with ChatGPT, Twilio and WhatsApp

Build a Serverless workout generator using AWS Lambda, Golang, ChatGPT, Twilio, and WhatsApp, and set up a CI/CD pipeline with SAM and GitHub Actions.

twitter
linkedin
reddit
hacker news

We have recently started hosting virtual workshops in our Discord community 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:

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

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.

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

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

The OPENAI_TOKEN value can be generated from the OpenAI platform. 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:

  1. Sign up for a free Twilio account.
  2. Activate the Twilio Sandbox for WhatsApp.
  3. Select a number from the available sandbox numbers to activate your sandbox.
  4. 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

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

go get 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 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

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, 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

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

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

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

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

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

EventBridge integration

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

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
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 and add the hashtag #WardensAssemblyChallenge. The three winners will be chosen on Friday, April 28th.

Regardless if you are a Developer, DevOps, or Cloud engineer. Dealing with the cloud can be tough at times, especially on your own. If you are using Tailwarden or Komiser and want to share your thoughts doubts and insights with other cloud practitioners feel free to join our Tailwarden discord server. Where you will find tips, community calls, and much more.
twitter
linkedin
reddit
hacker news
Related Posts
Ready to use Tailwarden?

Tailwarden is your all-in-one open-source platform. Seamlessly build your cloud asset inventory and gain detailed insights by breaking down costs at the resource level.

Try it for free