Skip to content

Instantly share code, notes, and snippets.

@sjcotto
Created December 16, 2017 20:58
Show Gist options
  • Save sjcotto/41ab50ed18dd25c05b96fb3b30876713 to your computer and use it in GitHub Desktop.
Save sjcotto/41ab50ed18dd25c05b96fb3b30876713 to your computer and use it in GitHub Desktop.
function isChatMessage(message) {
if (message.__x_isSentByMe) {
return false;
}
if (message.__x_isNotification) {
return false;
}
if (!message.__x_isUserCreatedType) {
return false;
}
return true;
}
function getUnreadChats() {
var Chats = Store.Chat.models;
var Output = [];
for (chat in Chats) {
if (isNaN(chat)) {
continue;
};
var temp = {};
temp.contact = Chats[chat].__x_formattedTitle;
temp.id = Chats[chat].__x_id;
temp.messages = [];
var messages = Chats[chat].msgs.models;
for (var i = messages.length - 1; i >= 0; i--) {
if (!messages[i].__x_isNewMsg) {
break;
} else {
if (!isChatMessage(messages[i])) {
continue
}
messages[i].__x_isNewMsg = false;
temp.messages.push({
message: messages[i].__x_body,
timestamp: messages[i].__x_t,
type : messages[i].__x_type,
e : messages[i]
});
}
}
if(temp.messages.length > 0) {
Output.push(temp);
}
}
console.log("Unread messages: ", Output);
return Output;
}
function sendMsg (id, text) {
var Chats = Store.Chat.models;
var contact = id;
var message = text;
for (chat in Chats) {
if (isNaN(chat)) {
continue;
};
var temp = {};
temp.contact = Chats[chat].__x__formattedTitle;
temp.id = Chats[chat].__x_id;
if(temp.id.search(contact)!=-1 && temp.id.search('g.us')==-1 ){
Chats[chat].sendMessage(message);
return true
}
}
}
@arianpasquali
Copy link

Awesome. Good job.
But how do you integrate this code with an external service?
I wrote a python server that handles input messages and answers like a bot, later I wrote an ajax call in this code to make the remote call.
However, I keep having tons of problems regarding security.
I have already tried all possible workaround I can think of with no success yet.

How do you integrate this with external sources?

@domgjntoki
Copy link

domgjntoki commented Jan 11, 2019

If anyone is having trouble trying to remove a message for everyone in a chat, I made a script for that:
Use: deleteMessage("5************-15********@g.us", "I will be deleted")

// The chat_id is in chat.__x_id._serialized; 
window.deleteMessage = function(chat_id, msg_str) {
    if (window.Store === undefined) {
        try {  // Often this cause the error "Cannot read property 'call' of undefined" but works anyway
            webpackJsonp([], {"bcihgfbdeb": (x, y, z) => window.Store = z('"bcihgfbdeb"')}, "bcihgfbdeb");
            webpackJsonp([], {"iaeeehaci": (x, y, z) => window.Store.Wap = z('"iaeeehaci"')}, "iaeeehaci");
        } catch (e) {}
    }
    Chats = Store.Chat.models
    for(chat in Chats) {
        if (isNaN(chat)) {
            continue;
        };
        temp_id = Chats[chat].__x_id._serialized;
        if(temp_id.search(chat_id) != -1){
            messages = Chats[chat].msgs.models
            for(message in messages) {
                temp_msg = messages[message]
                if(temp_msg.__x_text == msg_str && temp_msg.canRevoke()) {
                    temp_msg.sendRevoke(temp_msg)
                    return true;
                }
            }
        }
    }
}

@techashish12
Copy link

Temp.id.search is not a function

@thEpisode
Copy link

Here is a quick fix for Store is not defined
Just run
if (window.Store === undefined) {
webpackJsonp([], {"bcihgfbdeb": (x, y, z) => window.Store = z('"bcihgfbdeb"')}, "bcihgfbdeb");
webpackJsonp([], {"iaeeehaci": (x, y, z) => window.Store.Wap = z('"iaeeehaci"')}, "iaeeehaci");
}
Credit @schgressive

A little update:

if (window.Store === undefined) {
    webpackJsonp([], { "bcihgfbdeb": (x, y, z) => window.Store = z('"bcihgfbdeb"') }, "bcihgfbdeb");
    webpackJsonp([], { "cbagcefdge": (x, y, z) => window.Store.Wap = z('"cbagcefdge"') }, "cbagcefdge");
  }

@Zibri
Copy link

Zibri commented Feb 18, 2019

@Zibri
Copy link

Zibri commented Feb 18, 2019

but the script errors out saying:
Uncaught TypeError: temp.id.search is not a function

@mclabman
Copy link

Uncaught TypeError: temp.id.search is not a function
Change to temp.id = Chats[chat].__x_id.user;

@mclabman
Copy link

And how to get received message event?

@domgjntoki
Copy link

The sendMessage function in the Store.Chat.models[i].sendMessage has disappeared, has someone successfully fixed that?
The error
M433:1 Uncaught TypeError: Store.Chat.models[0].sendMessage is not a function

@sureshsarak
Copy link

Store.Chat.models
Uncaught TypeError: Cannot read property 'models' of undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment