Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsonaraujo committed Mar 11, 2023
1 parent d58ed95 commit 6d1df3d
Show file tree
Hide file tree
Showing 333 changed files with 4,326 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .env-exemple
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SERVER_PORT=3000
SESSION_NAME=TI-ZAP
WEBHOOK=http://127.0.0.1/webhook


#DATABASE CONFIGS
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=senha
DB_DATABASE=wa_venom


4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/.pnpm/[email protected]/node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
# wa-autovenom
Venomjs com Express
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
36 changes: 36 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import express, { Router, Request, Response } from 'express';

const router: Router = express.Router();

// Rota da API


/**
* @swagger
* /api/hello:
* post:
* description: Cria um novo usuário
* parameters:
* - name: username
* description: Nome do usuário
* in: formData
* required: true
* type: string
* - name: email
* description: E-mail do usuário
* in: formData
* required: true
* type: string
* responses:
* 200:
* description: Usuário criado com sucesso
*/
router.get('/api/hello', (req: Request, res: Response) => {

const name: string = req.params.name;
res.json({ message: `Olá, ${name}!` });
});



export default router;
59 changes: 59 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import express, { Application, NextFunction, Response, Request } from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import apiRoutes from './api/index';
import webRoutes from './web/routes/index';
import dotenv from 'dotenv';
import swaggerUi from 'swagger-ui-express';
import { db } from './configs/database';
import { whatsapp } from './configs/venombot';
import swaggerSpec from './configs/swaggerDef';

dotenv.config();

const PORT = process.env.SERVER_PORT;


//GLOBAIS

const intialize = async () => {
global.$db = db;
// global.$whatsapp = await whatsapp();

const app: Application = express();

// Configurações do aplicativo
app.set('views', path.join(__dirname, 'web/views'));
app.set('view engine', 'ejs');

// Middleware
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// Rotas da API
app.use('/api', apiRoutes);
// Middleware para servir arquivos estáticos
app.use(express.static(path.join(__dirname, 'public')));
// Rotas da aplicação web
app.use('/', webRoutes);



// Tratamento de erro
// Middleware para tratar erros
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Inicializa o servidor
const server = app.listen(PORT ? PORT : 3000, () => {
//@ts-ignore
console.log(`Servidor iniciado na porta ${server.address()?.port}`);
});

}
intialize();


18 changes: 18 additions & 0 deletions configs/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import knex from 'knex';

const HOST = <string>(process.env.DB_HOST ? process.env.DB_HOST : '127.0.0.1');
const PORT = <number>(process.env.DB_PORT ? process.env.DB_PORT : 3306);
const USER = <string>(process.env.DB_USER ? process.env.DB_USER : 'root');
const PASSWORD = <string>(process.env.DB_PASSWORD ? process.env.DB_PASSWORD : '');
const DATABASE = <string>(process.env.DB_DATABASE ? process.env.DB_DATABASE : 'database');

export const db = knex({
client: 'mysql',
connection: {
host: HOST,
port: PORT,
user: USER,
password: PASSWORD,
database: DATABASE
}
});
19 changes: 19 additions & 0 deletions configs/swaggerDef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import swaggerJSDoc from 'swagger-jsdoc';

const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'API WA-AUTOVENOM',
version: '1.0.0',
description: 'API de utilização da automatização de whatsapp.',
},
};

const options = {
swaggerDefinition,
apis: ['./api/*.ts'],
};

const swaggerSpec = swaggerJSDoc(options);

export default swaggerSpec;
40 changes: 40 additions & 0 deletions configs/venombot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Supports ES6
import { create, Whatsapp } from 'venom-bot';
import axios from 'axios';

export const whatsapp = (): Promise<Whatsapp> => new Promise((resolve, reject) => {

const SESSION_NAME = process.env.SESSION_NAME ? process.env.SESSION_NAME : "NO-NAME";
const WEBHOOK = process.env.WEBHOOK ? process.env.WEBHOOK : "127.0.0.1/wa-me";
const WEBHOOK_QRCODE = process.env.WEBHOOK_QRCODE ? process.env.WEBHOOK_QRCODE : "127.0.0.1/wa-me/qrcode";

create({
session: SESSION_NAME, //name of session
logQR: true,
})
.then(async (client) => {

await client.waitForQrCodeScan((qrcode) => {
axios.post(WEBHOOK_QRCODE).then(res => { }).catch((err) => {console.error(err) })
})

client.onMessage((msg) => {
console.log(msg);
axios.post(WEBHOOK, msg).then(res => {

}).catch(err => {
console.error(err);
});
})

resolve(client);
})
.catch((erro) => {
reject(erro)
console.log(erro);
});


})


5 changes: 5 additions & 0 deletions controllers/exemploController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Request, Response } from 'express';

export const index = (req: Request, res: Response) => {
res.render('index');
};
21 changes: 21 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Knex } from "knex";
import { Whatsapp } from "venom-bot";

declare global {

var $db: Knex;
var $whatsapp: Whatsapp | Promise<Whatsapp>;

enum TB {
TB_TABELA = "TB_TABELA"
}

interface Array<T> {
customFilter(callback: (value: T, index: number, array: T[]) => boolean): T[];
}

interface String {
capitalize(): string;
}
}

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("sucrase/register");
require('./app');
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "wa-autovenom",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"nodemonConfig": {
"ignore": [
"**/test/**",
"**/docs/**",
"**/swagger/**"
],
"ext": ".ts,js,json,ejs,env",
"delay": 2500
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.17",
"@types/knex": "^0.16.1",
"@types/node": "^18.15.0",
"nodemon": "^2.0.21",
"typescript": "^4.9.5"
},
"dependencies": {
"@types/swagger-ui-express": "^4.1.3",
"axios": "^1.3.4",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"ejs": "^3.1.8",
"express": "^4.18.2",
"knex": "^2.4.2",
"mysql": "^2.18.1",
"mysql2": "^3.2.0",
"sucrase": "^3.29.0",
"swagger-autogen": "^2.23.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^4.6.2",
"ts-node": "^10.9.1",
"venom-bot": "^4.3.7"
}
}
Loading

0 comments on commit 6d1df3d

Please sign in to comment.