Skip to content

Commit

Permalink
feat-and-refine: Log & Docs
Browse files Browse the repository at this point in the history
Added feature : Logging, new command .log

Refined Documentation
  • Loading branch information
gensart-ai committed Feb 25, 2024
1 parent 4ef8aee commit 70ffc8b
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 109 deletions.
78 changes: 10 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,23 @@

<p align="center">
<img width="400" height="400" src="https://i.ibb.co/7y1v1q4/hour.png">
<img width="300" height="300" src="https://i.ibb.co/7y1v1q4/hour.png">
</p>

# WhatsApp Bot by Node.js + whatsapp-web.js
> Since this project still on development 🚧, I do not have any briliant names for it. Maybe soon, stay tuned !
> This project still on development 🚧, so it might have a massive changes, stay tuned !
Documentation Page : [Click Here ▶](https://gensart-ai.github.io/whatsapp-bot)
#### Just an automated WhatsApp reply system based on text sent that built under Node.js, with help of a library called [whatsapp-web.js](https://wwebjs.dev/).
#### WhatsApp bot powered by Node.js, with several daily commands that might be useful for you !

[![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.png)](https://nodejs.org)
[![forthebadge](https://forthebadge.com/images/badges/powered-by-electricity.png)](https://web.pln.co.id/tentang-kami/profil-perusahaan)
[![forthebadge](http://forthebadge.com/images/badges/built-with-love.svg)](http://forthebadge.com)
# Technology Used
<a href='https://nodejs.org' target="_blank"><img alt='node.js' src='https://img.shields.io/badge/Node.js-100000?style=for-the-badge&logo=node.js&logoColor=white&labelColor=60AA50&color=447C42'/></a> <a href='https://www.typescriptlang.org/' target="_blank"><img alt='typescript' src='https://img.shields.io/badge/Typescript-100000?style=for-the-badge&logo=typescript&logoColor=white&labelColor=5094DD&color=5094DD'/></a>


# Table of contents
- [Requirements](#requirements)
- [Installation](#installation)
- [Configurations](#configurations)
- [Quickstart](#quickstart)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)

# Requirements
[(Back to top)](#table-of-contents)
This project requires **Node.js** to run, I use **Node v20.10** on Windows 10 and **Debian 11** too and it works fine. I haven't tested it yet on different version or environment than mine, but you may try it and report it to me, so I can update this details by you :).

Oh also, make sure you have installed **NPM** too. Usually **Node.js** includes **NPM** so you don't need to install it again.
# Technology Used <!-- {docsify-ignore} -->
<a href='https://nodejs.org' target="_blank"><img alt='node.js' src='https://img.shields.io/badge/Node.js-100000?style=for-the-badge&logo=node.js&logoColor=white&labelColor=60AA50&color=447C42'/></a> <a href='https://www.typescriptlang.org/' target="_blank"><img alt='typescript' src='https://img.shields.io/badge/Typescript-100000?style=for-the-badge&logo=typescript&logoColor=white&labelColor=5094DD&color=5094DD'/></a>

**🔴 IMPORTANT NOTICE 🔴**
This project greatly having use of [whatsapp-web.js](https://wwebjs.dev/), and maybe **if you are running on a `no-gui` Linux image (Server, or Linux OS that do not installed GUI system)**, you may need to see and follow this page first : [Click Me](https://wwebjs.dev/guide/#installation-on-no-gui-systems)
# Overview
!> This application uses `whatsapp-web.js` as a primary library. Keep in mind if you want to use this app, you did already know that **WhatsApp terms & conditions do not allow any bots or automation** on its services. So, I do not responsible for any banning happen to your WhatsApp account by using this app. <b>I strongly suggested</b> you to use this app with your WhatsApp backup number. I use my second number for this and still safe until now.

# Installation
[(Back to top)](#table-of-contents)
First thing you do, clone this repo by using :
```bash
git clone https://github.com/gensart-ai/whatsapp-bot.git
```
Do not know how to clone in GitHub ? maybe you can check this page for a quickstart : [Click Me!](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository)
Current Stable Version : `1.0.1`
Look at the sidebar for more docs.
(Can't see the sidebar on mobile ? The button is on your bottom left screen :))

After that, go to the repo root directory and do the command below to install the packages needed for the project :
```bash
npm i
```
And it's done.
# Configurations
[(Back to top)](#table-of-contents)
As the current version, I put some global configurations on a Typescript file in : `./src/env.ts`. You can change the bot name, imgBb keys, etc.

For `imgBBKey`, you can retrieve the key from [here](https://api.imgbb.com/), but you may need to sign up first if you didn't have any ImgBB account before.

# Quickstart
[(Back to top)](#table-of-contents)
If you have done the [Installation](#installation) and have checked the [Configurations](#configurations), you can open the terminal at root directory of the project and do the command below to build the `src` to JavaScript to `dist`:
```bash
npm run build
```
After that, you can run the project using this command :
```bash
npm run serve
```
If you succeed, you should see some message like this :
![WhatsApp QR Code shown on terminal](https://i.ibb.co/K5rzdxp/readme-quickstart-1.png)
The terminal will shows a QR Code to be scanned with your WhatsApp app (doing it like scanning for WhatsApp Web). And if project succeed the QR, it will output :
`System is online!`
And congrats, your WhatsApp number (that scan the QR) is successfully integrated with the project !🎉
# Usage
[(Back to top)](#table-of-contents)
As of current version, you can send a message : `.help` to an associated WhatsApp number with the project, you can see the detail of features on there ! :)

# Contributing
[(Back to top)](#table-of-contents)
Your contributions are always welcome! Please have a look at the [contribution guidelines](CONTRIBUTING.md). :tada:

# License
[(Back to top)](#table-of-contents)
The MIT License (MIT) 2017 - [Genesaret Johnes](https://github.com/gensart-ai/). Please have a look at the [license](LICENSE.md) for more details.
2 changes: 2 additions & 0 deletions docs/CONFIGURATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

> 🚧 Do note that this project is still on development, it **might** have impact changes, including this configurations method.
!> Also note, that I think the current configuration method is considered **NOT SAFE**. Why ? because it still on development, and I need to record the changes to version control, and the environment data will be recorded too. *"Then why you use this ?"*, hehe, I build this while learning Node and TS environment too, so, maybe soon if I have any idea to secure it more safe, I'll do it. Though, you may provide some feedback / suggestion for this, I may be appreciate that 💖.

As of current version, I put some global configurations needed for the project on a Typescript file in : `./src/env.ts`.
You can do several things below :

Expand Down
2 changes: 1 addition & 1 deletion docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ After that, go to the repo root directory on your system and do the command belo
npm i
```

And you're done ! Next is for [configuration](./CONFIGURATIONS.md).
And you're done ! Next is for [pre-configuration](./CONFIGURATIONS.md).
4 changes: 2 additions & 2 deletions docs/QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ This project made in Typescript. If you have done the [Installation](./INSTALLAT
```terminal
npm run build
```
After that, you can run the project using this command (this command executes the compiled main point file in `dist/index.js`) :
After that, you can run the project using this command (this command executes the compiled main point file in `dist/index.js`) on the terminal :
```terminal
npm run serve
```
If you succeed, you should see some message like this :
If you succeed, you should see some message like this in your terminal :
![WhatsApp QR Code shown on terminal](https://i.ibb.co/K5rzdxp/readme-quickstart-1.png)
The terminal will shows a QR Code to be scanned with your WhatsApp app (doing it like scanning for WhatsApp Web). And if project succeed the QR, it will output :
```terminal
Expand Down
17 changes: 6 additions & 11 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<img width="300" height="300" src="https://i.ibb.co/7y1v1q4/hour.png">

> This project still on development 🚧, so it might have a massive changes, stay tuned !
# whatsapp-bot
#### WhatsApp bot powered by Node.js, with several daily commands that might be useful for you !

[![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.png)](https://nodejs.org)
[![forthebadge](https://forthebadge.com/images/badges/powered-by-electricity.png)](https://web.pln.co.id/tentang-kami/profil-perusahaan)
[![forthebadge](http://forthebadge.com/images/badges/built-with-love.svg)](http://forthebadge.com)

# Technology Used <!-- {docsify-ignore} -->
<a href='https://nodejs.org' target="_blank"><img alt='node.js' src='https://img.shields.io/badge/Node.js-100000?style=for-the-badge&logo=node.js&logoColor=white&labelColor=60AA50&color=447C42'/></a> <a href='https://www.typescriptlang.org/' target="_blank"><img alt='typescript' src='https://img.shields.io/badge/Typescript-100000?style=for-the-badge&logo=typescript&logoColor=white&labelColor=5094DD&color=5094DD'/></a>

!> This application uses `whatsapp-web.js` as a primary library. Keep in mind if you want to use this app, you did already know that **WhatsApp terms & conditions do not allow any bots or automation** on its services. Make sure you use this app with your WhatsApp backup number to prevent banning to your primary WhatsApp account. I use my second number for this and still safe until now.
# Overview
!> This application uses `whatsapp-web.js` as a primary library. Keep in mind if you want to use this app, you did already know that **WhatsApp terms & conditions do not allow any bots or automation** on its services. So, I do not responsible for any banning happen to your WhatsApp account by using this app. <b>I strongly suggested</b> you to use this app with your WhatsApp backup number. I use my second number for this and still safe until now.

Current Stable Version : `1.0`
Current Stable Version : `1.0.1`
Look at the sidebar for more docs.
(Can't see the sidebar on mobile ? The button is on your bottom left screen :))

And it's done.

# Contributing
[(Back to top)](#table-of-contents)
Your contributions are always welcome! Please have a look at the [contribution guidelines](CONTRIBUTING.md). :tada:

# License
[(Back to top)](#table-of-contents)
The MIT License (MIT) 2017 - [Genesaret Johnes](https://github.com/gensart-ai/). Please have a look at the [license](LICENSE.md) for more details.
5 changes: 4 additions & 1 deletion docs/REQUIREMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
You need :
- Node.js v18++
- NPM installed on your device
- [ffmpeg](https://ffmpeg.org/) (Windows users may need this) - For multimedia support

## Long to short <!-- {docsify-ignore} -->
This project requires **Node.js** to run, I use **Node v20.10** on Windows 10 and **Debian 11** too and it works fine. I haven't tested it yet on different version or environment than mine, but you may try it and report it to me, so I can update this details by you :).
You can use this [link](https://nodejs.org/en) to go to Node.js homepage for installation or more details.

Oh also, make sure you have installed **NPM** too. Usually **Node.js** includes **NPM** so you don't need to install it again.
Oh also, make sure you have installed **NPM** too. Usually **Node.js** includes **NPM** so you don't need to install it again.

And i often experience errors when testing multimedia with this app on Windows 10 environment, but not on Debian 11. My assumption going to **ffmpeg** problem, maybe you can install that if you experience an error when dealing with multimedia thing.

## **🔴 IMPORTANT NOTICE 🔴** <!-- {docsify-ignore} -->
This project greatly having use of [whatsapp-web.js](https://wwebjs.dev/), and maybe **if you are running on a `no-gui` Linux image (Server, or Linux OS that did not installed GUI system)**, you may need to see and follow this page first : [Click Me](https://wwebjs.dev/guide/#installation-on-no-gui-systems)
29 changes: 28 additions & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@ After the project is installed and configured, you can start the bot by running

As of current version, I have built several built-in features in it too, some are these :

> Come Soon
```
*== Quotes ==*
.quotes
*== AI Feature ==*
.gemini - *TEMPORARY DISABLED* -
*== Translate ==*
.indotoeng [text indo] - Translate Indonesia to English
.engtoindo [text English] - Translate English to Indonesia
*== Media(Image/Short Video) to Sticker ==*
.s (send with the media with command as the caption)
.st (send with the media with command as the caption)
*== TextPro (Create styled-text in an image, add !sticker after the command to create as sticker) ==*
.neon
.lunar
.thunder
.shadow
.snow
.winter
.frozen
.artistic-typography
.gradient-neon
.blackpink
.sliced-effect
.red-batman
.neon-valentine
.neon-cube
.blackpink-logo
```
3 changes: 1 addition & 2 deletions docs/_navbar.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* Ever heard of me ?
* You must be mistaken :).
* [SoraErlyana Repo GitHub](https://github.com/gensart-ai/whatsapp-bot)
21 changes: 13 additions & 8 deletions src/command-hive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Client, Message } from 'whatsapp-web.js'
import { getForismaticQuotes } from 'services/external/quote-v1'
import { textProSingleTextRouter } from 'services/external/textpro'
import commandGuide from 'services/internal/command-guide'
import imageToSticker from 'services/internal/image-to-sticker'
import { translateEnglishToIndo, translateIndoToEnglish } from './services/external/translate'
import { imageToStickerText } from './services/external/image-to-sticker-meme'
import { geminiTextOnly } from './services/external/gemini'
import { getForismaticQuotes } from '@services/external/quote-v1'
import { textProSingleTextRouter } from '@services/external/textpro'
import commandGuide from '@services/internal/command-guide'
import imageToSticker from '@services/internal/image-to-sticker'
import { translateEnglishToIndo, translateIndoToEnglish } from '@services/external/translate'
import { imageToStickerText } from '@services/external/image-to-sticker-meme'
import { geminiTextOnly } from '@services/external/gemini'
import { log } from '@services/internal/log'

type Commands = {
[key: string] : (client: Client, message: Message) => any
[key: string]: (client: Client, message: Message) => any
}
type Executor = (client: Client, message: Message) => void

const commands: Commands = {

// ! Administrative commands
'.log': log,

// * Help
'.help': commandGuide,

Expand Down
3 changes: 2 additions & 1 deletion src/command-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const routeCommand = async (client: Client, message: Message) => {
if (extractedCommand in commands) {
await commands[extractedCommand](client, message);
} else {
const contact = await message.getContact();
wweb.sendMessage(
client,
message.from,
`${config.botShortName} tidak mengerti apa yang anda sampaikan :(. Harap ketik \`.help\` untuk mengetahui yang ${config.botShortName} pahami :)`
`${config.botShortName} tidak mengerti. Harap ketik \`.help\` untuk mengetahui yang ${config.botShortName} pahami, ${contact?.pushname ?? ''}!`
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const environmentConfiguration: EnvironmentConfiguration = {
botCodeName: 'SoraErlyana',

imgBBKey: '8ceb73c518129ab2ca63c8cf9f7ea8a6',
googleAiStudioKey: 'AIzaSyBIMSDB4T7Gn7pfnTMLJHMp4qenjG0gN0Q'
googleAiStudioKey: '--'
}

export default environmentConfiguration
9 changes: 7 additions & 2 deletions src/services/external/image-to-sticker-meme.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from 'axios'
import { Contact, MessageMedia } from 'whatsapp-web.js'
import axios, { AxiosError } from 'axios'
import querystring from 'querystring'
import config from '@/env'
import * as wweb from '@utils/wweb'
import { Executor } from '@/command-hive'
import { Contact, MessageMedia } from 'whatsapp-web.js'
import * as logger from '@utils/logger'

type Meme = {
image: string,
Expand Down Expand Up @@ -154,6 +155,10 @@ const imageToStickerText: Executor = async (client, message) => {
wweb.replyMessage(message, `${config.botShortName} perlu gambar untuk dijadikan stikernya, ${contact.pushname ?? ''}`)
}
} catch (error) {
const contact = await message.getContact();
const err = error as AxiosError;
logger.logError('imageToStickerText - ' + err.message + ' by ' + contact?.pushname ?? 'unknown');

wweb.replyMessage(message, `${config.botShortName} gagal memproses gambar yang anda tujukan, mohon coba lagi dengan mengirim gambar baru.`)
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/services/external/quote-v1.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios, { AxiosError } from 'axios';
import { Executor } from '@/command-hive';
import * as wweb from '@utils/wweb';
import axios, { AxiosError } from 'axios'
import { Executor } from '@/command-hive'
import * as wweb from '@utils/wweb'
import * as translate from '@utils/translation'
import * as logger from '@utils/logger'

type Quote = {
quote: string,
Expand Down Expand Up @@ -31,7 +32,10 @@ const getForismaticQuotes: Executor = async (client, message) => {
}

} catch (e) {
const error = e as AxiosError | Error;
const contact = await message.getContact();
const err = e as AxiosError | Error;
logger.logError('quoteV1Forismatic - ' + err.message + ' by ' + contact?.pushname ?? 'unknown');

wweb.replyMessage(message, 'Gagal memuat quotes, silahkan coba lagi.')
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/services/external/textpro.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AxiosError } from 'axios';
import axiosInstance from 'utils/axios-instance';
import { warningNoTextMessage, createTextProImage, TextProImageObject } from '@utils/textpro';
import * as wweb from '@utils/wweb';
import { Client, Contact, Message, MessageMedia } from 'whatsapp-web.js'
import { AxiosError } from 'axios'
import axiosInstance from '@utils/axios-instance'
import { warningNoTextMessage, createTextProImage, TextProImageObject } from '@utils/textpro'
import { Executor } from '@/command-hive'
import * as wweb from '@utils/wweb'
import * as logger from '@utils/logger'

import { Executor } from '@/command-hive';
import { Client, Contact, Message, MessageMedia } from 'whatsapp-web.js';

/**
* Routes textpro style based on the command.
Expand Down Expand Up @@ -103,7 +104,11 @@ const textpro = async (client: Client, message: Message, url: string) => {
wweb.replyMessage(message, new MessageMedia(image.mimetype, image.image));
}

} catch (e) {
} catch (error) {
const contact = await message.getContact();
const err = error as AxiosError;
logger.logError('TextPro - ' + err.message + ' by ' + contact?.pushname ?? 'unknown');

wweb.replyMessage(message, 'Gagal memproses gambar, silahkan coba lagi');
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/services/internal/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Executor } from '@/command-hive'
import * as logger from '@utils/logger'
import * as wweb from '@utils/wweb'
import config from '@/env'

const log: Executor = async (client, message) => {
// Extract the commands
const text = message.body.split(' ');
const logType: string = text[1];
let logFile: string;

switch (logType) {
case 'error':
logFile = logger.ERROR_LOG_FILE;
default:
logFile = '';
}

const logs: string = logger.fetchLog(logFile);

if (logs != '') {
wweb.replyMessage(message, logs)
} else {
wweb.replyMessage(message, `${config.botShortName} tidak melihat ada log saat ini.`)
}
}

export {
log
}
Loading

0 comments on commit 70ffc8b

Please sign in to comment.