package.
json (não copiar essa linha):
{
"name": "integrando-chatgpt-e-whatsapp",
"version": "1.0.0",
"description": "Um exemplo de como integrar ChatGPT e
WhatsApp.",
"main": "[Link]",
"scripts": {
"start": "node [Link]"
},
"dependencies": {
"axios": "^1.4.0",
"body-parser": "^1.20.2",
"chatgpt": "^5.2.4",
"express": "^4.18.2",
"openai": "^3.2.1",
"request": "^2.88.2"
},
"engines": {
"node": "16.x"
}
}
[Link] (não copiar essa linha):
"use strict";
const token = [Link].GRAPH_API_TOKEN;
const fs = require('fs').promises;
const express = require("express"),
body_parser = require("body-parser"),
https = require("https"),
app = express().use(body_parser.json());
const { Configuration, OpenAIApi } = require("openai");
const api = new OpenAIApi(new Configuration({ apiKey:
[Link].OPENAI_API_KEY }));
let lastRespondedMessageId = "";
[Link]([Link] || 1337, () => [Link]("webhook is
listening"));
[Link]("/webhook", async function (req, res) {
if ([Link]) {
let message = [Link]?.[0]?.changes?.
[0]?.value?.messages?.[0];
if (message && [Link] && [Link] &&
[Link] !== lastRespondedMessageId) {
lastRespondedMessageId = [Link];
let phone_number_id =
[Link][0].changes[0].[Link].phone_number_id;
let from = [Link];
let msg_body = [Link];
envia_chat(phone_number_id, token, from, msg_body, res);
}
[Link](200); // Confirmação de recebimento para o
webhook
} else {
[Link](404); // Resposta para requisição malformada
}
});
[Link]("/webhook", (req, res) => {
const verify_token = [Link].WEBHOOK_VERIFY_TOKEN;
let mode = [Link]["[Link]"];
let token = [Link]["hub.verify_token"];
let challenge = [Link]["[Link]"];
if (mode && token) {
if (mode === "subscribe" && token === verify_token) {
[Link]("WEBHOOK_VERIFIED");
[Link](200).send(challenge);
} else {
[Link](403);
}
}
});
const sendReply = (phone_number_id, whatsapp_token, to,
reply_message, resp) => {
let json = {
messaging_product: "whatsapp",
to: to,
text: { body: reply_message },
};
let data = [Link](json);
let path = "/v19.0/"+phone_number_id+"/messages?
access_token="+whatsapp_token;
let options = {
host: "[Link]",
path: path,
method: "POST",
headers: { "Content-Type": "application/json" }
};
let req = [Link](options, (response) => {});
[Link]("error", (e) => { [Link]("Erro ao enviar resposta: ",
e); });
[Link](data);
[Link]();
}
async function envia_chat(phone_number_id, token, from, msg_body,
res) {
try {
const databaseContent = await [Link]('[Link]', 'utf8');
const completion = await [Link]({
model: "gpt-3.5-turbo",
messages: [
{"role": "system", "content": "Você é Omni, uma IA criada por
Bruno Bracaioli para atender os clientes. Você é muito educado e
persuasivo. Seu trabalho é tirar dúvidas sobre o MasterMind IA
(treinamento criado por Bruno Bracaioli). Aqui estão algumas
informações úteis: " + databaseContent},
{ role: "user", content: msg_body }
],
max_tokens: 1000,
temperature: 0.9,
top_p: 0.5,
frequency_penalty: 0.25,
presence_penalty: 0.25,
});
const reply = [Link][0].[Link]();
if ([Link] > 0) {
sendReply(phone_number_id, token, from, reply, res);
} else {
[Link]("Resposta gerada está vazia.");
}
} catch (e) {
[Link]("Erro durante a geração da resposta: ", e);
}
}