Using ChatGPT to reply my Whatsapp ?
Tags: ai, javascript, tutorial, beginners
Published: 2023-02-28 01:51:26

With the advent of artificial intelligence (AI) and natural language processing (NLP), communication has become more seamless than ever before. One such development in this field is the creation of large language models, such as ChatGPT, which can be used to reply to WhatsApp messages. In this article, we will discuss how ChatGPT can be used to reply to WhatsApp messages and the benefits of doing so.

**WHAT IS CHATGPT ?**
ChatGPT is a state-of-the-art language model developed by OpenAI that has been trained on a large corpus of text data. It is capable of understanding natural language and can generate responses that are coherent and relevant to the conversation. 

To use ChatGPT to reply to WhatsApp messages, you will need to integrate it with a messaging app.

There are different ways to integrate ChatGPT with WhatsApp, but one common method is to use a chatbot platform like Twilio or Dialogflow.
These platforms allow you to create a bot that can receive messages from WhatsApp and respond to them using ChatGPT, But neither are free and allow 100% customisation.


**BUILDING THE BOT**
We are gonna be using **[Venom](https://orkestral.io/)** to handle the request form Whatsapp, Shout out to the team of maintainer keeping this project alive and free for anyone to use.

To create a bot, you will need to follow these steps:

- Sign up to [OpenAI](https://openai.com/).
- Install Venom-bot on your pc `npm i venom-bot`
- Integrate ChatGPT with the bot by using its API.
- Connect the bot to WhatsApp and reply using incoming messages as it prompt.


**Step 1**

Get your private key from your private setting [here](https://platform.openai.com/account/api-keys)


![Open AI api keys page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt62566pzz9myk56cfh9.png)


**Step 2**

Create a dir and install venom-bot and openai packages 

`$~ mkdir my-bot && cd my-bot
$~ npm init -y 
$~ npm i venom-bot openai dotenv`

Once the installation is completed we open our IDE and 
create a file to help get the response from chatGPT

```javascript
// File name chatGPT.js
const { Configuration, OpenAIApi } = require("openai");
require('dotenv').config()


const configuration = new Configuration({
  apiKey: process.env.CHAT_GPT_KEY,
});
const openai = new OpenAIApi(configuration);




 exports.response = async (prompt) =>{
  return await openai.createCompletion({
        model: "text-davinci-003",
        prompt: prompt,
        temperature: 0.5,
        max_tokens: 60,
        top_p: 1,
        frequency_penalty: 0.5,
        presence_penalty: 0.28,
        stop: ["You:"],
      })
    }
```

Now any call to `response ()` return a string response from chatgpt 

Then Our main file which contain the Whatsapp bot and the integration 
 

```javascript
//main.js
const venom = require('venom-bot');
const { response } = require("./chatgpt");

venom
  .create({
    session: 'session-name', //name of session
    multidevice: true // for version not multidevice use false.(default: true)
  })
  .then((client) => start(client))
  .catch((erro) => {
    console.log(erro);
  });

function start(client) {
  client.onMessage((message) => {
// This ignore messages from group and story 
    if (message.body && message.isGroupMsg === false && message.from !== 'status@broadcast') {
        response(message.body).then(data => {
            console.log(data.data.choices[0]);
            client
            .sendText(message.from, data.data.choices[0].text)
            .then((result) => {
              console.log('Result: ', result); //return object success
            })
            .catch((erro) => {
              console.error('Error when sending: ', erro); //return object error
            });
          }).catch(err =>{
            console.log(err);
          })
    }
  });
}

```

Once you have set up the bot run, 

```
$~ node main
```
it will be able to reply to WhatsApp messages using ChatGPT. The bot can be customized to respond to specific types of messages or questions, depending on your needs.

![Venom Running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgyr72kfywwmqk9rs5wm.png) 


**Screenshot Of the Bot Replies**


![Rply 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du1lbh10tf9951b8d3ct.png)

> Not Sure I could rizz someone as chatGPT does
 

![rep 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iq384dlzyddp3alav2ub.png)

> I was laughing when I saw this

Using ChatGPT to reply to WhatsApp messages has several benefits. Firstly, it can save time and effort by automating responses to common questions or inquiries. This can be particularly useful for businesses that receive a large number of messages from customers or clients. Secondly, it can improve the quality of responses by generating coherent and relevant replies that are tailored to the context of the conversation. Finally, it can provide a more seamless and interactive experience for users by allowing them to receive instant responses without the need for human intervention.

In conclusion, ChatGPT can be a powerful tool for replying to WhatsApp messages. By integrating it with a chatbot platform, you can create a bot that can understand natural language and generate responses that are coherent and relevant to the conversation. This can save time, improve the quality of responses, and provide a more seamless and interactive experience for users.