Repository navigation
Добавить возможность принимать входящие VoIP звонки в чатботе на Python #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
165fde4
feat: The incoming calls and the ai greeting text have been supported…
b581462
feat: Some interfaces and contracts have been improved; Breaking chan…
6c9bb2b
fix: The issue with LIDs has been fixed
c30c988
fix: The issue with the MP3 audio quality has been fixed
e609f16
feat: The calls implementation has been refactored; The DX has been i…
6ad980d
fix: The issue with the chatgpt greeting has been fixed; The linting …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| from whatsapp_chatbot_python import GreenAPIBot, Notification | ||
| from whatsapp_chatbot_python import GreenAPIBot, IncomingCallNotification | ||
|
|
||
| bot = GreenAPIBot( | ||
| "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" | ||
| ) | ||
|
|
||
|
|
||
| @bot.router.incoming_call() | ||
| def message_handler(notification: Notification) -> None: | ||
| print("Call from:", notification.event["from"], "with status", notification.event["status"]) | ||
| def incoming_call_handler(notification: IncomingCallNotification) -> None: | ||
| call = notification.call | ||
| print("Call from:", call.sender, "with status", call.status) | ||
| # notification.event (or notification.raw_event) still contains the original webhook. | ||
|
|
||
| bot.run_forever() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| """Deterministic external boundaries for call integration specifications.""" | ||
|
|
||
| import asyncio | ||
| from dataclasses import dataclass, field | ||
| from types import SimpleNamespace | ||
|
|
||
| from whatsapp_chatbot_python.calls.models import PCM | ||
|
|
||
|
|
||
| @dataclass | ||
| class Scenario: | ||
| state: str = "idle" | ||
| call_id: str = "call-1" | ||
| wid: str = "sender@c.us" | ||
| answer: bool = True | ||
| remote_hangup: bool = True | ||
| accept_error: bool = False | ||
| open_error: bool = False | ||
| bridge_error: bool = False | ||
| voice_error: bool = False | ||
| gates: dict[str, asyncio.Event] = field(default_factory=dict) | ||
| actions: list = field(default_factory=list) | ||
|
|
||
| def info(self): | ||
| return SimpleNamespace(id=self.call_id, wid=self.wid) | ||
|
|
||
| async def wait(self, name): | ||
| if gate := self.gates.get(name): | ||
| await gate.wait() | ||
|
|
||
|
|
||
| class FakeRtc: | ||
| def __init__(self, api, audio_factory): | ||
| self.api = api | ||
| self.audio_factory = audio_factory | ||
| self.listeners = {} | ||
| self.audio = None | ||
| self.closed = False | ||
|
|
||
| def on(self, name, callback): | ||
| self.listeners[name] = callback | ||
|
|
||
| def emit(self, name, detail=None): | ||
| if name == "state": | ||
| self.api.state = detail.state | ||
| self.api.reason = getattr(detail, "reason", None) | ||
| callback = self.listeners.get(name) | ||
| if callback: | ||
| callback(detail) | ||
|
|
||
| def set_state(self, state, reason=None, *, call_id=None): | ||
| info = None if state == "idle" else SimpleNamespace( | ||
| id=call_id or self.api.scenario.call_id, wid=self.api.scenario.wid) | ||
| self.emit("state", SimpleNamespace(state=state, reason=reason, info=info)) | ||
|
|
||
| async def openAsync(self, *, timeout): | ||
| self.api.scenario.actions.append("open") | ||
| await self.api.scenario.wait("open") | ||
| if self.api.scenario.open_error: | ||
| raise RuntimeError("RTC unavailable") | ||
| self.set_state(self.api.state) | ||
|
|
||
| async def startAudioAsync(self): | ||
| self.api.scenario.actions.append("bridge") | ||
| await self.api.scenario.wait("bridge") | ||
| self.audio = await self.audio_factory() | ||
| if self.api.scenario.bridge_error: | ||
| raise RuntimeError("bridge unavailable") | ||
| if self.api.scenario.remote_hangup: | ||
| asyncio.get_running_loop().call_later( | ||
| 0.01, self.set_state, "idle", "hangup") | ||
|
|
||
| async def closeAsync(self): | ||
| self.api.scenario.actions.append("rtc_close") | ||
| await self.api.scenario.wait("rtc_close") | ||
| if self.audio is not None: | ||
| await self.audio.close() | ||
| self.closed = True | ||
|
|
||
|
|
||
| class FakeApi: | ||
| def __init__(self, scenario, id_instance, api_token_instance, *, host, raise_errors, host_timeout): | ||
| self.scenario = scenario | ||
| self.credentials = (id_instance, api_token_instance, host) | ||
| self.state = scenario.state | ||
| self.reason = None | ||
| self.rtc = None | ||
| self.session_closed = False | ||
| self.session = SimpleNamespace(close=self.close_session) | ||
| self.voip = self | ||
|
|
||
| def close_session(self): | ||
| self.scenario.actions.append("session_close") | ||
| self.session_closed = True | ||
|
|
||
| def connect(self, *, audio_factory): | ||
| self.rtc = FakeRtc(self, audio_factory) | ||
| return self.rtc | ||
|
|
||
| async def getStateAsync(self): | ||
| self.scenario.actions.append("get_state") | ||
| info = None if self.state == "idle" else self.scenario.info() | ||
| return SimpleNamespace(state=self.state, info=info, reason=self.reason) | ||
|
|
||
| async def dialAsync(self, target): | ||
| self.scenario.actions.append(("dial", target)) | ||
| await self.scenario.wait("dial") | ||
| self.rtc.set_state("out-call") | ||
| if self.scenario.answer: | ||
| asyncio.get_running_loop().call_soon(self.rtc.set_state, "on-call") | ||
|
|
||
| async def acceptAsync(self): | ||
| self.scenario.actions.append("accept") | ||
| await self.scenario.wait("accept") | ||
| if self.scenario.accept_error: | ||
| raise RuntimeError("accept unavailable") | ||
| self.rtc.set_state("on-call") | ||
|
|
||
| async def rejectAsync(self): | ||
| self.scenario.actions.append("reject") | ||
| self.state = "idle" | ||
|
|
||
| async def hangUpAsync(self): | ||
| self.scenario.actions.append("hangup") | ||
| self.state = "idle" | ||
|
|
||
|
|
||
| class FakeVoice: | ||
| def __init__(self, scenario, *args): | ||
| self.scenario = scenario | ||
| self.greetings = [] | ||
| self.closed = False | ||
| self.started = False | ||
|
|
||
| async def __aenter__(self): | ||
| self.scenario.actions.append("voice_enter") | ||
| return self | ||
|
|
||
| async def __aexit__(self, *exc): | ||
| self.scenario.actions.append("voice_close") | ||
| self.closed = True | ||
|
|
||
| async def start(self): | ||
| self.scenario.actions.append("voice_start") | ||
| await self.scenario.wait("voice_start") | ||
| if self.scenario.voice_error: | ||
| raise RuntimeError("voice unavailable") | ||
| self.started = True | ||
|
|
||
| async def greet_once(self, text): | ||
| if not self.greetings: | ||
| self.greetings.append(text) | ||
| self.scenario.actions.append("greet") | ||
|
|
||
| def read_frame(self): | ||
| return bytes(PCM.bytes_per_frame) | ||
|
|
||
| def replace_track(self): | ||
| self.scenario.actions.append("replace_track") | ||
|
|
||
| async def write_audio(self, data): | ||
| self.scenario.actions.append(("caller_audio", len(data))) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.