Git Product home page Git Product logo

romeo-assistant's Introduction

.NET WhatsApp Bot with OpenAI GPT-3.5, Postgres and Maytapi

This .NET WhatsApp bot project utilizes OpenAI's GPT-3.5 model, Postgres (Supabase) and the Maytapi API. The bot is designed to provide automated, intelligent responses in WhatsApp group interactions.

The Maytapi API allows the bot to integrate with WhatsApp groups, while OpenAI's GPT-3.5 model enables the generation of context-aware text based on user input. All data handling and storage is managed through Supabase, providing a reliable database solution for the project.

Table of Contents

  1. Meet Romeo
  2. Features
  3. Setup
  4. Getting Started
  5. Start Playing
  6. Additional Project: Romeo Assistant Azure Functions

Meet Romeo

Romeo profile

Whatsapp Contact

Romeo profile

Features

This bot is designed to work exclusively within WhatsApp groups. Once added to a group, it can be controlled with the following commands:

!prompt

This command to guide the bot's behavior by suggesting a specific response or action. Prompt Demo

!active

This command will make the bot respond to all messages in the group. This might lead to frequent bot responses. Active Demo

!passive

This command will make the bot respond only when it is specifically mentioned in a message, thus being less intrusive. Passive Demo

!tokens

This command to get a count of the remaining tokens before the bot's "memory" or context is reset. This helps you understand the bot's remaining capacity for remembering previous interactions. Tokens Demo

location

This is a specialized feature for the region of Mallorca. When used, the bot responds with the number of bicycles available near a specified GPS location. Location Demo

Setup

Here are the steps to set up the project on your local machine.

Prerequisites

  • Visual Studio (version 17.5.4 or later)
  • .NET Core SDK
  • Maytapi account
  • OpenAI account
  • Supabase account (Postgres)

Visual Studio

You will need Visual Studio 17.5.4 or later to utilize the Dev Tunnels feature, which allows you to access your local development server from anywhere on the internet. This is crucial for testing and development purposes.

Maytapi account

Maytapi is a crucial component of this project as it allows the bot to integrate seamlessly with WhatsApp. Maytapi provides an API for WhatsApp, enabling your application to send and receive messages, images, and other types of content directly through WhatsApp groups or individual conversations.

One of the reasons for choosing Maytapi over Twilio, which also provides a WhatsApp Business API, is the ability of Maytapi to work directly with standard WhatsApp accounts. While Twilio's WhatsApp API is primarily designed for business accounts, Maytapi's API allows our bot to function in any regular WhatsApp group, making it more versatile for different types of users and uses.

The cost of a Maytapi account is $30 per month. However, if you're not ready to commit to a paid plan just yet, Maytapi offers a 3-day trial period. This allows you to test out the service and see if it meets your project's needs before you decide to purchase a subscription.

To utilize the Maytapi service for this project, it's crucial to have an available WhatsApp number that also has the capability to access WhatsApp Web. Maytapi's service operates by connecting to your WhatsApp account through WhatsApp Web, so this is an essential prerequisite. Make sure that the WhatsApp number you plan to use with Maytapi is set up and ready for WhatsApp Web usage.

Once you have an active Maytapi account linked with a valid WhatsApp number , make sure to copy the Product ID and the Token and store them in a safe place for later use.

OpenAI account

In order to use the OpenAI's GPT-3.5 model for generating responses, you'll need an OpenAI API account. This API allows your bot to communicate with OpenAI's models and use them to generate text.

As of the current OpenAI pricing ($0.002 / 1K tokens), the cost to use the GPT-3.5 model is quite affordable. However, costs can vary based on the usage and specific pricing details can be found on the OpenAI Pricing page

Running the bot for one month has not cost me more than 5 dollars.

Once you have an active OpenAPI account linked with a api key , make sure to copy the secret key and store them in a safe place for later use.

Supabase account

Supabase is an open-source alternative to Firebase, providing a suite of tools and services that allows developers to create, manage, and scale modern web applications faster.

A significant advantage of Supabase is its free tier. For this project, Supabase is an excellent fit because of its compatibility with Postgres, ease of use, real-time capabilities, and cost-effectiveness.

You will need to create a database in supabase to store whatsapp messages.

Once you have an active Supabase account created with a database already settled , make sure to copy your supabase url and the supabase api key and store them in a safe place for later use.

Getting start

Clone the repository

git clone https://github.com/brunonuzzi/romeo-assistant.git

and open the romeo-assistant.sln on visual studio 17.5.4 or later

Update configurations

You must update the config.json file on /romeo-assistant-core/config.json

Make sure to modify the following keys and save.

"OpenAI": {
    "ApiKey": "__OPEN AI API KEY__"
  },
"Maytapi": {
        "ApiKey": "__MAY TAPI API KEY__",
        "ProductId": "__MAY TAPI PRODUCT ID__",
    }
"Supabase": {
        "Url": "__SUPA BASE DATABASE URL__",
        "Key": "SUPA BASE API KEY",
    } ,
"RomeoSetup": {
        "RomeoNumber": "__TELEPHONE NUMBER WITH CONTRY PREFIX__",
        "AzureFunctionsUrl": "__",
    }   

Create tables on Postgres (supabase)

You must create the 3 tables responsible for managing all api messages in supabase. The script creation of each one of the tables is here database/schemas.sql

Just open the supabase sql editor and execute the script. Supabase configuration

Very important: You should remove the rls feature for all the tables , otherwise the application will not work

Rls configuration

Start playing

Once you have:

  1. The open ai account created and configured on config.json
  2. The maytapi account created and configured on config.json
  3. The supabase account created and configured on config.json
  4. The tables created on supabase with rls removed
  5. The project opened in visual studio with the dev tunnels feature enabled.

Choose the romeo-assistant-api and Set as startup project

The Program.cs for the romeo-assistant-api has an initialization configuration that will register your local url generated through the dev tunnel in the may tapi api.

Make sure to have the dev tunnels enabled.

Dev tunnels

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseMiddleware<LogRequestMiddleware>();

    // Resolve the IWhatsappService instance and call the RunOnce method
    using var serviceScope = app.Services.CreateScope();

    var serviceProvider = serviceScope.ServiceProvider;
    var whatsappService = serviceProvider.GetRequiredService<IWhatsappService>();
    var localUrl = $"{Environment.GetEnvironmentVariable("VS_TUNNEL_URL")}Whatsapp";
    whatsappService.ConfigureWebHook(localUrl);
}

This is a webhook configuration, which means that whenever a new message arrives to a whatsapp group where Romeo is registered, the url of your local environment will be called from maytapi to manage the incoming message.

Webhook configuration

Hit the play

Start the application Run app

You should see the api ready to receive the incoming requests from maytapi. Run app

Making sure everything is working

  • You could validate if your local webhook is properly configured on maytapi over here
  • You also should see all the incoming messages through the console of your application

Up and running app

Congrats !!! , your bot now is running locally.

Additional Project: Romeo Assistant Azure Functions

Within the solution, you'll find an additional project named "romeo-assistant-az-functions". This project is an Azure Function that is designed to run in the Azure environment.

An important aspect of this project is its automated pipeline. This pipeline is set up in such a way that any changes pushed to the master branch of the GitHub repository trigger a GitHub Action. This GitHub Action automatically publishes the changes, ensuring that the Azure Function is always up-to-date with the latest code changes.

This setup enhances the development and deployment process, making it efficient, reliable, and hassle-free. It ensures continuous deployment and allows for rapid iterations and updates to the Azure Function.

Enabling production mode

Once the url of your azure function is published and known, the maytapi registration web hook could be manual called to direct all traffic to the azure function.

Webhook configuration Webhook configuration

romeo-assistant's People

Contributors

brunonuzzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.