Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	webwhatsapi/__init__.py
  • Loading branch information
mukulhase committed May 18, 2018
2 parents 03295dd + 650a1b8 commit 4a10a89
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
23 changes: 15 additions & 8 deletions webwhatsapi/__init__.py
Expand Up @@ -315,7 +315,6 @@ def get_all_chat_ids(self):
def get_unread(self, include_me=False, include_notifications=False, use_unread_count=False):
"""
Fetches unread messages
:param include_me: Include user's messages
:type include_me: bool or None
:param include_notifications: Include events happening on chat
Expand Down Expand Up @@ -443,7 +442,7 @@ def get_chat_from_id(self, chat_id):

raise ChatNotFoundError("Chat {0} not found".format(chat_id))

def get_chat_from_phone_number(self, number):
def get_chat_from_phone_number(self, number, createIfNotFound = False):
"""
Gets chat by phone number
Number format should be as it appears in Whatsapp ID
Expand All @@ -460,13 +459,14 @@ def get_chat_from_phone_number(self, number):
if not isinstance(chat, UserChat) or number not in chat.id:
continue
return chat
if createIfNotFound:
self.create_chat_by_number(number)
self.wait_for_login()
for chat in self.get_all_chats():
if not isinstance(chat, UserChat) or number not in chat.id:
continue
return chat

self.create_chat_by_number(number)
self.wait_for_login()
for chat in self.get_all_chats():
if not isinstance(chat, UserChat) or number not in chat.id:
continue
return chat
raise ChatNotFoundError('Chat for phone {0} not found'.format(number))

def reload_qr(self):
Expand Down Expand Up @@ -512,6 +512,13 @@ def chat_send_message(self, chat_id, message):
return factory_message(result, self)
return result

def chat_reply_message(self, message_id, message):
result = self.wapi_functions.ReplyMessage(message_id, message)

if not isinstance(result, bool):
return factory_message(result, self)
return result

def send_message_to_id(self, recipient, message):
"""
Send a message to a chat given its ID
Expand Down
63 changes: 61 additions & 2 deletions webwhatsapi/js/wapi.js
Expand Up @@ -252,12 +252,12 @@ window.WAPI.getUnreadMessagesInChat = function (
if (!messageObj.__x_isNewMsg) {
break;
}

messageObj.__x_isNewMsg = false;
// process it
let message = WAPI.processMessageObj(messageObj,
includeMe,
includeNotifications);

// save processed message on result list
if (message) output.push(message);
}
Expand Down Expand Up @@ -509,6 +509,65 @@ window.WAPI.getMessageById = function (id, done) {
}
};

window.WAPI.ReplyMessage = function (idMessage,message,done) {
var messageObject = Store.Msg.find(idMessage);
if(messageObject===undefined){
if (done !== undefined) {
done(false);
return false;
} else {
return false;
}
}
messageObject = messageObject.value();
const Chats = Store.Chat.models;

for (const chat in Chats) {
if (isNaN(chat)) {
continue;
}

let temp = {};
temp.name = Chats[chat].__x__formattedTitle;
temp.id = Chats[chat].__x_id;
if (temp.id === messageObject.chat.id) {
if (done !== undefined) {
Chats[chat].sendMessage(message,null,messageObject).then(function () {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

var trials = 0;

function check() {
for (let i=Chats[chat].msgs.models.length - 1; i >= 0; i--) {
let msg = Chats[chat].msgs.models[i];

if (!msg.senderObj.isMe || msg.body != message) {
continue;
}
done(WAPI._serializeMessageObj(msg));
return True;
}
trials += 1;
console.log(trials);
if (trials > 30) {
done(true);
return;
}
sleep(500).then(check);
}
check();
});
return true;
} else {
Chats[chat].sendMessage(message,null,messageObject);
return true;
}
}
}
};

window.WAPI.sendMessageToID = function (id, message, done) {
if(Store.Chat.models.length == 0)
return false;
Expand Down

0 comments on commit 4a10a89

Please sign in to comment.