You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/open-source/events-manager.mdx
+38-65
Original file line number
Diff line number
Diff line change
@@ -5,87 +5,60 @@ description: "How events are emitted and consumed."
5
5
6
6
## What is the Events Manager
7
7
8
-
The Events Manager is a class designed to facilitate asynchronous handling of events in the application. It allows for non-blocking actions on events, such as processing transcripts, managing phone calls, and other tasks. The main components of the Events Manager are the `EventsManager` class and several `Event` subclasses representing various event types.
8
+
The Events Manager consumes realtime events during conversations - it provides a framework to consume and take action on these events asychronously.
9
9
10
-
## EventsManager Class
11
-
12
-
The `EventsManager` class is responsible for managing the event queue and handling events asynchronously. The class provides methods for publishing events, starting the event loop, handling events, and ending the event loop.
The `EventsManager` constructor accepts an optional list of `EventType` subscriptions. By default, it initializes an empty set of subscriptions, an asynchronous queue, and sets the `active` attribute to `False`.
14
+
1.`TRANSCRIPT`: Indicates a partial transcript for the conversation has been received.
15
+
2.`TRANSCRIPT_COMPLETE`: Indicates the transcript is complete (ie conversation has ended).
16
+
3.`ACTION`: Indicates that a Vocode action has begun or completed.
17
+
4.`PHONE_CALL_CONNECTED`: Indicates a phone call has been connected (only gets sent during `PhoneConversation`s)
18
+
5.`PHONE_CALL_ENDED`: Indicates a phone call has ended.
24
19
25
-
### Publishing Events
20
+
##Usage
26
21
27
-
```python
28
-
defpublish_event(self, event: Event):
29
-
if event.type inself.subscriptions:
30
-
self.queue.put_nowait(event)
31
-
```
22
+
Using the events manager to take particular action when events fire requires that you subclass `vocode.streaming.utils.EventsManager` and override the `handle_event` method.
32
23
33
-
The `publish_event` method takes an `Event` object as input and adds it to the queue if its type is in the set of subscribed event types.
24
+
You can also configure which events your `EventsManager` is subscribed to by using the `subscriptions` property (see example).
34
25
35
-
### Starting the Event Loop
26
+
### Example
36
27
37
28
```python
38
-
asyncdefstart(self):
39
-
self.active =True
40
-
whileself.active:
41
-
try:
42
-
event: Event =awaitself.queue.get()
43
-
except asyncio.QueueEmpty:
44
-
await asyncio.sleep(1)
45
-
self.handle_event(event)
46
-
```
29
+
from vocode.streaming.models.events import Event, EventType
47
30
48
-
## Current Event Types
31
+
from vocode.streaming.models.events import Event, EventType
32
+
from vocode.streaming.models.transcript import TranscriptCompleteEvent
33
+
from vocode.streaming.utils.events_manager import EventsManager
49
34
50
-
The current event types include:
51
35
52
-
1.`TRANSCRIPT`: Indicates a partial transcript for the conversation has been received.
53
-
2.`TRANSCRIPT_COMPLETE`: Indicates the transcript is complete (ie conversation has ended).
54
-
3.`PHONE_CALL_CONNECTED`: Indicates a phone call has been connected.
55
-
4.`PHONE_CALL_ENDED`: Indicates a phone call has ended.
56
-
5.`RECORDING`: (Vonage Only) Indicates a secure URL containing a recording of the call is available. Requires `recording=true` in `VonageConfig`.
36
+
classCustomEventsManager(EventsManager):
37
+
def__init__(self):
38
+
super().__init__([EventType.TRANSCRIPT_COMPLETE])
57
39
58
-
## Example Usage
40
+
asyncdefhandle_event(self, event: Event):
41
+
ifisinstance(event, TranscriptCompleteEvent):
42
+
print("The call has finished, the transcript was", event.transcript.to_string())
43
+
```
59
44
60
-
The following example demonstrates how the `EventsManager`class can be used to consume the `TRANSCRIPT_COMPLETE` event and save the transcript to a file using the `add_transcript` method:
45
+
In this example, we create a custom `EventsManager`subclass is created with a subscription to the `TRANSCRIPT_COMPLETE` event and then print the transcript when we receive the event.
61
46
62
-
```python
63
-
import logging
64
-
from fastapi import FastAPI
65
-
from vocode.streaming.models.events import Event, EventType, TranscriptCompleteEvent
66
-
from vocode.streaming.utils import events_manager
67
-
from call_transcript_utils import add_transcript
47
+
To use `CustomEventsManager`, you can pass it into any Conversation, e.g.
68
48
69
-
app = FastAPI(docs_url=None)
49
+
```
50
+
...
51
+
conversation = StreamingConversation(
52
+
...,
53
+
events_manager=CustomEventsManager()
54
+
)
55
+
```
70
56
71
-
logging.basicConfig()
72
-
logger = logging.getLogger(__name__)
73
-
logger.setLevel(logging.DEBUG)
57
+
You can also pass it into a `TelephonyServer`, like:
In this example, a custom `EventsManager` subclass is created with a subscription to the `TRANSCRIPT_COMPLETE` event. The `handle_event` method is overridden to save the transcript to a file using the `add_transcript` method when the `TRANSCRIPT_COMPLETE` event is received.
0 commit comments