mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
feat: Send and receive location
This commit is contained in:
@@ -59,6 +59,7 @@ Take a look at [example.js](https://github.com/pedroslopez/whatsapp-web.js/blob/
|
||||
| Promote/demote group participants | ✅ |
|
||||
| Mention users | _pending_ |
|
||||
| Get contact info | ✅ |
|
||||
| Send/receive Location | ✅ |
|
||||
|
||||
Something missing? Make an issue and let us know!
|
||||
|
||||
|
||||
@@ -123,6 +123,8 @@ client.on('message', async msg => {
|
||||
client.sendMessage(msg.from, attachmentData, {caption: 'Here\'s your requested media.'});
|
||||
}
|
||||
|
||||
} else if(msg.location) {
|
||||
msg.reply(msg.location);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
3
index.js
3
index.js
@@ -14,5 +14,6 @@ module.exports = {
|
||||
Contact: require('./src/structures/Contact'),
|
||||
PrivateContact: require('./src/structures/PrivateContact'),
|
||||
BusinessContact: require('./src/structures/BusinessContact'),
|
||||
ClientInfo: require('./src/structures/ClientInfo')
|
||||
ClientInfo: require('./src/structures/ClientInfo'),
|
||||
Location: require('./src/structures/Location')
|
||||
};
|
||||
@@ -13,6 +13,7 @@ const ContactFactory = require('./factories/ContactFactory');
|
||||
const ClientInfo = require('./structures/ClientInfo');
|
||||
const Message = require('./structures/Message');
|
||||
const MessageMedia = require('./structures/MessageMedia');
|
||||
const Location = require('./structures/Location');
|
||||
|
||||
/**
|
||||
* Starting point for interacting with the WhatsApp Web API
|
||||
@@ -255,6 +256,9 @@ class Client extends EventEmitter {
|
||||
} else if(options.media instanceof MessageMedia) {
|
||||
internalOptions.media = options.media;
|
||||
internalOptions.caption = content;
|
||||
} else if(content instanceof Location) {
|
||||
internalOptions.location = content;
|
||||
content = '';
|
||||
}
|
||||
|
||||
const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => {
|
||||
|
||||
33
src/structures/Location.js
Normal file
33
src/structures/Location.js
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Location information
|
||||
*/
|
||||
class Location {
|
||||
/**
|
||||
* @param {number} latitude
|
||||
* @param {number} longitude
|
||||
* @param {?string} descriptionon
|
||||
*/
|
||||
constructor(latitude, longitude, description) {
|
||||
/**
|
||||
* Location latitude
|
||||
* @type {number}
|
||||
*/
|
||||
this.latitude = latitude;
|
||||
|
||||
/**
|
||||
* Location longitude
|
||||
* @type {number}
|
||||
*/
|
||||
this.longitude = longitude;
|
||||
|
||||
/**
|
||||
* Name for the location
|
||||
* @type {?string}
|
||||
*/
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Location;
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
const Base = require('./Base');
|
||||
const MessageMedia = require('./MessageMedia');
|
||||
const Location = require('./Location');
|
||||
const { MessageTypes } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Represents a Message on WhatsApp
|
||||
@@ -90,6 +92,12 @@ class Message extends Base {
|
||||
*/
|
||||
this.hasQuotedMsg = data.quotedMsg ? true : false;
|
||||
|
||||
/**
|
||||
* Location information contained in the message, if the message is type "location"
|
||||
* @type {Location}
|
||||
*/
|
||||
this.location = data.type === MessageTypes.LOCATION ? new Location(data.lat, data.lng, data.loc) : undefined;
|
||||
|
||||
/**
|
||||
* Indicates the mentions in the message body.
|
||||
* @type {Array<string>}
|
||||
|
||||
@@ -51,7 +51,9 @@ exports.MessageTypes = {
|
||||
IMAGE: 'image',
|
||||
VIDEO: 'video',
|
||||
DOCUMENT: 'document',
|
||||
STICKER: 'sticker'
|
||||
STICKER: 'sticker',
|
||||
LOCATION: 'location',
|
||||
REDACTED: 'redacted'
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,7 +24,7 @@ exports.ExposeStore = (moduleRaidStr) => {
|
||||
exports.LoadUtils = () => {
|
||||
window.WWebJS = {};
|
||||
|
||||
window.WWebJS.sendMessage = async (chat, content, options) => {
|
||||
window.WWebJS.sendMessage = async (chat, content, options={}) => {
|
||||
let attOptions = {};
|
||||
if (options.attachment) {
|
||||
attOptions = await window.WWebJS.processMediaData(options.attachment);
|
||||
@@ -39,6 +39,17 @@ exports.LoadUtils = () => {
|
||||
}
|
||||
delete options.quotedMessageId;
|
||||
}
|
||||
|
||||
let locationOptions = {};
|
||||
if (options.location) {
|
||||
locationOptions = {
|
||||
type: 'location',
|
||||
loc: options.location.description,
|
||||
lat: options.location.latitude,
|
||||
lng: options.location.longitude
|
||||
};
|
||||
delete options.location;
|
||||
}
|
||||
|
||||
const newMsgId = new window.Store.MsgKey({
|
||||
from: window.Store.Conn.me,
|
||||
@@ -58,6 +69,7 @@ exports.LoadUtils = () => {
|
||||
t: parseInt(new Date().getTime() / 1000),
|
||||
isNewMsg: true,
|
||||
type: 'chat',
|
||||
...locationOptions,
|
||||
...attOptions,
|
||||
...quotedMsgOptions
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user