Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mimamch committed Jan 17, 2023
1 parent 609f443 commit c3d0769
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ app.use(function (req, res, next) {

// error handler
app.use(function (err, req, res, next) {
return res.status(404).json({
status: false,
data: {
error: "route not found",
},
});
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
Expand Down
37 changes: 37 additions & 0 deletions handle-message-upsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { proto } = require("@adiwajshing/baileys");
const fs = require("fs");

/**
*
* @param {proto.IWebMessageInfo} msg
* @param {import("@adiwajshing/baileys").AnyWASocket} session
*/
const handleMessageUpsert = async (session, msg) => {
try {
if (msg.key.fromMe) return;
console.log(msg);
// if (msg.key.remoteJid == "120363041318010923@g.us") {
// session.sendMessage(
// msg.key.remoteJid,
// {
// text: "hai",
// },
// { quoted: msg }
// );
// }
if (
msg.message?.imageMessage?.caption?.toLowerCase().includes(".sticker")
) {
// msg.message?.imageMessage?.
// console.log(msg.message?.imageMessage.);
// await session.sendMessage(msg.key.remoteJid, {
// sticker: fs.readFileSync("img2.png"),
// mimetype: msg.message?.imageMessage?.mimetype,
// });
}
} catch (error) {
console.log(error);
}
};

module.exports = handleMessageUpsert;
47 changes: 47 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
sendMessageText,
getSession,
startWhatsapp,
getSessionList,
sendSticker,
} = require("../whatsapp");
var router = express.Router();

Expand Down Expand Up @@ -150,5 +152,50 @@ router.use("/send-bulk-message", async (req, res) => {
});
}
});
router.use("/sessions", async (req, res) => {
try {
const sessions = getSessionList();

res.status(200).json({
status: true,
data: {
sessions,
},
});
} catch (error) {
console.log(error);
res.status(500).json({
status: false,
data: {
error: error?.message,
},
});
}
});
// router.use("/send-sticker", async (req, res) => {
// try {
// const session = getSession(
// req.body.session || req.query.session || req.headers.session || "mimamch"
// );
// const sessions = sendSticker(session, {
// receiver: processNumber("085838707828"),
// });

// res.status(200).json({
// status: true,
// data: {
// sessions,
// },
// });
// } catch (error) {
// console.log(error);
// res.status(500).json({
// status: false,
// data: {
// error: error?.message,
// },
// });
// }
// });

module.exports = router;
18 changes: 17 additions & 1 deletion whatsapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fs = require("fs");
const path = require("path");
const { processNumber } = require("./tools");
const { toDataURL } = require("qrcode");
const handleMessageUpsert = require("./handle-message-upsert");
let msgRetryCounterMap = {};

const sessions = new Map();
Expand Down Expand Up @@ -69,8 +70,10 @@ exports.startWhatsapp = async (name, res, scan) => {
await saveCreds();
}
});
// sock.ev.on("messages.upsert", (msg) =>
// handleMessageUpsert(sock, msg.messages[0])
// );
};

/**
* @returns {(import('@adiwajshing/baileys').AnyWASocket|null)}
*/
Expand Down Expand Up @@ -117,6 +120,19 @@ exports.isNumberExist = async (session, receiver, isGroup) => {
}
};

exports.getSessionList = () => {
return Array.from(sessions.keys());
};

/**
* @param {import('@adiwajshing/baileys').AnyWASocket} session
*/
exports.sendSticker = async (session, { receiver, sticker }) => {
return session.sendMessage(receiver, {
sticker: "https://placehold.jp/150x150.png",
});
};

exports.init = () => {
if (!fs.existsSync(path.resolve("credentials"))) {
fs.mkdirSync(path.resolve("credentials"));
Expand Down

0 comments on commit c3d0769

Please sign in to comment.