Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bot invoking capabilities #3009

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,8 @@ declare namespace WAWebJS {
}[]
/** Send 'seen' status */
sendSeen?: boolean
/** Bot Wid when doing a bot mention like @Meta AI */
invokedBotWid?: string
/** Media to be sent */
media?: MessageMedia
/** Extra options */
Expand Down
2 changes: 2 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ class Client extends EventEmitter {
* @property {GroupMention[]} [groupMentions] - An array of object that handle group mentions
* @property {string[]} [mentions] - User IDs to mention in the message
* @property {boolean} [sendSeen=true] - Mark the conversation as seen after sending the message
* @property {string} [invokedBotWid=undefined] - Bot Wid when doing a bot mention like @Meta AI
* @property {string} [stickerAuthor=undefined] - Sets the author of the sticker, (if sendMediaAsSticker is true).
* @property {string} [stickerName=undefined] - Sets the name of the sticker, (if sendMediaAsSticker is true).
* @property {string[]} [stickerCategories=undefined] - Sets the categories of the sticker, (if sendMediaAsSticker is true). Provide emoji char array, can be null.
Expand Down Expand Up @@ -862,6 +863,7 @@ class Client extends EventEmitter {
parseVCards: options.parseVCards === false ? false : true,
mentionedJidList: options.mentions || [],
groupMentions: options.groupMentions,
invokedBotWid: options.invokedBotWid,
extraOptions: options.extra
};

Expand Down
6 changes: 4 additions & 2 deletions src/util/Injected/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ exports.ExposeStore = () => {
window.Store.QueryExist = window.require('WAWebQueryExistsJob').queryWidExists;
window.Store.ReplyUtils = window.require('WAWebMsgReply');
window.Store.Settings = window.require('WAWebUserPrefsGeneral');
window.Store.BotSecret = window.require('WAWebBotMessageSecret');
window.Store.BotProfiles = window.require('WAWebBotProfileCollection');

window.Store.ForwardUtils = {
...window.require("WAWebForwardMessagesToChat")
...window.require('WAWebForwardMessagesToChat')
};

window.Store.StickerTools = {
Expand Down Expand Up @@ -116,4 +118,4 @@ exports.ExposeStore = () => {
window.injectToFunction({ module: 'WAWebBackendJobsCommon', function: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });

window.injectToFunction({ module: 'WAWebE2EProtoUtils', function: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage || proto.groupInviteMessage ? 'text' : func(...args); });
};
};
15 changes: 15 additions & 0 deletions src/util/Injected/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ exports.LoadUtils = () => {
delete listOptions.list.footer;
}

const botOptions = {};
if (options.invokedBotWid) {
botOptions.messageSecret = window.crypto.getRandomValues(new Uint8Array(32));
botOptions.botMessageSecret = await window.Store.BotSecret.genBotMsgSecretFromMsgSecret(botOptions.messageSecret);
botOptions.invokedBotWid = window.Store.WidFactory.createWid(options.invokedBotWid);
botOptions.botPersonaId = window.Store.BotProfiles.BotProfileCollection.get(options.invokedBotWid).personaId;
delete options.invokedBotWid;
}

const meUser = window.Store.User.getMaybeMeUser();
const newId = await window.Store.MsgKey.newId();

Expand Down Expand Up @@ -232,8 +241,14 @@ exports.LoadUtils = () => {
...vcardOptions,
...buttonOptions,
...listOptions,
...botOptions,
...extraOptions
};

// Bot's won't reply if canonicalUrl is set (linking)
if (botOptions) {
delete message.canonicalUrl;
}

await window.Store.SendMessage.addAndSendMsgToChat(chat, message);
return window.Store.Msg.get(newMsgId._serialized);
Expand Down