Skip to content

Commit

Permalink
Group Message
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavms13 committed May 2, 2020
1 parent f9bfcb7 commit b869f35
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions APIDOC.MD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
``GET : /chat/getchats``
>Returns an Array of all Chats

## Group Chat
<u>Sending Messages to Group</u>
``POST : /group/sendmessage/<Group_Name>``
> Request Body
> - message - contains the message to be sent
<hr>

## Contact
<u>Get Contacts</u>
``GET : /contact/getcontacts``
Expand Down
12 changes: 11 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const express = require("express");
const bodyParser = require("body-parser");
const fs = require('fs');
const { Client } = require('whatsapp-web.js');
// const wwjs = require('whatsapp-web.js');
const SESSION_FILE_PATH = './session.json';
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) {
Expand Down Expand Up @@ -42,6 +41,7 @@ client.on('ready', () => {
client.initialize();

const chatRoute = require('./components/chatting');
const groupRoute = require('./components/group');
const authRoute = require('./components/auth');
const contactRoute = require('./components/contact');

Expand All @@ -50,9 +50,19 @@ app.use(function(req,res,next){
next();
});
app.use('/chat',chatRoute);
app.use('/group',groupRoute);
app.use('/auth',authRoute);
app.use('/contact',contactRoute);

app.listen(port, () => {
console.log("Server Running Live on Port : " + port);
});

// client.on('message', msg => {
// // console.log(msg.id)
// client.getChats().then((data) => {
// // console.log(JSON.stringify(data))
// console.log(data.length)

// });
// });
24 changes: 24 additions & 0 deletions components/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const router = require('express').Router();

router.post('/sendmessage/:chatname', async (req,res) => {
let chatname = req.params.chatname;
let message = req.body.message;

if(chatname==undefined||message==undefined){
res.send({status:"error",message:"please enter valid chatname and message"})
}else{
client.getChats().then((data) => {
data.forEach(chat => {
if(chat.id.server==="g.us" && chat.name===chatname){
client.sendMessage(chat.id._serialized,message).then((response)=>{
if(response.id.fromMe){
res.send({status:'success',message:'Message successfully send to '+chatname})
}
});
}
});
});
}
});

module.exports = router;

0 comments on commit b869f35

Please sign in to comment.