From 0e5e1615167270479fd82a3ef19ec56d10d8ebda Mon Sep 17 00:00:00 2001 From: Johann Jose <72455289+j0h4nn1410@users.noreply.github.com> Date: Sat, 11 Dec 2021 11:32:00 +0300 Subject: [PATCH] Modified 'getdp' module to display tagged person's display image. (#72) * Reincluded qr and decodeqr files. Signed-off-by: Johann * Reformat code Signed-off-by: Prince Mendiratta Co-authored-by: Prince Mendiratta --- lib/db.js | 16 +++++ modules/decodeqr.js | 140 ++++++++++++++++++++++++++++++++++++++++++++ modules/qr.js | 81 +++++++++++++++++++++++++ package.json | 2 + 4 files changed, 239 insertions(+) create mode 100644 modules/decodeqr.js create mode 100644 modules/qr.js diff --git a/lib/db.js b/lib/db.js index 15c92b8e..57ba7e5b 100644 --- a/lib/db.js +++ b/lib/db.js @@ -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: @@ -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: diff --git a/modules/decodeqr.js b/modules/decodeqr.js new file mode 100644 index 00000000..4a0a60db --- /dev/null +++ b/modules/decodeqr.js @@ -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); + } + }, +}; diff --git a/modules/qr.js b/modules/qr.js new file mode 100644 index 00000000..001e3238 --- /dev/null +++ b/modules/qr.js @@ -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); + } + } +}; diff --git a/package.json b/package.json index e8785e3d..da23f142 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",