Skip to content

Commit 4a10a89

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # webwhatsapi/__init__.py
2 parents 03295dd + 650a1b8 commit 4a10a89

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

webwhatsapi/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ def get_all_chat_ids(self):
315315
def get_unread(self, include_me=False, include_notifications=False, use_unread_count=False):
316316
"""
317317
Fetches unread messages
318-
319318
:param include_me: Include user's messages
320319
:type include_me: bool or None
321320
:param include_notifications: Include events happening on chat
@@ -443,7 +442,7 @@ def get_chat_from_id(self, chat_id):
443442

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

446-
def get_chat_from_phone_number(self, number):
445+
def get_chat_from_phone_number(self, number, createIfNotFound = False):
447446
"""
448447
Gets chat by phone number
449448
Number format should be as it appears in Whatsapp ID
@@ -460,13 +459,14 @@ def get_chat_from_phone_number(self, number):
460459
if not isinstance(chat, UserChat) or number not in chat.id:
461460
continue
462461
return chat
462+
if createIfNotFound:
463+
self.create_chat_by_number(number)
464+
self.wait_for_login()
465+
for chat in self.get_all_chats():
466+
if not isinstance(chat, UserChat) or number not in chat.id:
467+
continue
468+
return chat
463469

464-
self.create_chat_by_number(number)
465-
self.wait_for_login()
466-
for chat in self.get_all_chats():
467-
if not isinstance(chat, UserChat) or number not in chat.id:
468-
continue
469-
return chat
470470
raise ChatNotFoundError('Chat for phone {0} not found'.format(number))
471471

472472
def reload_qr(self):
@@ -512,6 +512,13 @@ def chat_send_message(self, chat_id, message):
512512
return factory_message(result, self)
513513
return result
514514

515+
def chat_reply_message(self, message_id, message):
516+
result = self.wapi_functions.ReplyMessage(message_id, message)
517+
518+
if not isinstance(result, bool):
519+
return factory_message(result, self)
520+
return result
521+
515522
def send_message_to_id(self, recipient, message):
516523
"""
517524
Send a message to a chat given its ID

webwhatsapi/js/wapi.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ window.WAPI.getUnreadMessagesInChat = function (
252252
if (!messageObj.__x_isNewMsg) {
253253
break;
254254
}
255-
255+
messageObj.__x_isNewMsg = false;
256256
// process it
257257
let message = WAPI.processMessageObj(messageObj,
258258
includeMe,
259259
includeNotifications);
260-
260+
261261
// save processed message on result list
262262
if (message) output.push(message);
263263
}
@@ -509,6 +509,65 @@ window.WAPI.getMessageById = function (id, done) {
509509
}
510510
};
511511

512+
window.WAPI.ReplyMessage = function (idMessage,message,done) {
513+
var messageObject = Store.Msg.find(idMessage);
514+
if(messageObject===undefined){
515+
if (done !== undefined) {
516+
done(false);
517+
return false;
518+
} else {
519+
return false;
520+
}
521+
}
522+
messageObject = messageObject.value();
523+
const Chats = Store.Chat.models;
524+
525+
for (const chat in Chats) {
526+
if (isNaN(chat)) {
527+
continue;
528+
}
529+
530+
let temp = {};
531+
temp.name = Chats[chat].__x__formattedTitle;
532+
temp.id = Chats[chat].__x_id;
533+
if (temp.id === messageObject.chat.id) {
534+
if (done !== undefined) {
535+
Chats[chat].sendMessage(message,null,messageObject).then(function () {
536+
function sleep(ms) {
537+
return new Promise(resolve => setTimeout(resolve, ms));
538+
}
539+
540+
var trials = 0;
541+
542+
function check() {
543+
for (let i=Chats[chat].msgs.models.length - 1; i >= 0; i--) {
544+
let msg = Chats[chat].msgs.models[i];
545+
546+
if (!msg.senderObj.isMe || msg.body != message) {
547+
continue;
548+
}
549+
done(WAPI._serializeMessageObj(msg));
550+
return True;
551+
}
552+
trials += 1;
553+
console.log(trials);
554+
if (trials > 30) {
555+
done(true);
556+
return;
557+
}
558+
sleep(500).then(check);
559+
}
560+
check();
561+
});
562+
return true;
563+
} else {
564+
Chats[chat].sendMessage(message,null,messageObject);
565+
return true;
566+
}
567+
}
568+
}
569+
};
570+
512571
window.WAPI.sendMessageToID = function (id, message, done) {
513572
if(Store.Chat.models.length == 0)
514573
return false;

0 commit comments

Comments
 (0)