Skip to content

Commit

Permalink
Modified 'getdp' module to display tagged person's display image. (Pr…
Browse files Browse the repository at this point in the history
…ince-Mendiratta#72)

* Reincluded qr and decodeqr files.

Signed-off-by: Johann <johannjose@msn.com>

* Reformat code

Signed-off-by: Prince Mendiratta <prince.mendi@gmail.com>

Co-authored-by: Prince Mendiratta <prince.mendi@gmail.com>
  • Loading branch information
j0h4nn1410 and Prince-Mendiratta authored Dec 11, 2021
1 parent c9fcc6e commit 0e5e161
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ const data = {
OUTPUT_TEMPLATE: "⭐ *Output:*```\n{stdout}```\n\n⚠️ *Error:*```\n{stderr}```\n\n👨🏻‍💻 *Command:*```\n{code}```",
PROCESSING: "```Executing, please wait...```"
},
decodeqr: {
DESCRIPTION: "Decode QR code",
EXTENDED_DESCRIPTION: "```Use this plugin to decode a QR code by simply replying to an existing QR image in the chat using``` *.decodeqr* ```or uploading a QR image with caption as``` *.decodeqr*",
INVALID_REPLY: "```Please ensure that you are replying to a QR image/sticker.```",
INVALID_INPUT: "```Invalid input. Use``` *.help decodeqr* ```for more info.```",
PROCESSING: "```Decoding. Please wait...```"
},
demote: {
DESCRIPTION: "Demote a person from admin",
EXTENDED_DESCRIPTION:
Expand Down Expand Up @@ -179,6 +186,15 @@ const data = {
MESSAGE_NOT_TAGGED:
"```Reply/tag/enter contact number of the person to be promoted.```"
},
qr: {
DESCRIPTION: "Convert a text/image to a QR code",
EXTENDED_DESCRIPTION:
"```Use this module to convert a text into a qr code. You can either specify the text after the .qr command or reply to a message using .qr.```",
INVALID_INPUT:
"```No input provided. Specify the text to be converted to QR code after the ``` *.qr* ```command or reply to a text/image using the``` *.qr* ```command.```",
PROCESSING: "```Generating QR code. Please wait...```",
IMAGE_CAPTION: "```Here's your QR image.```",
},
rbl: {
DESCRIPTION: "Module to enable a blacklist person or group to use the bot.",
EXTENDED_DESCRIPTION:
Expand Down
140 changes: 140 additions & 0 deletions modules/decodeqr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
const { MessageType } = require("@adiwajshing/baileys");
const Jimp = require("jimp");
const fs = require("fs");
const ffmpeg = require("fluent-ffmpeg");
const { JSDOM } = require("jsdom");
const { window } = new JSDOM();
const inputSanitization = require("../sidekick/input-sanitization");
const qrCode = require("qrcode-reader");
const Strings = require("../lib/db");
const DECODE = Strings.decodeqr;

module.exports = {
name: "dqr",
description: DECODE.DESCRIPTION,
extendedDescription: DECODE.EXTENDED_DESCRIPTION,
demo: { isEnabled: false },

async handle(client, chat, BotsApp, args) {
var startTime = window.performance.now();

var processing, filePath;

// Function to convert qr to text
const qrToText = async (imagePath, processing) => {
var buffer = fs.readFileSync(imagePath);
Jimp.read(buffer, function (err, image) {
if (err) {
console.error(err);
}
let qrcode = new qrCode();
qrcode.callback = async function (err, value) {
if (err) {
console.error(err);
} else {
// Printing the decrypted value
console.log(value.result);
await client
.sendMessage(BotsApp.chatId, value.result, MessageType.text)
.catch((err) =>
inputSanitization.handleError(err, client, BotsApp)
);
}
};

// Decoding the QR code
qrcode.decode(image.bitmap);
});

//Image and message deletion
await inputSanitization.deleteFiles(imagePath);
return await client
.deleteMessage(BotsApp.chatId, {
id: processing.key.id,
remoteJid: BotsApp.chatId,
fromMe: true,
})
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
};

// Function to convert sticker to image
const convertToImage = async (stickerId, replyChat, processing) => {
const fileName = "./tmp/convert_to_image-" + stickerId;
const filePath = await client
.downloadAndSaveMediaMessage(replyChat, fileName)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
const imagePath = "./tmp/image-" + stickerId + ".png";

try {
ffmpeg(filePath)
.save(imagePath)
.on("error", async function (err, stdout, stderr) {
inputSanitization.deleteFiles(filePath);
inputSanitization.performanceTime(startTime);
throw err;
})
.on("end", async () => {
inputSanitization.deleteFiles(filePath);
inputSanitization.performanceTime(startTime);
qrToText(imagePath, processing);
});

} catch (err) {
await inputSanitization.handleError(err, client, BotsApp);
}
};

try {
if (BotsApp.isReply && (BotsApp.isReplyAudio || BotsApp.isReplyVideo || BotsApp.isReplyAnimatedSticker)) {

await client
.sendMessage(BotsApp.chatId, DECODE.INVALID_REPLY, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
inputSanitization.performanceTime(startTime);
return;

} else if (BotsApp.isReplySticker) {

processing = await client
.sendMessage(BotsApp.chatId, DECODE.PROCESSING, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
var replyChatObject = {
message: chat.message.extendedTextMessage.contextInfo.quotedMessage,
};
var stickerId = chat.message.extendedTextMessage.contextInfo.stanzaId;
filePath = await convertToImage(stickerId, replyChatObject, processing);

} else if (BotsApp.isReplyImage) {

processing = await client
.sendMessage(BotsApp.chatId, DECODE.PROCESSING, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
var imageId = chat.key.id;
const fileName = "./tmp/qr_pic" + imageId;
filePath = await client
.downloadAndSaveMediaMessage(
{
message:
chat.message.extendedTextMessage.contextInfo.quotedMessage,
},
fileName
)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));

qrToText(filePath, processing);

} else if (!BotsApp.isImage) {

await client
.sendMessage(BotsApp.chatId, DECODE.INVALID_INPUT, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
inputSanitization.performanceTime(startTime);
return;

}

} catch (err) {
await inputSanitization.handleError(err, client, BotsApp);
}
},
};
81 changes: 81 additions & 0 deletions modules/qr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { MessageType, Mimetype } = require("@adiwajshing/baileys");
const inputSanitization = require("../sidekick/input-sanitization");
const Strings = require("../lib/db");
const QR = Strings.qr;
const { Encoder, QRByte, ErrorCorrectionLevel } = require("@nuintun/qrcode");
const fs = require("fs");

module.exports = {
name: "qr",
description: QR.DESCRIPTION,
extendedDescription: QR.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ".qr Hey, I am BotsApp." },
async handle(client, chat, BotsApp, args) {
try {
if (args.length === 0 && !BotsApp.isReply) {
await client
.sendMessage(BotsApp.chatId, QR.INVALID_INPUT, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));
return;
}

const processing = await client
.sendMessage(BotsApp.chatId, QR.PROCESSING, MessageType.text)
.catch((err) => inputSanitization.handleError(err, client, BotsApp));

let message;
if (!BotsApp.isReply) {
message = args.join(" ");
} else {
message = BotsApp.replyMessage;
}

const qrcode = new Encoder();

qrcode.setEncodingHint(true);
qrcode.setErrorCorrectionLevel(ErrorCorrectionLevel.Q);
qrcode.write(new QRByte(message));
qrcode.make();
const output = qrcode.toDataURL().split(",")[1];

const imagePath = "./tmp/qr.png";
fs.writeFileSync(
imagePath,
output,
{ encoding: "base64" },
function (err) {
if (err) {
console.log(err);
}
}
);

await client.sendMessage(
BotsApp.chatId,
fs.readFileSync(imagePath),
MessageType.image,
{
mimetype: Mimetype.png,
caption: QR.IMAGE_CAPTION,
}).catch((err) =>
inputSanitization.handleError(err, client, BotsApp)
);

inputSanitization.deleteFiles(imagePath);

await client
.deleteMessage(BotsApp.chatId, {
id: processing.key.id,
remoteJid: BotsApp.chatId,
fromMe: true,
})
.catch((err) =>
inputSanitization.handleError(err, client, BotsApp)
);
return;

} catch (err) {
await inputSanitization.handleError(err, client, BotsApp);
}
}
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@adiwajshing/baileys": "^3.5.3",
"@nuintun/qrcode": "^3.0.1",
"@vitalets/google-translate-api": "^7.0.0",
"chalk": "^4.1.1",
"dotenv": "^10.0.0",
Expand All @@ -23,6 +24,7 @@
"ocr-space-api-wrapper": "^1.0.6",
"pg": "^8.7.1",
"python-format-js": "^1.3.9",
"qrcode-reader": "^1.0.4",
"sequelize": "^6.7.0",
"simple-git": "^2.47.0",
"sqlite3": "^5.0.2",
Expand Down

0 comments on commit 0e5e161

Please sign in to comment.