-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
48 lines (37 loc) · 1.3 KB
/
examples.py
File metadata and controls
48 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
from tryx.backend import SqliteBackend
from tryx.client import Tryx, TryxClient # , Test, K
from tryx.events import EvMessage
from tryx.waproto.whatsapp_pb2 import Message as msg
DB_PATH = "whatsapp.db"
# t = Test()
# @t.on(K)
# async def handle_event(client: TryxClient, event: int) -> None:
# print("Handling event with data:", event)
backend = SqliteBackend(DB_PATH)
app = Tryx(backend)
@app.on(EvMessage)
async def on_message(client: TryxClient, event: EvMessage) -> None:
data = event.data
info = data.message_info
source = info.source
sender = source.sender
text = data.get_text() or data.caption or "<non-text message>"
sender_jid = f"{sender.user}@{sender.server}"
chat = source.chat
print(data.raw_proto)
print(f"[{info.id}] {sender_jid}: {text}")
print("text:", data.get_text())
print("client:", client)
print("chat:", chat, dir(chat))
await client.send_message(chat, msg(conversation="Hello!"))
await asyncio.sleep(1)
await client.send_message(chat, msg(conversation="This is a follow-up message."))
await asyncio.sleep(4)
await client.send_message(
chat, msg(conversation="This is a message after a delay.")
)
async def main() -> None:
await app.run()
if __name__ == "__main__":
asyncio.run(main())