Skip to content

Commit

Permalink
Merge pull request #40 from farinchan/master
Browse files Browse the repository at this point in the history
feat: send image
  • Loading branch information
mimamch authored Dec 10, 2024
2 parents 41dcfea + c995763 commit 70fced7
Show file tree
Hide file tree
Showing 4 changed files with 599 additions and 123 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ Open On Browser & Start New Session
| `to` | `string` | **Required**. Receiver Phone Number with Country Code (e.g: 62812345678) |
| `text` | `string` | **Required**. Text Message |

#### Send Image Message

```
POST /send-image
```

| Body | Type | Description |
| :-------- | :------- | :----------------------------------------------------------------------- |
| `session` | `string` | **Required**. Session Name You Have Created |
| `to` | `string` | **Required**. Receiver Phone Number with Country Code (e.g: 62812345678) |
| `urlImage`| `string` | **Required**. URL Image |
| `caption` | `string` | **Required**. Caption Massage |


#### Send Bulk Message

```
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/message_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ exports.sendMessage = async (req, res, next) => {
next(error);
}
};
exports.sendImageMessage = async (req, res, next) => {
try {
let to = req.body.to || req.query.to;
let caption = req.body.caption || req.query.caption;
let urlImage = req.body.urlImage || req.query.urlImage;
const sessionId =
req.body.session || req.query.session || req.headers.session;

if (!to || !urlImage) throw new ValidationError("Missing Parameters");

const receiver = to;
if (!sessionId) throw new ValidationError("Session Not Founds");
const send = await whatsapp.sendImage({
sessionId,
to: receiver,
media : urlImage,
text: caption,
});

res.status(200).json(
responseSuccessWithData({
id: send?.key?.id,
status: send?.status,
message: send?.message?.extendedTextMessage?.text || "Not Text",
remoteJid: send?.key?.remoteJid,
})
);
} catch (error) {
next(error);
}
}


exports.sendBulkMessage = async (req, res, next) => {
try {
const sessionId =
Expand Down
2 changes: 2 additions & 0 deletions app/routers/message_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const { Router } = require("express");
const {
sendMessage,
sendBulkMessage,
sendImageMessage
} = require("../controllers/message_controller");
const MessageRouter = Router();

MessageRouter.all("/send-message", sendMessage);
MessageRouter.all("/send-bulk-message", sendBulkMessage);
MessageRouter.all("/send-image", sendImageMessage);

module.exports = MessageRouter;
Loading

0 comments on commit 70fced7

Please sign in to comment.