feat: Send and receive location

This commit is contained in:
Pedro Lopez
2020-02-08 00:46:08 -04:00
parent 4b1768450f
commit 7b229a39a4
8 changed files with 66 additions and 3 deletions

View File

@@ -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!

View File

@@ -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);
}
});

View File

@@ -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')
};

View File

@@ -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) => {

View 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;

View File

@@ -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>}

View File

@@ -51,7 +51,9 @@ exports.MessageTypes = {
IMAGE: 'image',
VIDEO: 'video',
DOCUMENT: 'document',
STICKER: 'sticker'
STICKER: 'sticker',
LOCATION: 'location',
REDACTED: 'redacted'
};
/**

View File

@@ -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
};