Skip to content

Commit

Permalink
feat: Send and receive location
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed Feb 8, 2020
1 parent 4b17684 commit 7b229a3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
2 changes: 2 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};
4 changes: 4 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
33 changes: 33 additions & 0 deletions src/structures/Location.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>}
Expand Down
4 changes: 3 additions & 1 deletion src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ exports.MessageTypes = {
IMAGE: 'image',
VIDEO: 'video',
DOCUMENT: 'document',
STICKER: 'sticker'
STICKER: 'sticker',
LOCATION: 'location',
REDACTED: 'redacted'
};

/**
Expand Down
14 changes: 13 additions & 1 deletion src/util/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -58,6 +69,7 @@ exports.LoadUtils = () => {
t: parseInt(new Date().getTime() / 1000),
isNewMsg: true,
type: 'chat',
...locationOptions,
...attOptions,
...quotedMsgOptions
};
Expand Down

0 comments on commit 7b229a3

Please sign in to comment.