Skip to content

Commit d58c561

Browse files
committed
fix: goneonize
1 parent a21d3b8 commit d58c561

File tree

72 files changed

+18434
-3707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+18434
-3707
lines changed

examples/multisession.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,12 @@ def interrupted(*_):
3535
signal.signal(signal.SIGINT, interrupted)
3636

3737

38-
3938
client_factory = ClientFactory("db.sqlite3")
4039

4140
# create clients from preconfigured sessions
4241
sessions = client_factory.get_all_devices()
4342
for device in sessions:
44-
client_factory.new_client(
45-
device.JID
46-
)
43+
client_factory.new_client(device.JID)
4744
# if new_client jid parameter is not passed, it will create a new client
4845

4946
# or create a new client
@@ -71,7 +68,6 @@ def on_message(client: NewClient, message: MessageEv):
7168
handler(client, message)
7269

7370

74-
7571
def handler(client: NewClient, message: MessageEv):
7672
text = message.Message.conversation or message.Message.extendedTextMessage.text
7773
chat = message.Info.MessageSource.Chat

goneonize/Neonize.proto

+8
Original file line numberDiff line numberDiff line change
@@ -869,4 +869,12 @@ message ReturnFunctionWithError {
869869
LocalChatSettings LocalChatSettings = 2;
870870
}
871871

872+
}
873+
874+
message SendRequestExtra {
875+
required string ID = 1;
876+
required JID InlineBotJID = 2;
877+
required bool Peer = 3;
878+
required int64 Timeout = 4;
879+
required string MediaHandle = 5;
872880
}

goneonize/defproto/Neonize.pb.go

+148-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

goneonize/defproto/Neonize.proto

+8
Original file line numberDiff line numberDiff line change
@@ -869,4 +869,12 @@ message ReturnFunctionWithError {
869869
LocalChatSettings LocalChatSettings = 2;
870870
}
871871

872+
}
873+
874+
message SendRequestExtra {
875+
required string ID = 1;
876+
required JID InlineBotJID = 2;
877+
required bool Peer = 3;
878+
required int64 Timeout = 4;
879+
required string MediaHandle = 5;
872880
}

goneonize/main.go

+67-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"github.com/krypton-byte/neonize/utils"
2121
_ "github.com/mattn/go-sqlite3"
2222
"go.mau.fi/whatsmeow"
23+
"go.mau.fi/whatsmeow/proto/waConsumerApplication"
24+
"go.mau.fi/whatsmeow/proto/waMsgApplication"
2325
"go.mau.fi/whatsmeow/store"
2426
"go.mau.fi/whatsmeow/store/sqlstore"
2527
"go.mau.fi/whatsmeow/types"
@@ -159,7 +161,7 @@ func Neonize(db *C.char, id *C.char, JIDByte *C.uchar, JIDSize C.int, logLevel *
159161
}
160162
deviceStore, err_device = container.GetDevice(utils.DecodeJidProto(&JID))
161163
} else {
162-
deviceStore, err_device = container.GetFirstDevice(), nil
164+
deviceStore, err_device = container.GetFirstDevice()
163165
}
164166
if err_device != nil {
165167
panic(err)
@@ -1969,6 +1971,70 @@ func GetAllDevices(db *C.char) *C.char {
19691971
return C.CString(result.String())
19701972
}
19711973

1974+
//export SendFBMessage
1975+
func SendFBMessage(id *C.char, to *C.uchar, toSize C.int, message *C.uchar, messageSize C.int, metadata *C.uchar, metadataSize C.int, extra *C.uchar, extraSize C.int) C.struct_BytesReturn {
1976+
var toJID defproto.JID
1977+
var waConsumerApp waConsumerApplication.ConsumerApplication
1978+
var waConsumerAppMetadata waMsgApplication.MessageApplication_Metadata
1979+
var SendRequestExtra defproto.SendRequestExtra
1980+
err := proto.Unmarshal(
1981+
getByteByAddr(
1982+
to,
1983+
toSize,
1984+
),
1985+
&toJID,
1986+
)
1987+
if err != nil {
1988+
panic(err)
1989+
}
1990+
err_1 := proto.Unmarshal(
1991+
getByteByAddr(
1992+
message,
1993+
messageSize,
1994+
),
1995+
&waConsumerApp,
1996+
)
1997+
if err_1 != nil {
1998+
panic(err_1)
1999+
}
2000+
err_2 := proto.Unmarshal(
2001+
getByteByAddr(metadata, metadataSize),
2002+
&waConsumerAppMetadata,
2003+
)
2004+
if err != nil {
2005+
panic(err_2)
2006+
}
2007+
err_3 := proto.Unmarshal(
2008+
getByteByAddr(extra, extraSize),
2009+
&SendRequestExtra,
2010+
)
2011+
if err_3 != nil {
2012+
panic(err_3)
2013+
}
2014+
resp, err_fbmessage := clients[C.GoString(id)].SendFBMessage(
2015+
context.Background(),
2016+
utils.DecodeJidProto(&toJID),
2017+
&waConsumerApp,
2018+
&waConsumerAppMetadata,
2019+
utils.DecodeSendRequestExtra(
2020+
&SendRequestExtra,
2021+
),
2022+
)
2023+
if err_fbmessage != nil {
2024+
panic(err_fbmessage)
2025+
}
2026+
response := defproto.SendResponse{
2027+
Timestamp: proto.Int64(resp.Timestamp.UnixNano()),
2028+
ID: proto.String(resp.ID),
2029+
ServerID: proto.Int64(int64(resp.ServerID)),
2030+
DebugTimings: utils.EncodeMessageDebugTimings(resp.DebugTimings),
2031+
}
2032+
response_bytes, err_marshal := proto.Marshal(&response)
2033+
if err_marshal != nil {
2034+
panic(err_marshal)
2035+
}
2036+
return ReturnBytes(response_bytes)
2037+
}
19722038
func main() {
19732039

19742040
}

goneonize/utils/decoder.go

+9
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,12 @@ func DecodeContactEntry(entry *defproto.ContactEntry) *store.ContactEntry {
162162
FullName: *entry.FullName,
163163
}
164164
}
165+
166+
func DecodeSendRequestExtra(extra *defproto.SendRequestExtra) whatsmeow.SendRequestExtra {
167+
return whatsmeow.SendRequestExtra{
168+
ID: *extra.ID,
169+
InlineBotJID: DecodeJidProto(extra.InlineBotJID),
170+
Peer: *extra.Peer,
171+
Timeout: time.Duration(*extra.Timeout),
172+
}
173+
}

neonize/_binder.py

+12
Original file line numberDiff line numberDiff line change
@@ -454,5 +454,17 @@ def get_bytes(self):
454454
gocode.GetChatSettings.restype = Bytes
455455
gocode.GetAllDevices.argtypes = [ctypes.c_char_p]
456456
gocode.GetAllDevices.restype = ctypes.c_char_p
457+
gocode.SendFBMessage.argtypes = [
458+
ctypes.c_char_p,
459+
ctypes.c_char_p,
460+
ctypes.c_int,
461+
ctypes.c_char_p,
462+
ctypes.c_int,
463+
ctypes.c_char_p,
464+
ctypes.c_int,
465+
ctypes.c_char_p,
466+
ctypes.c_int,
467+
]
468+
gocode.SendFBMessage.restype = Bytes
457469
else:
458470
gocode: Any = object()

neonize/client.py

+46-12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
NewsletterMetadata,
100100
PrivacySettings,
101101
ProfilePictureInfo,
102+
SendRequestExtra,
102103
StatusPrivacy,
103104
UploadReturnFunction,
104105
GroupInfo,
@@ -134,6 +135,8 @@
134135
DocumentMessage,
135136
ContactMessage,
136137
)
138+
from .proto.waConsumerApplication.WAConsumerApplication_pb2 import ConsumerApplication
139+
from .proto.waMsgApplication.WAMsgApplication_pb2 import MessageApplication
137140
from .types import MessageServerID, MessageWithContextInfo
138141
from .utils import add_exif, gen_vcard, log, validate_link
139142
from .utils.enum import (
@@ -2494,6 +2497,30 @@ def get_message_for_retry(
24942497
if not model.isEmpty:
24952498
return model.Message
24962499

2500+
def send_fb_message(
2501+
self,
2502+
to: JID,
2503+
message: ConsumerApplication,
2504+
metadata: MessageApplication.Metadata,
2505+
extra: SendRequestExtra,
2506+
):
2507+
to_buff = to.SerializeToString()
2508+
message_buff = message.SerializeToString()
2509+
metadata_buff = metadata.SerializeToString()
2510+
extra_buff = extra.SerializeToString()
2511+
response = self.__client.SendFBMessage(
2512+
self.uuid,
2513+
to_buff,
2514+
len(to_buff),
2515+
message_buff,
2516+
len(message_buff),
2517+
metadata_buff,
2518+
len(metadata_buff),
2519+
extra_buff,
2520+
len(extra_buff),
2521+
)
2522+
return SendResponse.FromString(response.get_bytes())
2523+
24972524
def connect(self):
24982525
"""Establishes a connection to the WhatsApp servers."""
24992526
# Convert the list of functions to a bytearray
@@ -2538,9 +2565,8 @@ def disconnect(self) -> None:
25382565
self.__client.Disconnect(self.uuid)
25392566

25402567

2541-
25422568
class ClientFactory:
2543-
def __init__(self, database_name: str = 'neonize.db') -> None:
2569+
def __init__(self, database_name: str = "neonize.db") -> None:
25442570
"""
25452571
This class is used to create new instances of the client.
25462572
"""
@@ -2559,31 +2585,39 @@ def get_all_devices_from_db(db: str) -> List["Device"]:
25592585
c_string = gocode.GetAllDevices(db.encode()).decode()
25602586
if not c_string:
25612587
return []
2562-
2588+
25632589
class Device:
2564-
def __init__(self, JID: JID, PushName: str, BussinessName: str = None, Initialized: bool = None):
2590+
def __init__(
2591+
self,
2592+
JID: JID,
2593+
PushName: str,
2594+
BussinessName: str = None,
2595+
Initialized: bool = None,
2596+
):
25652597
self.JID = JID
25662598
self.PushName = PushName
25672599
self.BusinessName = BussinessName
25682600
self.Initialized = Initialized
25692601

25702602
devices: list[Device] = []
25712603

2572-
for device_str in c_string.split('|\u0001|'):
2573-
id, push_name, bussniess_name, initialized = device_str.split(',')
2574-
id, server = id.split('@')
2604+
for device_str in c_string.split("|\u0001|"):
2605+
id, push_name, bussniess_name, initialized = device_str.split(",")
2606+
id, server = id.split("@")
25752607
jid = build_jid(id, server)
25762608

2577-
device = Device(jid, push_name, bussniess_name, initialized == 'true')
2609+
device = Device(jid, push_name, bussniess_name, initialized == "true")
25782610
devices.append(device)
2579-
2611+
25802612
return devices
25812613

25822614
def get_all_devices(self) -> List["Device"]:
25832615
"""Retrieves all devices associated with the current account from the database."""
25842616
return self.get_all_devices_from_db(self.database_name)
25852617

2586-
def new_client(self, jid: JID = None, uuid: str = None, props: Optional[DeviceProps] = None) -> NewClient:
2618+
def new_client(
2619+
self, jid: JID = None, uuid: str = None, props: Optional[DeviceProps] = None
2620+
) -> NewClient:
25872621
"""
25882622
This function creates a new instance of the client. If the jid parameter is not provided, a new client will be created.
25892623
:param name: The name of the client.
@@ -2601,7 +2635,7 @@ def new_client(self, jid: JID = None, uuid: str = None, props: Optional[DevicePr
26012635
raise Exception("JID and UUID cannot be none")
26022636

26032637
client = NewClient(self.database_name, jid, props, uuid)
2604-
self.clients.append(client)
2638+
self.clients.append(client)
26052639
return client
26062640

26072641
def run(self):
@@ -2612,4 +2646,4 @@ def run(self):
26122646
name=client.uuid,
26132647
).start()
26142648

2615-
Event.default_blocking(None)
2649+
Event.default_blocking(None)

neonize/events.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101

102102
event = EventThread()
103103

104+
104105
class EventsManager:
105106
def __init__(self, client_factory: ClientFactory):
106107
self.client_factory = client_factory
@@ -116,6 +117,7 @@ def __call__(
116117
:return: A decorator that registers the callback function.
117118
:rtype: Callable[[Callable[[NewClient, EventType], None]], None]
118119
"""
120+
119121
def callback(func: Callable[[NewClient, EventType], None]) -> None:
120122
for client in self.client_factory.clients:
121123
wrapped_func = client.event.wrap(func, event)
@@ -227,4 +229,4 @@ def callback(func: Callable[[NewClient, EventType], None]) -> None:
227229
wrapped_func = self.wrap(func, event)
228230
self.list_func.update({EVENT_TO_INT[event]: wrapped_func})
229231

230-
return callback
232+
return callback

neonize/proto/Neonize_pb2.py

+319-302
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)