Skip to content

Commit 872d910

Browse files
author
kennyromanov
committed
feat: The methods have been given the new names, The linting has been fixed
1 parent f2999cb commit 872d910

9 files changed

Lines changed: 165 additions & 78 deletions

File tree

‎.github/workflows/python-package.yml‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ jobs:
2525
- name: Install dependencies
2626
run: |
2727
python -m pip install --upgrade pip
28-
pip install ruff pytest
29-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
28+
python -m pip install ruff pytest pytest-asyncio ".[voip]"
3029
- name: Lint with ruff
3130
run: |
32-
# stop the build if there are Python syntax errors or undefined names
33-
ruff check --output-format=github --select=E9,F63,F7,F82 --target-version=py37 .
34-
# default set of ruff rules with GitHub Annotations
35-
ruff check --output-format=github --target-version=py37 .
31+
ruff check --output-format=github --select=E4,E7,E9,F --target-version=py310 .
3632
- name: Test with pytest
3733
run: |
3834
pytest

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ authorize the account, go to your [cabinet](https://console.green-api.com/) and
4040
python -m pip install whatsapp-api-client-python
4141
```
4242

43+
VoIP calls require the optional dependencies:
44+
45+
```shell
46+
python -m pip install "whatsapp-api-client-python[voip]"
47+
```
48+
4349
## Import
4450

4551
```
@@ -302,6 +308,8 @@ asyncio.run(main())
302308
| Example of account methods asynchronously | [accountMethodsAsync.py](./examples/async/accountMethodsAsync.py) |
303309
| Example of getting last incoming and outgoing calls | [lastCalls.py](./examples/sync/lastCalls.py) |
304310
| Example of getting last calls asynchronously | [lastCallsAsync.py](./examples/async/lastCallsAsync.py) |
311+
| Outgoing headless VoIP call | [headless_call.py](./examples/async/voip/headless_call.py) |
312+
| Incoming headless VoIP call | [incoming_call.py](./examples/async/voip/incoming_call.py) |
305313
| Example of sending a message with link preview options | [sendMessageWithPreview.py](./examples/sync/sending/sendMessageWithPreview.py) |
306314
| Example of sending a message with link preview options asynchronously | [sendMessageWithPreviewAsync.py](./examples/async/sending/sendMessageWithPreviewAsync.py) |
307315
| Example of queues methods (counts, clear webhooks queue) | [queuesMethods.py](./examples/sync/queuesMethods.py) |

‎docs/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ whatsapp-api-client-python - библиотека для интеграции с
3939
python -m pip install whatsapp-api-client-python
4040
```
4141

42+
Для VoIP нужны дополнительные зависимости:
43+
44+
```shell
45+
python -m pip install "whatsapp-api-client-python[voip]"
46+
```
47+
4248
## Импорт
4349

4450
```
@@ -303,6 +309,8 @@ asyncio.run(main())
303309
| Пример асинхронных методов аккаунта | [accountMethodsAsync.py](../examples/async/accountMethodsAsync.py) |
304310
| Пример получения журнала звонков | [lastCalls.py](../examples/sync/lastCalls.py) |
305311
| Пример асинхронного получения журнала звонков | [lastCallsAsync.py](../examples/async/lastCallsAsync.py) |
312+
| Исходящий VoIP-звонок без аудиоустройств | [headless_call.py](../examples/async/voip/headless_call.py) |
313+
| Входящий VoIP-звонок без аудиоустройств | [incoming_call.py](../examples/async/voip/incoming_call.py) |
306314
| Пример отправки сообщения с настройками превью ссылки | [sendMessageWithPreview.py](../examples/sync/sending/sendMessageWithPreview.py) |
307315
| Пример асинхронной отправки сообщения с превью ссылки | [sendMessageWithPreviewAsync.py](../examples/async/sending/sendMessageWithPreviewAsync.py) |
308316
| Пример методов очереди (счётчики, очистка вебхуков) | [queuesMethods.py](../examples/sync/queuesMethods.py) |

‎examples/async/voip/headless_call.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""Outgoing voice call using a WAV file and decoded audio frames, no audio devices."""
22

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
63
import asyncio
74
import os
85
import sys
96

7+
from aiortc.contrib.media import MediaPlayer
8+
9+
from whatsapp_api_client_python.API import GreenAPI
10+
from whatsapp_api_client_python.tools.voip import CallAudio, FrameAudioSink
11+
1012

1113
async def make_audio() -> CallAudio:
1214
player = MediaPlayer(sys.argv[2])
@@ -21,10 +23,10 @@ async def on_frame(frame):
2123
sink = FrameAudioSink(on_frame)
2224

2325
async def close():
24-
await sink.close()
26+
await sink.closeAsync()
2527
player.audio.stop()
2628

27-
return CallAudio(player.audio, sink.attach, close)
29+
return CallAudio(player.audio, sink.attachAsync, close)
2830

2931

3032
async def main():
@@ -41,12 +43,12 @@ def on_end(detail):
4143
calls.on("error", lambda detail: print("Call error:", detail))
4244

4345
try:
44-
await calls.open(timeout=30)
45-
await api.voip.dial(sys.argv[1])
46-
await calls.start_audio()
46+
await calls.openAsync(timeout=30)
47+
await api.voip.dialAsync(sys.argv[1])
48+
await calls.startAudioAsync()
4749
await ended.wait()
4850
finally:
49-
await calls.close()
51+
await calls.closeAsync()
5052

5153

5254
if __name__ == "__main__":
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Answer an incoming voice call using a WAV file, without audio devices."""
2+
3+
import asyncio
4+
import os
5+
import sys
6+
7+
from aiortc.contrib.media import MediaPlayer
8+
9+
from whatsapp_api_client_python.API import GreenAPI
10+
from whatsapp_api_client_python.tools.voip import CallAudio, FrameAudioSink
11+
12+
13+
async def make_audio() -> CallAudio:
14+
player = MediaPlayer(sys.argv[1])
15+
16+
if player.audio is None:
17+
raise ValueError("The file has no audio stream")
18+
19+
async def on_frame(frame):
20+
# Replace this with your application's frame consumer.
21+
print(f"received {frame.samples} samples")
22+
23+
sink = FrameAudioSink(on_frame)
24+
25+
async def close():
26+
await sink.closeAsync()
27+
player.audio.stop()
28+
29+
return CallAudio(player.audio, sink.attachAsync, close)
30+
31+
32+
async def main():
33+
api = GreenAPI(os.environ["GREEN_API_ID"], os.environ["GREEN_API_TOKEN"])
34+
calls = api.voip.connect(audio_factory=make_audio)
35+
incoming = asyncio.Queue()
36+
ended = asyncio.Event()
37+
38+
calls.on("incoming_call", incoming.put_nowait)
39+
calls.on("end_call", lambda detail: ended.set())
40+
calls.on("error", lambda detail: print("Call error:", detail))
41+
42+
try:
43+
await calls.openAsync(timeout=30)
44+
45+
info = await incoming.get()
46+
47+
print("Incoming call:", info.wid)
48+
49+
if calls.state is None or calls.state.state != "inc-call":
50+
return
51+
52+
await api.voip.acceptAsync()
53+
await calls.startAudioAsync()
54+
await ended.wait()
55+
finally:
56+
await calls.closeAsync()
57+
58+
59+
if __name__ == "__main__":
60+
if len(sys.argv) != 2:
61+
raise SystemExit("Usage: incoming_call.py <audio.wav>")
62+
63+
asyncio.run(main())

0 commit comments

Comments
 (0)