Skip to content

Commit f2999cb

Browse files
author
kennyromanov
committed
add: The VoIP feature has been supported in the API client
1 parent 494b62d commit f2999cb

11 files changed

Lines changed: 1573 additions & 2 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ cython_debug/
157157
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160-
#.idea/
160+
/.idea/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Outgoing voice call using a WAV file and decoded audio frames, no audio devices."""
2+
3+
from aiortc.contrib.media import MediaPlayer
4+
from whatsapp_api_client_python.API import GreenAPI
5+
from whatsapp_api_client_python.tools.voip import CallAudio, FrameAudioSink
6+
import asyncio
7+
import os
8+
import sys
9+
10+
11+
async def make_audio() -> CallAudio:
12+
player = MediaPlayer(sys.argv[2])
13+
14+
if player.audio is None:
15+
raise ValueError("The file has no audio stream")
16+
17+
async def on_frame(frame):
18+
# Replace this with your application's frame consumer.
19+
print(f"received {frame.samples} samples")
20+
21+
sink = FrameAudioSink(on_frame)
22+
23+
async def close():
24+
await sink.close()
25+
player.audio.stop()
26+
27+
return CallAudio(player.audio, sink.attach, close)
28+
29+
30+
async def main():
31+
api = GreenAPI(os.environ["GREEN_API_ID"], os.environ["GREEN_API_TOKEN"])
32+
calls = api.voip.connect(audio_factory=make_audio)
33+
ended = asyncio.Event()
34+
calls.on("incoming_call", lambda info: print("Incoming call:", info.wid))
35+
36+
def on_end(detail):
37+
print("Call ended:", detail)
38+
ended.set()
39+
40+
calls.on("end_call", on_end)
41+
calls.on("error", lambda detail: print("Call error:", detail))
42+
43+
try:
44+
await calls.open(timeout=30)
45+
await api.voip.dial(sys.argv[1])
46+
await calls.start_audio()
47+
await ended.wait()
48+
finally:
49+
await calls.close()
50+
51+
52+
if __name__ == "__main__":
53+
if len(sys.argv) != 3:
54+
raise SystemExit("Usage: headless_call.py <phone> <audio.wav>")
55+
56+
asyncio.run(main())

‎setup.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"Creative Commons Attribution-NoDerivatives 4.0 International"
4141
" (CC BY-ND 4.0)"
4242
),
43-
install_requires=["requests-2.34.2", "aiofiles>=24.1.0", "aiogram>=3.28.2", "aiohttp>=3.13.5"],
43+
install_requires=["requests==2.34.2", "aiofiles>=24.1.0", "aiogram>=3.28.2", "aiohttp>=3.13.5"],
44+
extras_require={"voip": ["aiortc>=1.15,<2", "httpx>=0.28,<1", "websockets>=16,<17"]},
4445
python_requires=">=3.10"
4546
)

0 commit comments

Comments
 (0)