@@ -4,7 +4,7 @@ var { EventEmitter } = require("events");
44var util = require ( "./util.js" ) ;
55var { Opcode } = require ( "./enums.js" ) ;
66var { InvalidPhoneError, AppOldError, FailedToLoginError, InvalidPayloadError, InvalidQRDataError, QRExpiredError } = require ( "./errors.js" ) ;
7- var { Dialog, Chat, Channel, User, Me } = require ( "./types.js" ) ;
7+ var { Dialog, Chat, Channel, User, Me, Message } = require ( "./types.js" ) ;
88var chalk = require ( "chalk" ) ;
99var WebSocket = require ( "ws" ) ;
1010var UserAgent = require ( "user-agents" ) ;
@@ -241,6 +241,47 @@ class BaseClient extends EventEmitter {
241241 }
242242 this . _log ( "info" , `Sync completed: dialogs=${ this . dialogs . length } chats=${ this . chats . length } channels=${ this . channels . length } ` ) ;
243243 }
244+ async _handlePacket ( packet ) {
245+ var { payload } = packet ;
246+ if ( packet . opcode == Opcode . NOTIF_MESSAGE ) {
247+ var msg = new Message ( payload ) ;
248+ if ( msg . chatId && msg . id ) {
249+ this . _send ( Opcode . NOTIF_MESSAGE , {
250+ "chatId" : msg . chatId ,
251+ "messageId" : msg . id . toString ( )
252+ } , 1 ) ;
253+ this . _log ( "debug" , `Sent NOTIF_MESSAGE_RECEIVED for chat_id=${ msg . chatId } message_id=${ msg . id } ` ) ;
254+ }
255+ if ( payload . status == "EDITED" ) {
256+ this . emit ( "messageEdit" , msg ) ;
257+ } else if ( payload . status == "REMOVED" ) {
258+ this . emit ( "messageDelete" , msg ) ;
259+ } else {
260+ this . emit ( "message" , msg ) ;
261+ }
262+ }
263+ }
264+ sendMessage ( chatId , data ) {
265+ if ( typeof data === "string" ) {
266+ data = {
267+ "text" : data
268+ } ;
269+ }
270+ if ( typeof data . notify === "undefined" ) {
271+ data . notify = true ;
272+ }
273+ this . _log ( "info" , `Sending message to chat_id=${ chatId } notify=${ data . notify } ` ) ;
274+ this . _send ( Opcode . MSG_SEND , {
275+ chatId,
276+ "message" : {
277+ "text" : data . text ,
278+ "cid" : - Date . now ( ) ,
279+ "elements" : [ ] ,
280+ "attaches" : [ ]
281+ } ,
282+ "notify" : data . notify
283+ } ) ;
284+ }
244285}
245286
246287class MaxClient extends BaseClient {
@@ -269,6 +310,9 @@ class MaxClient extends BaseClient {
269310 setTimeout ( ( ) => this . start ( ) , this . reconnectDelay * 1000 ) ;
270311 }
271312 } ) ;
313+ this . _connection . on ( "message" , data => {
314+ this . _handlePacket ( JSON . parse ( data . toString ( "utf-8" ) ) ) ;
315+ } ) ;
272316 return new Promise ( res => {
273317 this . _connection . on ( "open" , ( ) => {
274318 this . isConnected = true ;
0 commit comments