fhy-wabot
v1.0.11
Published
fhy-wabot is a Node.js package designed to make using WhatsApp Bot easier.
Downloads
43
Maintainers
Readme
Documentation
fhy-wabot is a Node.js package designed to simplify the use of WhatsApp Bots.
Installation
To install the fhy-wabot
package, run the following command in the terminal:
npm install fhy-wabot
Usage
After installing the package, you can use it in your Node.js project as follows (server.js):
Import Package
First, import WaBot
from fhy-wabot
:
const { WaBot } = require('fhy-wabot');
Initialize and Run the Bot
Next, call the WaBot()
function to initialize and run the bot
Initialization has fixed rules:
WaBot(QRUrl = false, QRCustom, AutoResponse, ManualResponse, self = false);`
QRUrl
: to set the use of QR Url (set:true
orfalse
)QRCustom
: to create a custom QR url function (if you don't want to use it set to:null
)AutoResponse
: to Initialize the function ofAutoResponse
(if you don't want to use it set to:null
ManualResponse
: to Initialize the function ofManualResponse
(if you don't want to use it set to:null
self
: to set the use of self mode (set:true
orfalse
)
Basic usage example that just uses the defaults:
WaBot(QRUrl = false, null, null, null, self = false);
You can use another method
WaBot(false, null, null, null, false);
or
WaBot(true, null, null, null, true);
Usage Example
Here’s a complete example of how to use fhy-wabot
:
const { WaBot } = require('fhy-wabot');
// Default Initialize and run the bot
WaBot(false, null, null, null, false);
QRCode Url
You can make the QRcode a data URL with the following command:
const { WaBot } = require('fhy-wabot');
const QRCode = require('qrcode');
const QRCustom = async (qr) => {
try {
const url = await QRCode.toDataURL(qr);
console.log('Custom QRCode URL:', url);
} catch (err) {
console.error('Failed to generate QR URL:', err);
}
};
WaBot(true, QRCustom, null, null, false);
Self Mode
WaBot(false, null, null, null, true);
Sending Automatic Response Messages
AutoResponse
is an object used to configure automatic responses that will be given by the bot based on the received input. (supports multi-message).
const { WaBot } = require('fhy-wabot');
const AutoResponse = {
'text': {
'!text': {
response: 'pong!',
reply: false
}
// You can add other data here
},
// You can add other data types here
};
WaBot(false, null, AutoResponse, null, false);
- response: The message that will be sent (e.g., 'pong!').
- reply: If set to
true
, the bot will reply to the message with a quote.
Text
'text': {
'!text': {
response: 'I sent text!',
reply: false
}
},
Text With Reaction
'textReact': {
'!text-react': {
response: 'I sent text with reaction!',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
},
- preReact: Emoji displayed before sending the message (e.g., '⏳').
- postReact: Emoji displayed after the message is sent (e.g., '✅').
- errReact: Emoji displayed if an error occurs (e.g., '❌').
Mention
'mention': {
'!mention': {
response: '@628xxxxxxxx',
mention: '[email protected]',
reply: false
}
},
- mention: The ID number of the user to mention.
Mention With Reaction
'mention': {
'!mention-react': {
response: '@628xxxxxxxx',
mention: '[email protected]',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
},
Location
'location': {
'!location': {
latitude: 24.121231,
longitude: 55.1121221,
reply: false
}
},
- latitude: The latitude of the location to be shared.
- longitude: The longitude of the location to be shared.
Location With Reaction
'locationReact': {
'!location-react': {
latitude: '24.121231',
longitude: '55.1121221',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
},
Images
'images': {
'!image': {
url: './uploads/default.jpg',
caption: 'I sent caption image!',
reply: false
}
},
- url: Image location
- caption: Image caption
Images With Reaction
'imagesReact': {
'!image-react': {
url: './uploads/default.jpg',
caption: 'I sent caption image with reaction!',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
},
Video
'video': {
'!video': {
url: './uploads/default.mp4',
caption: 'I sent caption video!',
reply: false
},
},
Video With Reaction
'videoReact': {
'!video-react': {
url: './uploads/default.mp4',
caption: 'I sent caption video with reaction!',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
},
},
Audio
'audio': {
'!audio': {
url: './uploads/default.mp3',
caption: 'I sent caption audio!',
reply: false
},
},
Audio With Reaction
'audioReact': {
'!audio-react': {
url: './uploads/default.mp3',
caption: 'I sent caption audio with caption!',
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: true
},
},
Polling
'polling': {
'!poll': {
poll: {
name: 'Title Polling:',
values: ['Option 1', 'Option 2', 'Option 3'],
selectableCount: 1
},
reply: false
}
},
- name: Poll Title
- values: Fill in the poll options
- selectableCount:
1
for just one option,0
for multiple options
Polling With Reaction
'pollingReact': {
'!poll-react': {
poll: {
name: 'Title Polling:',
values: ['Option 1', 'Option 2', 'Option 3'],
selectableCount: 1
},
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
}
VCard
'vcard': {
'!vcard': {
contacts: {
fullName: 'Your Name',
organization: 'Organization Name',
phoneId: '+62 8xx xxxx xxxx',
whatsappId: '628xxxxxxxxxx'
},
reply: false
}
},
- fullName: Contact Name
- organization: Organization Name
- phoneId: Phone number
- whatsappId: Whatsapp number
VCard With Reaction
'vcardReact': {
'!vcard-react': {
contacts: {
fullName: 'Your Name',
organization: 'Organization Name',
phoneId: '+62 8xx xxxx xxxx',
whatsappId: '628xxxxxxxxxx'
},
preReact: '⏳',
postReact: '✅',
errReact: '❌',
reply: false
}
},
Sending Messages
ManualResponse
is an object used to configure various types of messages you want to send (supports multi-message).
const { WaBot } = require('fhy-wabot');
const ManualResponse = {
text: [
{
id: '[email protected]',
messageText: 'I sent text!'
}
],
};
WaBot(false, null, null, ManualResponse, false);
Text
text: [
{
id: '[email protected]',
messageText: 'I sent text!'
}
],
- id: Message recipient number
- messageText: Message content
Images
images: [
{
id: '[email protected]',
url: './uploads/default.jpg',
caption: 'I sent image!'
}
],
- url: Image location
- caption: Image caption
Video
video: [
{
id: '[email protected]',
url: './uploads/default.mp4',
caption: 'I sent video!'
}
],
Audio
audio: [
{
id: '[email protected]',
url: './uploads/default.mp3',
caption: 'I sent audio!'
}
],
Mention
mention: [
{
id: '[email protected]',
messageText: '@6281xxxxxxxxx',
mention: '[email protected]'
}
],
- mention: ID number to mention
Location
location: [
{
id: '[email protected]',
latitude: 24.121231,
longitude: 55.1121221
}
],
- latitude: The latitude of the location to be shared.
- longitude: The longitude of the location to be shared.
Polling
polling: [
{
id: '[email protected]',
name: 'Title Polling:',
values: ['Option 1', 'Option 2', 'Option 3'],
selectableCount: 1
}
],
- name: Poll Title
- values: Fill in the poll options
- selectableCount:
1
for just one option,0
for multiple options
VCard
vcard: [
{
id: '[email protected]',
fullName: 'Your Name',
organization: 'Organization Name',
phoneId: '+62 8xx xxxx xxxx',
whatsappId: '628xxxxxxxxxx'
}
],
- fullName: Contact Name
- organization: Organization Name
- phoneId: Phone number
- whatsappId: Whatsapp number
Complete Usage Example
const { WaBot } = require('fhy-wabot');
const QRCode = require('qrcode');
const QRCustom = async (qr) => {
try {
const url = await QRCode.toDataURL(qr);
console.log('Custom QRCode URL:', url);
} catch (err) {
console.error('Failed to generate QR URL:', err);
}
};
const AutoResponse = {
'text': {
'!text': {
response: 'pong!',
reply: false
}
// You can add other data here
},
// You can add other data types here
};
const ManualResponse = {
text: [
{
id: '[email protected]',
messageText: 'I sent text!'
}
// You can add other data here
],
// You can add other data types here
};
WaBot(true, QRCustom, AutoResponse, ManualResponse, false);
Advanced Usage Example
const { WaBot } = require('fhy-wabot');
const AutoResponse = {};
const ManualResponse = {};
(async () => {
const sock = await WaBot(false, null, AutoResponse, ManualResponse, false);
sock.ev.on('messages.upsert', async (messageUpdate) => {
const message = messageUpdate.messages[0];
const sender = message.key.remoteJid;
const text = message.message.conversation || message.message.extendedTextMessage?.text || '';
if (text.toLowerCase() === '!hi') {
try {
await sock.sendMessage(sender, { text: 'oh hello there' });
} catch (error) {
console.error(`Error occurred: ${error.message}`);
}
}
});
})();
Download Media Message
To download media you need downloadMediaMessage
, fs
, and path
const { WaBot, downloadMediaMessage } = require('fhy-wabot');
const fs = require('fs');
const path = require('path');
Usage example for downloading media when quoting a media message with the .download
command:
if (text.toLowerCase() === '.download') {
const quotedMessage = message.message.extendedTextMessage?.contextInfo?.quotedMessage;
if (quotedMessage?.imageMessage) {
try {
const buffer = await downloadMediaMessage({ message: quotedMessage }, 'buffer');
const inputFilePath = path.join(__dirname, './download.jpg');
fs.writeFileSync(inputFilePath, buffer);
console.log(`Image downloaded: ${inputFilePath}`);
} catch (error) {
console.error(`Error occurred: ${error.message}`);
}
} else {
console.log("Quoted message is not an image.");
}
}
Advanced Full Usage Example
const { WaBot } = require('fhy-wabot');
const QRCode = require('qrcode');
const QRCustom = async (qr) => {
try {
const url = await QRCode.toDataURL(qr);
console.log('Custom QRCode URL:', url);
} catch (err) {
console.error('Failed to generate QR URL:', err);
}
};
const AutoResponse = {
'text': {
'!text': {
response: 'pong!',
reply: false
}
// You can add other data here
},
// You can add other data types here
};
const ManualResponse = {
text: [
{
id: '[email protected]',
messageText: 'I sent text!'
}
// You can add other data here
],
// You can add other data types here
};
(async () => {
const sock = await WaBot(true, QRCustom, AutoResponse, ManualResponse, false);
sock.ev.on('messages.upsert', async (messageUpdate) => {
const message = messageUpdate.messages[0];
const sender = message.key.remoteJid;
const text = message.message.conversation || message.message.extendedTextMessage?.text || '';
if (text.toLowerCase() === '.test') {
try {
await sock.sendMessage(sender, { text: 'oh hello there' });
} catch (error) {
console.error(`Error occurred: ${error.message}`);
}
}
// You can add other advanced data here
});
})();
Send Message With Endpoint
const express = require('express');
const { WaBot } = require('fhy-wabot');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.post('/send-message', async (req, res) => {
const { id, messageText } = req.body;
if (!id || !messageText) {
return res.status(400).json({ error: 'ID and messageText are required.' });
}
const ManualResponse = {
text: [{ id: `${id}@s.whatsapp.net`, messageText }]
};
try {
await WaBot(false, null, null, ManualResponse, false);
res.json({ message: 'Message sent successfully.', recipient: `${id}@s.whatsapp.net` });
} catch (error) {
console.error('Failed to send message:', error);
res.status(500).json({ error: 'Failed to send message.' });
}
});
- npm install express
- Endpoint:
http://localhost:3000/send-message
- Body Request:
{ "id": "628xxxxxxxxxx", "messageText": "Hello from Endpoint!" }
Support and Contributions
If you encounter any issues or have questions regarding the use of this package, please open an issue in your GitHub repository. Contributions are also very welcome to improve the features and functionality of fhy-wabot
.