Forwarding messages

This commit is contained in:
Adhiraj
2020-07-28 15:41:32 +05:30
parent b068bba238
commit 466346c404
4 changed files with 44 additions and 10 deletions

View File

@@ -15,7 +15,7 @@ export default class WhatsAppWebBase extends WAConnection {
/** Set the callback for message status updates (when a message is delivered, read etc.) */
setOnMessageStatusChange(callback: (update: MessageStatusUpdate) => void) {
const func = (json) => {
const func = json => {
json = json[1]
let ids = json.id
if (json.cmd === 'ack') {

View File

@@ -62,6 +62,7 @@ export enum Mimetype {
}
export interface MessageOptions {
quoted?: WAMessage
contextInfo?: WAContextInfo
timestamp?: Date
caption?: string
thumbnail?: string
@@ -150,3 +151,4 @@ export const WAMessageType = function () {
export type WAContactMessage = proto.ContactMessage
export type WAMessageKey = proto.IMessageKey
export type WATextMessage = proto.ExtendedTextMessage
export type WAContextInfo = proto.IContextInfo

View File

@@ -195,6 +195,27 @@ export default class WhatsAppWebMessages extends WhatsAppWebGroups {
}
return this.sendGenericMessage (id, json, {})
}
/**
* Forward a message like WA does
* @param id the id to forward the message to
* @param message the message to forward
*/
async forardMessage(id: string, message: WAMessage) {
const content = message.message
if (!content) throw new Error ('no content in message')
let key = Object.keys(content)[0]
const score = content[key].contextInfo?.forwardingScore || 0
if (key === MessageType.text) {
content[MessageType.extendedText] = { text: content[key] }
delete content[MessageType.text]
key = MessageType.extendedText
}
content[key].contextInfo = { forwardingScore: (score+1), isForwarded: true }
return this.sendGenericMessage (id, content, {})
}
async sendMessage(
id: string,
message: string | WATextMessage | WALocationMessage | WAContactMessage | Buffer,
@@ -292,20 +313,22 @@ export default class WhatsAppWebMessages extends WhatsAppWebGroups {
}
/** Generic send message function */
async sendGenericMessage(id: string, message: WAMessageContent, options: MessageOptions) {
if (!options.timestamp) {
// if no timestamp was provided,
options.timestamp = new Date() // set timestamp to now
}
if (!options.timestamp) options.timestamp = new Date() // set timestamp to now
const key = Object.keys(message)[0]
const timestamp = options.timestamp.getTime()/1000
const quoted = options.quoted
if (options.contextInfo) message[key].contextInfo = options.contextInfo
if (quoted) {
const participant = quoted.key.participant || quoted.key.remoteJid
message[key].contextInfo = {
participant: participant,
stanzaId: quoted.key.id,
quotedMessage: quoted.message,
}
message[key].contextInfo = message[key].contextInfo || { }
message[key].contextInfo.participant = participant
message[key].contextInfo.stanzaId = quoted.key.id
message[key].contextInfo.quotedMessage = quoted.message
// if a participant is quoted, then it must be a group
// hence, remoteJid of group must also be entered
if (quoted.key.participant) {

View File

@@ -33,6 +33,15 @@ WAClientTest('Messages', (client) => {
const message = await sendAndRetreiveMessage(client, 'hello fren', MessageType.text)
assert.strictEqual(message.message.conversation, 'hello fren')
})
it('should forward a message', async () => {
let messages = await client.loadConversation (testJid, 1)
await client.forardMessage (testJid, messages[0])
messages = await client.loadConversation (testJid, 1)
const message = messages[0]
const content = message.message[ Object.keys(message.message)[0] ]
assert.equal (content?.contextInfo?.isForwarded, true)
})
it('should send a link preview', async () => {
const content = await client.generateLinkPreview ('hello this is from https://www.github.com/adiwajshing/Baileys')
const message = await sendAndRetreiveMessage(client, content, MessageType.text)