From 7b229a39a446f2b0f726e4c61b82f49669971a6d Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sat, 8 Feb 2020 00:46:08 -0400 Subject: [PATCH] feat: Send and receive location --- README.md | 1 + example.js | 2 ++ index.js | 3 ++- src/Client.js | 4 ++++ src/structures/Location.js | 33 +++++++++++++++++++++++++++++++++ src/structures/Message.js | 8 ++++++++ src/util/Constants.js | 4 +++- src/util/Injected.js | 14 +++++++++++++- 8 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/structures/Location.js diff --git a/README.md b/README.md index deebbe8..1d9d035 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/example.js b/example.js index 245cd29..2f8708d 100644 --- a/example.js +++ b/example.js @@ -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); } }); diff --git a/index.js b/index.js index abe07e8..a5a6bc2 100644 --- a/index.js +++ b/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') }; \ No newline at end of file diff --git a/src/Client.js b/src/Client.js index b277e27..169653f 100644 --- a/src/Client.js +++ b/src/Client.js @@ -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) => { diff --git a/src/structures/Location.js b/src/structures/Location.js new file mode 100644 index 0000000..00e6482 --- /dev/null +++ b/src/structures/Location.js @@ -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; \ No newline at end of file diff --git a/src/structures/Message.js b/src/structures/Message.js index c4dec50..3e47f35 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -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} diff --git a/src/util/Constants.js b/src/util/Constants.js index df49d34..48a3d93 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -51,7 +51,9 @@ exports.MessageTypes = { IMAGE: 'image', VIDEO: 'video', DOCUMENT: 'document', - STICKER: 'sticker' + STICKER: 'sticker', + LOCATION: 'location', + REDACTED: 'redacted' }; /** diff --git a/src/util/Injected.js b/src/util/Injected.js index 59c46de..a78b4c2 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -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 };